UNPKG

@dfinity/ledger-icp

Version:

A library for interfacing with the ICP ledger on the Internet Computer.

9 lines (7 loc) 6.89 kB
var N=Object.create;var H=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var V=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var z=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var v=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of j(t))!G.call(e,o)&&o!==n&&H(e,o,{get:()=>t[o],enumerable:!(r=F(t,o))||r.enumerable});return e};var J=(e,t,n)=>(n=e!=null?N(V(e)):{},v(t||!e||!e.__esModule?H(n,"default",{value:e,enumerable:!0}):n,e));import{arrayOfNumberToUint8Array as R,asciiStringToByteArray as X,bigEndianCrc32 as K,uint8ArrayToHexString as q}from"@dfinity/utils";function W(e){return e instanceof Uint8Array||e!=null&&typeof e=="object"&&e.constructor.name==="Uint8Array"}function A(e,...t){if(!W(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error(`Uint8Array expected of length ${t}, not of length=${e.length}`)}function U(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function S(e,t){A(e);let n=t.outputLen;if(e.length<n)throw new Error(`digestInto() expects output buffer of length at least ${n}`)}var g=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),h=(e,t)=>e<<32-t|e>>>t;var tt=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function $(e){if(typeof e!="string")throw new Error(`utf8ToBytes expected string, got ${typeof e}`);return new Uint8Array(new TextEncoder().encode(e))}function B(e){return typeof e=="string"&&(e=$(e)),A(e),e}var d=class{clone(){return this._cloneInto()}},et={}.toString;function T(e){let t=r=>e().update(B(r)).digest(),n=e();return t.outputLen=n.outputLen,t.blockLen=n.blockLen,t.create=()=>e(),t}function M(e,t,n,r){if(typeof e.setBigUint64=="function")return e.setBigUint64(t,n,r);let o=BigInt(32),c=BigInt(4294967295),i=Number(n>>o&c),s=Number(n&c),u=r?4:0,a=r?0:4;e.setUint32(t+u,i,r),e.setUint32(t+a,s,r)}var C=(e,t,n)=>e&t^~e&n,k=(e,t,n)=>e&t^e&n^t&n,w=class extends d{constructor(t,n,r,o){super(),this.blockLen=t,this.outputLen=n,this.padOffset=r,this.isLE=o,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=g(this.buffer)}update(t){U(this);let{view:n,buffer:r,blockLen:o}=this;t=B(t);let c=t.length;for(let i=0;i<c;){let s=Math.min(o-this.pos,c-i);if(s===o){let u=g(t);for(;o<=c-i;i+=o)this.process(u,i);continue}r.set(t.subarray(i,i+s),this.pos),this.pos+=s,i+=s,this.pos===o&&(this.process(n,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){U(this),S(t,this),this.finished=!0;let{buffer:n,view:r,blockLen:o,isLE:c}=this,{pos:i}=this;n[i++]=128,this.buffer.subarray(i).fill(0),this.padOffset>o-i&&(this.process(r,0),i=0);for(let f=i;f<o;f++)n[f]=0;M(r,o-8,BigInt(this.length*8),c),this.process(r,0);let s=g(t),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let a=u/4,p=this.get();if(a>p.length)throw new Error("_sha2: outputLen bigger than state");for(let f=0;f<a;f++)s.setUint32(4*f,p[f],c)}digest(){let{buffer:t,outputLen:n}=this;this.digestInto(t);let r=t.slice(0,n);return this.destroy(),r}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());let{blockLen:n,buffer:r,length:o,finished:c,destroyed:i,pos:s}=this;return t.length=o,t.pos=s,t.finished=c,t.destroyed=i,o%n&&t.buffer.set(r),t}};var P=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),x=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),b=new Uint32Array(64),E=class extends w{constructor(){super(64,32,8,!1),this.A=x[0]|0,this.B=x[1]|0,this.C=x[2]|0,this.D=x[3]|0,this.E=x[4]|0,this.F=x[5]|0,this.G=x[6]|0,this.H=x[7]|0}get(){let{A:t,B:n,C:r,D:o,E:c,F:i,G:s,H:u}=this;return[t,n,r,o,c,i,s,u]}set(t,n,r,o,c,i,s,u){this.A=t|0,this.B=n|0,this.C=r|0,this.D=o|0,this.E=c|0,this.F=i|0,this.G=s|0,this.H=u|0}process(t,n){for(let f=0;f<16;f++,n+=4)b[f]=t.getUint32(n,!1);for(let f=16;f<64;f++){let y=b[f-15],l=b[f-2],I=h(y,7)^h(y,18)^y>>>3,m=h(l,17)^h(l,19)^l>>>10;b[f]=m+b[f-7]+I+b[f-16]|0}let{A:r,B:o,C:c,D:i,E:s,F:u,G:a,H:p}=this;for(let f=0;f<64;f++){let y=h(s,6)^h(s,11)^h(s,25),l=p+y+C(s,u,a)+P[f]+b[f]|0,m=(h(r,2)^h(r,13)^h(r,22))+k(r,o,c)|0;p=a,a=u,u=s,s=i+l|0,i=c,c=o,o=r,r=l+m|0}r=r+this.A|0,o=o+this.B|0,c=c+this.C|0,i=i+this.D|0,s=s+this.E|0,u=u+this.F|0,a=a+this.G|0,p=p+this.H|0,this.set(r,o,c,i,s,u,a,p)}roundClean(){b.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},L=class extends E{constructor(){super(),this.A=-1056596264,this.B=914150663,this.C=812702999,this.D=-150054599,this.E=-4191439,this.F=1750603025,this.G=1694076839,this.H=-1090891868,this.outputLen=28}};var O=T(()=>new L);var D=class e{constructor(t){this.bytes=t}static fromHex(t){return new e(Uint8Array.from(Buffer.from(t,"hex")))}static fromPrincipal({principal:t,subAccount:n=_.fromID(0)}){let r=X(` account-id`),o=O.create();o.update(R([...r,...t.toUint8Array(),...n.toUint8Array()]));let c=o.digest(),i=K(c),s=new Uint8Array([...i,...c]);return new e(s)}toHex(){return q(this.bytes)}toUint8Array(){return this.bytes}toNumbers(){return Array.from(this.bytes)}toAccountIdentifierHash(){return{hash:this.toUint8Array()}}},_=class e{constructor(t){this.bytes=t}static fromBytes(t){return t.length!=32?Error("Subaccount length must be 32-bytes"):new e(t)}static fromPrincipal(t){let n=new Uint8Array(32).fill(0),r=t.toUint8Array();n[0]=r.length;for(let o=0;o<r.length;o++)n[1+o]=r[o];return new e(n)}static fromID(t){if(t<0)throw new Error("Number cannot be negative");if(t>Number.MAX_SAFE_INTEGER)throw new Error("Number is too large to fit in 32 bytes.");let n=new DataView(new ArrayBuffer(32));if(typeof n.setBigUint64=="function")n.setBigUint64(24,BigInt(t));else{let o=BigInt(1)<<BigInt(32);n.setUint32(24,Number(BigInt(t)>>BigInt(32))),n.setUint32(28,Number(BigInt(t)%o))}let r=new Uint8Array(n.buffer);return new e(r)}toUint8Array(){return this.bytes}};export{z as a,J as b,O as c,D as d,_ as e}; /*! Bundled license information: @noble/hashes/esm/utils.js: (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *) */ //# sourceMappingURL=chunk-D2JQPN4X.js.map