UNPKG

@dfinity/sns

Version:

A library for interfacing with a Service Nervous System (SNS) project.

8 lines (6 loc) • 9.64 kB
import{a as y}from"./chunk-4INNMFYU.js";import{bigIntToUint8Array as H,toNullable as O}from"@dfinity/utils";import{arrayOfNumberToUint8Array as E,asciiStringToByteArray as M,numberToUint8Array as Q}from"@dfinity/utils";function D(t){return t instanceof Uint8Array||t!=null&&typeof t=="object"&&t.constructor.name==="Uint8Array"}function w(t,...e){if(!D(t))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(t.length))throw new Error(`Uint8Array expected of length ${e}, not of length=${t.length}`)}function v(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function k(t,e){w(t);let r=e.outputLen;if(t.length<r)throw new Error(`digestInto() expects output buffer of length at least ${r}`)}var x=t=>new DataView(t.buffer,t.byteOffset,t.byteLength),m=(t,e)=>t<<32-e|t>>>e;var $=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;function _(t){if(typeof t!="string")throw new Error(`utf8ToBytes expected string, got ${typeof t}`);return new Uint8Array(new TextEncoder().encode(t))}function N(t){return typeof t=="string"&&(t=_(t)),w(t),t}var P=class{clone(){return this._cloneInto()}},W={}.toString;function A(t){let e=s=>t().update(N(s)).digest(),r=t();return e.outputLen=r.outputLen,e.blockLen=r.blockLen,e.create=()=>t(),e}function G(t,e,r,s){if(typeof t.setBigUint64=="function")return t.setBigUint64(e,r,s);let n=BigInt(32),o=BigInt(4294967295),i=Number(r>>n&o),a=Number(r&o),u=s?4:0,f=s?0:4;t.setUint32(e+u,i,s),t.setUint32(e+f,a,s)}var C=(t,e,r)=>t&e^~t&r,B=(t,e,r)=>t&e^t&r^e&r,b=class extends P{constructor(e,r,s,n){super(),this.blockLen=e,this.outputLen=r,this.padOffset=s,this.isLE=n,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(e),this.view=x(this.buffer)}update(e){v(this);let{view:r,buffer:s,blockLen:n}=this;e=N(e);let o=e.length;for(let i=0;i<o;){let a=Math.min(n-this.pos,o-i);if(a===n){let u=x(e);for(;n<=o-i;i+=n)this.process(u,i);continue}s.set(e.subarray(i,i+a),this.pos),this.pos+=a,i+=a,this.pos===n&&(this.process(r,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){v(this),k(e,this),this.finished=!0;let{buffer:r,view:s,blockLen:n,isLE:o}=this,{pos:i}=this;r[i++]=128,this.buffer.subarray(i).fill(0),this.padOffset>n-i&&(this.process(s,0),i=0);for(let c=i;c<n;c++)r[c]=0;G(s,n-8,BigInt(this.length*8),o),this.process(s,0);let a=x(e),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let f=u/4,d=this.get();if(f>d.length)throw new Error("_sha2: outputLen bigger than state");for(let c=0;c<f;c++)a.setUint32(4*c,d[c],o)}digest(){let{buffer:e,outputLen:r}=this;this.digestInto(e);let s=e.slice(0,r);return this.destroy(),s}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:r,buffer:s,length:n,finished:o,destroyed:i,pos:a}=this;return e.length=n,e.pos=a,e.finished=o,e.destroyed=i,n%r&&e.buffer.set(s),e}};var F=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]),p=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),h=new Uint32Array(64),I=class extends b{constructor(){super(64,32,8,!1),this.A=p[0]|0,this.B=p[1]|0,this.C=p[2]|0,this.D=p[3]|0,this.E=p[4]|0,this.F=p[5]|0,this.G=p[6]|0,this.H=p[7]|0}get(){let{A:e,B:r,C:s,D:n,E:o,F:i,G:a,H:u}=this;return[e,r,s,n,o,i,a,u]}set(e,r,s,n,o,i,a,u){this.A=e|0,this.B=r|0,this.C=s|0,this.D=n|0,this.E=o|0,this.F=i|0,this.G=a|0,this.H=u|0}process(e,r){for(let c=0;c<16;c++,r+=4)h[c]=e.getUint32(r,!1);for(let c=16;c<64;c++){let g=h[c-15],l=h[c-2],T=m(g,7)^m(g,18)^g>>>3,S=m(l,17)^m(l,19)^l>>>10;h[c]=S+h[c-7]+T+h[c-16]|0}let{A:s,B:n,C:o,D:i,E:a,F:u,G:f,H:d}=this;for(let c=0;c<64;c++){let g=m(a,6)^m(a,11)^m(a,25),l=d+g+C(a,u,f)+F[c]+h[c]|0,S=(m(s,2)^m(s,13)^m(s,22))+B(s,n,o)|0;d=f,f=u,u=a,a=i+l|0,i=o,o=n,n=s,s=l+S|0}s=s+this.A|0,n=n+this.B|0,o=o+this.C|0,i=i+this.D|0,a=a+this.E|0,u=u+this.F|0,f=f+this.G|0,d=d+this.H|0,this.set(s,n,o,i,a,u,f,d)}roundClean(){h.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}};var L=A(()=>new I);var R=({index:t,controller:e})=>{let s=[12,...M("neuron-stake"),...e.toUint8Array(),...Q(t)],n=L.create();return n.update(E(s)),n.digest()};var U=class{constructor({root:e,governance:r,ledger:s,swap:n,index:o,certified:i}){this.listNeurons=e=>this.governance.listNeurons(this.mergeParams(e));this.listProposals=e=>this.governance.listProposals(this.mergeParams(e));this.getProposal=e=>this.governance.getProposal(this.mergeParams(e));this.listNervousSystemFunctions=e=>this.governance.listNervousSystemFunctions(this.mergeParams(e));this.metadata=e=>Promise.all([this.governance.metadata(this.mergeParams(e)),this.ledger.metadata(this.mergeParams(e))]);this.nervousSystemParameters=e=>this.governance.nervousSystemParameters(this.mergeParams(e));this.ledgerMetadata=e=>this.ledger.metadata(this.mergeParams(e));this.transactionFee=e=>this.ledger.transactionFee(this.mergeParams(e));this.totalTokensSupply=e=>this.ledger.totalTokensSupply(this.mergeParams(e));this.balance=e=>this.ledger.balance(this.mergeParams(e));this.transfer=e=>this.ledger.transfer(e);this.getNeuron=e=>this.governance.getNeuron(this.mergeParams(e));this.queryNeuron=e=>this.governance.queryNeuron(this.mergeParams(e));this.nextNeuronAccount=async e=>{for(let r=0;r<65536;r++){let s=R({index:r,controller:e}),n={id:s},o=await this.governance.queryNeuron({neuronId:n,certified:!1});if(o===void 0&&(o=await this.governance.queryNeuron({neuronId:n,certified:!0}),o===void 0))return{account:{...this.owner,subaccount:s},index:BigInt(r)}}throw new y("No more neuron accounts available")};this.stakeNeuron=async({stakeE8s:e,source:r,controller:s,createdAt:n,fee:o})=>{this.assertCertified("stakeNeuron");let{account:i,index:a}=await this.nextNeuronAccount(s);if(i.subaccount===void 0)throw new y("There was an error creating the neuron subaccount");return await this.ledger.transfer({amount:e,to:{owner:i.owner,subaccount:O(i.subaccount)},from_subaccount:r.subaccount,memo:H(a),created_at_time:n,fee:o}),this.governance.claimNeuron({memo:BigInt(a),controller:s,subaccount:i.subaccount})};this.increaseStakeNeuron=async({stakeE8s:e,source:r,neuronId:s})=>(this.assertCertified("stakeNeuron"),await this.ledger.transfer({amount:e,to:{...this.owner,subaccount:O(Uint8Array.from(s.id))},from_subaccount:r.subaccount}),this.governance.refreshNeuron(s));this.getNeuronBalance=async e=>{let r={...this.owner,subaccount:Uint8Array.from(e.id)};return this.ledger.balance({...r,certified:this.certified})};this.addNeuronPermissions=e=>this.governance.addNeuronPermissions(e);this.refreshNeuron=e=>this.governance.refreshNeuron(e);this.claimNeuron=e=>this.governance.claimNeuron(e);this.removeNeuronPermissions=e=>this.governance.removeNeuronPermissions(e);this.splitNeuron=e=>this.governance.splitNeuron(e);this.disburse=e=>this.governance.disburse(e);this.startDissolving=e=>this.governance.startDissolving(e);this.stopDissolving=e=>this.governance.stopDissolving(e);this.setDissolveTimestamp=e=>this.governance.setDissolveTimestamp(e);this.increaseDissolveDelay=e=>this.governance.increaseDissolveDelay(e);this.setTopicFollowees=e=>this.governance.setTopicFollowees(e);this.setFollowing=e=>this.governance.setFollowing(e);this.registerVote=async e=>this.governance.registerVote(e);this.swapState=e=>this.swap.state(this.mergeParams(e));this.notifyPaymentFailure=()=>this.swap.notifyPaymentFailure();this.notifyParticipation=e=>this.swap.notifyParticipation(e);this.getUserCommitment=e=>this.swap.getUserCommitment(this.mergeParams(e));this.getOpenTicket=e=>this.swap.getOpenTicket(this.mergeParams(e));this.newSaleTicket=e=>this.swap.newSaleTicket(e);this.getLifecycle=e=>this.swap.getLifecycle(this.mergeParams(e));this.getFinalizationStatus=e=>this.swap.getFinalizationStatus(this.mergeParams(e));this.getSaleParameters=e=>this.swap.getSaleParameters(this.mergeParams(e));this.getDerivedState=e=>this.swap.getDerivedState(this.mergeParams(e));this.getTransactions=e=>this.index.getTransactions(e);this.stakeMaturity=e=>this.governance.stakeMaturity(e);this.disburseMaturity=e=>this.governance.disburseMaturity(e);this.autoStakeMaturity=e=>this.governance.autoStakeMaturity(e);this.assertCertified=e=>{if(!this.certified)throw new y(`Call to ${e} needs to be certified`)};this.root=e,this.governance=r,this.ledger=s,this.swap=n,this.index=o,this.certified=i}get canisterIds(){return{rootCanisterId:this.root.canisterId,ledgerCanisterId:this.ledger.canisterId,governanceCanisterId:this.governance.canisterId,swapCanisterId:this.swap.canisterId,indexCanisterId:this.index.canisterId}}mergeParams(e){return{...e,certified:this.certified}}get owner(){return{owner:this.canisterIds.governanceCanisterId}}};export{R as a,U as b}; /*! Bundled license information: @noble/hashes/esm/utils.js: (*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *) */ //# sourceMappingURL=chunk-TJDVNGQZ.js.map