UNPKG

@swtc/address-codec

Version:

encode decode base58 SWTC identifiers

3 lines (2 loc) 3.2 kB
import e from"base-x";import t from"crypto";import{funcConcatArgs as n,funcSeqEqual as o,funcGetChain as s,SM3 as r}from"@swtc/common";var c=t.createHash;class d{constructor(t){this.sha256=t.sha256,this.alphabet=t.alphabet,this.codec=e(this.alphabet),this.base=this.alphabet.length}encode(e,t){const n=t.versions;return this.encodeVersioned(e,n,t.expectedLength)}encodeVersioned(e,t,o){if(o&&e.length!==o)throw new Error("unexpected_payload_length: bytes.length does not match expectedLength");return this.encodeChecked(Buffer.from(n(t,e)))}encodeChecked(e){const t=this.sha256(this.sha256(e)).slice(0,4);return this.encodeRaw(Buffer.from(n(e,t)))}encodeRaw(e){return this.codec.encode(e)}decode(e,t){const n=t.versions,s=t.versionTypes,r=this.decodeChecked(e);if(n.length>1&&!t.expectedLength)throw new Error("expectedLength is required because there are >= 2 possible versions");const c="number"==typeof n[0]?1:n[0].length,d=t.expectedLength||r.length-c,i=r.slice(0,-d),h=r.slice(-d);for(let e=0;e<n.length;e++){const t=Array.isArray(n[e])?n[e]:[n[e]];if(o(i,t))return{version:t,bytes:h,type:s?s[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),n=e.slice(-4);return o(t,n)}}const i=[1,225,75];function h(e="jingtum"){let t;const n=!1,o="SWT",h="jpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65rkm8oFqi1tuvAxyz";if("string"==typeof e){const n=s(e);if(!n)throw new Error("the chain you specified is not registered");t=n}else t=e;const a=t.guomi||n,u=(t.currency||o).toUpperCase(),p=t.ACCOUNT_ALPHABET||h,l=new d({sha256:a?e=>(new r).update(e).digest():e=>c("sha256").update(e).digest(),alphabet:p}),g=(e,t={versionTypes:a?["ed25519","sm2p256v1"]:["ed25519","secp256k1"],versions:[i,33],expectedLength:16})=>l.decode(e,t),v=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},m=e=>{try{f(e)}catch(e){return!1}return!0};return{guomi:a,token:u,codec:l,encode:l.encode,decode:l.decode,encodeSeed:(e,t=(a?"sm2p256v1":"secp256k1"))=>{if(16!==e.length)throw new Error("entropy must have length 16");const n={expectedLength:16,versions:"ed25519"===t?i:[33]};return l.encode(e,n)},decodeSeed:g,isValidSeed:e=>{try{g(e)}catch(e){return!1}return!0},encodeAccountID:v,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:m,isValidAddress:m,encodeAddress:v,decodeAddress:f}}const a=h(),u=h("guomi");export{d as Codec,h as Factory,a as addressCodec,u as addressCodecGm}; //# sourceMappingURL=index.js.map