UNPKG

@swtc/address-codec

Version:

encode decode base58 SWTC identifiers

3 lines (2 loc) 3.39 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("base-x"),t=require("crypto"),n=require("@swtc/common");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=s(e),o=s(t).default.createHash;class c{constructor(e){this.sha256=e.sha256,this.alphabet=e.alphabet,this.codec=r.default(this.alphabet),this.base=this.alphabet.length}encode(e,t){const n=t.versions;return this.encodeVersioned(e,n,t.expectedLength)}encodeVersioned(e,t,s){if(s&&e.length!==s)throw new Error("unexpected_payload_length: bytes.length does not match expectedLength");return this.encodeChecked(Buffer.from(n.funcConcatArgs(t,e)))}encodeChecked(e){const t=this.sha256(this.sha256(e)).slice(0,4);return this.encodeRaw(Buffer.from(n.funcConcatArgs(e,t)))}encodeRaw(e){return this.codec.encode(e)}decode(e,t){const s=t.versions,r=t.versionTypes,o=this.decodeChecked(e);if(s.length>1&&!t.expectedLength)throw new Error("expectedLength is required because there are >= 2 possible versions");const c="number"==typeof s[0]?1:s[0].length,d=t.expectedLength||o.length-c,i=o.slice(0,-d),h=o.slice(-d);for(let e=0;e<s.length;e++){const t=Array.isArray(s[e])?s[e]:[s[e]];if(n.funcSeqEqual(i,t))return{version:t,bytes:h,type:r?r[e]:null}}throw new Error("version_invalid: version bytes do not match any of the provided version(s)")}decodeChecked(e){const t=this.decodeRaw(e);if(t.length<5)throw new Error("invalid_input_size: decoded data must have length >= 5");if(!this.verifyCheckSum(t))throw new Error("checksum_invalid");return t.slice(0,-4)}decodeRaw(e){return this.codec.decode(e)}verifyCheckSum(e){const t=this.sha256(this.sha256(e.slice(0,-4))).slice(0,4),s=e.slice(-4);return n.funcSeqEqual(t,s)}}const d=[1,225,75];function i(e="jingtum"){let t;const s=!1,r="SWT",i="jpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65rkm8oFqi1tuvAxyz";if("string"==typeof e){const s=n.funcGetChain(e);if(!s)throw new Error("the chain you specified is not registered");t=s}else t=e;const h=t.guomi||s,a=(t.currency||r).toUpperCase(),u=t.ACCOUNT_ALPHABET||i,l=new c({sha256:h?e=>(new n.SM3).update(e).digest():e=>o("sha256").update(e).digest(),alphabet:u}),p=(e,t={versionTypes:h?["ed25519","sm2p256v1"]:["ed25519","secp256k1"],versions:[d,33],expectedLength:16})=>l.decode(e,t),g=e=>{const t={versions:[0],expectedLength:20};return l.encode(e,t)},f=e=>{const t={versions:[0],expectedLength:20};return l.decode(e,t).bytes},v=e=>{try{f(e)}catch(e){return!1}return!0};return{guomi:h,token:a,codec:l,encode:l.encode,decode:l.decode,encodeSeed:(e,t=(h?"sm2p256v1":"secp256k1"))=>{if(16!==e.length)throw new Error("entropy must have length 16");const n={expectedLength:16,versions:"ed25519"===t?d:[33]};return l.encode(e,n)},decodeSeed:p,isValidSeed:e=>{try{p(e)}catch(e){return!1}return!0},encodeAccountID:g,decodeAccountID:f,encodeNodePublic:e=>{const t={versions:[28],expectedLength:33};return l.encode(e,t)},decodeNodePublic:e=>{const t={versions:[28],expectedLength:33};return l.decode(e,t).bytes},encodeNodePrivate:e=>{const t={versions:[32],expectedLength:32};return l.encode(e,t)},decodeNodePrivate:e=>{const t={versions:[32],expectedLength:32};return l.decode(e,t).bytes},isValidClassicAddress:v,isValidAddress:v,encodeAddress:g,decodeAddress:f}}const h=i(),a=i("guomi");exports.Codec=c,exports.Factory=i,exports.addressCodec=h,exports.addressCodecGm=a; //# sourceMappingURL=index.js.map