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