UNPKG

swtc-factory

Version:
197 lines 6.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const swtc_chains_1 = __importDefault(require("swtc-chains")); const keypairs_1 = require("./keypairs"); const getChain = (chain_name = "jingtum") => swtc_chains_1.default.filter((chain) => chain.code.toLowerCase() === chain_name.toLowerCase() || chain.currency.toUpperCase() === chain_name.toUpperCase())[0]; class Wallet { constructor(secret, token = "swt") { try { this._kp = new keypairs_1.KeyPairs(token); this._keypairs = this._kp.deriveKeyPair(secret); this._secret = secret; } catch (err) { this._kp = null; this._keypairs = null; this._secret = null; } } static generate(token = "swt", options = {}) { const kp = new keypairs_1.KeyPairs(token); const secret = kp.generateSeed(options); const keypair = kp.deriveKeyPair(secret); const address = kp.deriveAddress(keypair.publicKey); return { secret, address }; } static fromSecret(secret, token = "swt") { try { const kp = new keypairs_1.KeyPairs(token); const keypair = kp.deriveKeyPair(secret); const address = kp.deriveAddress(keypair.publicKey); return { secret, address }; } catch (err) { return null; } } static isValidAddress(address, token = "swt") { const kp = new keypairs_1.KeyPairs(token); return kp.checkAddress(address); } static isValidSecret(secret, token = "swt") { try { const kp = new keypairs_1.KeyPairs(token); kp.deriveKeyPair(secret); return true; } catch (err) { return false; } } static getCurrency(token = "swt") { const chain = getChain(token); return chain ? chain.currency : ""; } static getCurrencies(token = "swt") { const chain = getChain(token); return chain && chain.CURRENCIES ? chain.CURRENCIES : {}; } static getChain(token = "swt") { const chain = getChain(token); return chain ? chain.code : ""; } static getFee(token = "swt") { const chain = getChain(token); return chain && chain.fee ? chain.fee : 10000; } static getAccountZero(token = "swt") { const chain = getChain(token); return chain && chain.ACCOUNT_ZERO ? chain.ACCOUNT_ZERO : ""; } static getAccountOne(token = "swt") { const chain = getChain(token); return chain && chain.ACCOUNT_ONE ? chain.ACCOUNT_ONE : ""; } static getIssuer(token = "swt") { const chain = getChain(token); return chain && chain.issuer ? chain.issuer : ""; } static makeCurrency(currency = "swt", issuer = Wallet.getIssuer(), token = "swt") { const CURRENCIES = Wallet.getCurrencies(token); currency = currency.toUpperCase(); currency = CURRENCIES.hasOwnProperty(currency) ? CURRENCIES[currency] : currency; return currency === Wallet.getCurrency(token) ? { currency, issuer: "" } : { currency, issuer }; } static makeAmount(value = 1, currency = "swt", issuer = Wallet.getIssuer(), token) { return typeof currency === "object" ? Object.assign({}, currency, { value: Number(value) }) : Object.assign({}, this.makeCurrency(currency, issuer, token), { value: Number(value) }); } /** * get wallet address * @returns {*} */ address() { if (!this._keypairs) return null; const address = this._kp.deriveAddress(this._keypairs.publicKey); return address; } /** * get wallet secret * @returns {*} */ secret() { if (!this._keypairs) return null; return this._secret; } toJson() { if (!this._keypairs) return null; return { secret: this.secret(), address: this.address() }; } /* * Get the public key from key pair * used for local signing operation. */ getPublicKey() { if (!this._keypairs) return null; return this._keypairs.publicKey; } /** * sign message with wallet privatekey * @param message * @returns {*} */ sign(message) { if (!message) return null; if (!this._keypairs) return null; const privateKey = this._keypairs.privateKey; // Export DER encoded signature in Array return this._kp.keyPairs().signHash(message, privateKey); } /** * verify signature with wallet publickey * @param message * @param signature * @returns {*} */ verify(message, signature) { if (!this._keypairs) return null; const publicKey = this.getPublicKey(); return this._kp.keyPairs().verifyHash(message, signature, publicKey); } /** * sign message with wallet privatekey * Export DER encoded signature in Array * @param message * @returns {*} */ signTx(message) { if (!message) return null; if (!this._keypairs) return null; const privateKey = this._keypairs.privateKey; // Export DER encoded signature in Array return this._kp.keyPairs().signTx(message, privateKey); } /** * verify signature with wallet publickey * @param message * @param signature * @returns {*} */ verifyTx(message, signature) { if (!this._keypairs) return null; const publicKey = this.getPublicKey(); return this._kp.keyPairs().verifyTx(message, signature, publicKey); } } exports.Wallet = Wallet; //# sourceMappingURL=wallet.js.map