@swtc/wallet
Version:
swtc wallet library
212 lines • 8.61 kB
JavaScript
import { funcGetChain, ACCOUNT_ID_ZERO, ACCOUNT_ID_ONE } from "@swtc/common";
import { Factory as KeypairFactory } from "@swtc/keypairs";
const Factory = (token_or_chain = "jingtum") => {
var _a;
let config;
const KeyPair = KeypairFactory(token_or_chain);
const addressCodec = KeyPair.addressCodec;
const config_default = {
code: "jingtum",
fee: 10
};
if (typeof token_or_chain === "string") {
const active_chain = funcGetChain(token_or_chain);
if (!active_chain) {
throw new Error("the chain you specified is not registered");
}
else {
config = active_chain;
}
}
else {
config = token_or_chain;
}
config.code = (config.code || config_default.code).toLowerCase();
config.currency = KeyPair.token;
config.fee = config.fee || config_default.fee;
config.guomi = KeyPair.guomi;
config.ACCOUNT_ALPHABET = KeyPair.addressCodec.codec.alphabet;
config.ACCOUNT_ZERO = addressCodec.encodeAccountID(Buffer.from(Buffer.from(ACCOUNT_ID_ZERO, "hex").toJSON().data));
config.ACCOUNT_ONE = addressCodec.encodeAccountID(Buffer.from(Buffer.from(ACCOUNT_ID_ONE, "hex").toJSON().data));
config.ACCOUNT_GENESIS = KeyPair.deriveAddress(KeyPair.deriveKeypair(addressCodec.encodeSeed(Buffer.from(KeyPair.seedFromPhrase("masterpassphrase")))).publicKey);
config.issuer = config.issuer || config.ACCOUNT_GENESIS;
config.CURRENCIES = config.CURRENCIES || {};
config.XLIB = config.XLIB || {};
return _a = class Wallet {
constructor(secret_or_private_key, algorithm = "secp256k1") {
try {
this._keypairs = KeyPair.deriveKeyPair(secret_or_private_key, algorithm);
if (typeof secret_or_private_key !== "string") {
throw new Error("use secret or private key to instantiate wallet");
}
else if (/^s/.test(secret_or_private_key)) {
this._secret = secret_or_private_key;
}
else if (secret_or_private_key.length >= 64) {
this._secret = "privatekey";
}
else {
throw new Error("use secret or private key to instantiate wallet");
}
}
catch (err) {
this._keypairs = null;
this._secret = null;
}
}
static getCurrency() {
return Wallet.config.currency || "SWT";
}
static getCurrencies() {
return Wallet.config.CURRENCIES || {};
}
static getChain() {
return Wallet.config.code || "jingtum";
}
static getFee() {
return Wallet.config.fee || 10;
}
static getAccountZero() {
return Wallet.config.ACCOUNT_ZERO;
}
static getAccountOne() {
return Wallet.config.ACCOUNT_ONE;
}
static getIssuer() {
return Wallet.config.issuer || "shouldnotfalltothisdefault";
}
static makeCurrency(currency = Wallet.getCurrency(), issuer = Wallet.getIssuer()) {
const CURRENCIES = Wallet.getCurrencies();
currency = currency.toUpperCase();
currency = CURRENCIES.hasOwnProperty(currency)
? CURRENCIES[currency]
: currency;
return currency === Wallet.getCurrency()
? { currency, issuer: "" }
: { currency, issuer };
}
static makeAmount(value = 1, currency = Wallet.getCurrency(), issuer = Wallet.getIssuer()) {
return typeof currency === "object"
? Object.assign({}, currency, { value: `${value}` })
: Object.assign({}, this.makeCurrency(currency, issuer), {
value: `${value}`
});
}
static generate(options = {}) {
const secret = KeyPair.generateSeed(options);
const keypair = KeyPair.deriveKeyPair(secret);
const address = KeyPair.deriveAddress(keypair.publicKey);
return {
secret,
address
};
}
static fromPhrase(phrase, algorithm = Wallet.guomi ? "sm2p256v1" : "secp256k1") {
return Wallet.fromSecret(addressCodec.encodeSeed(Buffer.from(KeyPair.seedFromPhrase(phrase)), algorithm), algorithm);
}
static fromSecret(secret_or_private_key, algorithm = Wallet.guomi ? "sm2p256v1" : "secp256k1") {
try {
let secret = secret_or_private_key;
const keypair = KeyPair.deriveKeyPair(secret_or_private_key, algorithm);
const address = KeyPair.deriveAddress(keypair.publicKey);
if (/^s/.test(secret_or_private_key)) {
secret = secret_or_private_key;
}
else if (secret_or_private_key.length >= 64) {
secret = "privatekey";
}
else {
throw new Error("use secret or private key to get wallet");
}
return {
secret,
address
};
}
catch (err) {
return null;
}
}
static isValidAddress(address) {
return KeyPair.isValidAddress(address);
}
static isValidSecret(secret) {
try {
KeyPair.deriveKeyPair(secret);
}
catch (err) {
return false;
}
return true;
}
static checkTx(message, signature, publicKey) {
return KeyPair.verifyTx(message, signature, publicKey);
}
address() {
if (!this._keypairs)
return null;
const address = KeyPair.deriveAddress(this._keypairs.publicKey);
return address;
}
secret() {
if (!this._keypairs)
return null;
return this._secret;
}
isEd25519() {
if (!this._keypairs)
return false;
return this._keypairs.privateKey.slice(0, 2).toUpperCase() === "ED";
}
toJson() {
if (!this._keypairs)
return null;
return {
secret: this.secret(),
address: this.address()
};
}
getPublicKey() {
if (!this._keypairs)
return null;
return this._keypairs.publicKey;
}
sign(message) {
if (!message)
return null;
if (!this._keypairs)
return null;
const privateKey = this._keypairs.privateKey;
return KeyPair.sign(message, privateKey);
}
verify(message, signature) {
if (!this._keypairs)
return false;
const publicKey = this.getPublicKey();
return KeyPair.verify(message, signature, publicKey);
}
signTx(message) {
if (!message)
return null;
if (!this._keypairs)
return null;
const privateKey = this._keypairs.privateKey;
return KeyPair.signTx(message, privateKey);
}
verifyTx(message, signature) {
if (!this._keypairs)
return false;
const publicKey = this.getPublicKey();
return KeyPair.verifyTx(message, signature, publicKey);
}
},
_a.config = config,
_a.token = config.currency,
_a.chain = config.code,
_a.KeyPair = KeyPair,
_a.hash = KeyPair.hash,
_a.guomi = config.guomi,
_a;
};
export { Factory };
//# sourceMappingURL=factory.js.map