UNPKG

@swtc/wallet

Version:
3 lines (2 loc) 3.72 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});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 e{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}}static getCurrency(){return e.config.currency||"SWT"}static getCurrencies(){return e.config.CURRENCIES||{}}static getChain(){return e.config.code||"jingtum"}static getFee(){return e.config.fee||10}static getAccountZero(){return e.config.ACCOUNT_ZERO}static getAccountOne(){return e.config.ACCOUNT_ONE}static getIssuer(){return e.config.issuer||"shouldnotfalltothisdefault"}static makeCurrency(r=e.getCurrency(),t=e.getIssuer()){const s=e.getCurrencies();return r=r.toUpperCase(),(r=s.hasOwnProperty(r)?s[r]:r)===e.getCurrency()?{currency:r,issuer:""}:{currency:r,issuer:t}}static makeAmount(r=1,t=e.getCurrency(),s=e.getIssuer()){return"object"==typeof t?Object.assign({},t,{value:""+r}):Object.assign({},this.makeCurrency(t,s),{value:""+r})}static generate(e={}){const r=n.generateSeed(e),t=n.deriveKeyPair(r);return{secret:r,address:n.deriveAddress(t.publicKey)}}static fromPhrase(r,t=(e.guomi?"sm2p256v1":"secp256k1")){return e.fromSecret(c.encodeSeed(Buffer.from(n.seedFromPhrase(r)),t),t)}static fromSecret(r,t=(e.guomi?"sm2p256v1":"secp256k1")){try{let e=r;const s=n.deriveKeyPair(r,t),i=n.deriveAddress(s.publicKey);if(/^s/.test(r))e=r;else{if(!(r.length>=64))throw new Error("use secret or private key to get wallet");e="privatekey"}return{secret:e,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)}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