UNPKG

@secux/app-sol

Version:
18 lines (15 loc) 16.4 kB
"use strict"; /*! Copyright 2022 SecuX Technology Inc Copyright Chen Wei-En Copyright Wu Tsung-Yu Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */var _Transaction_recentBlockhash,_Transaction_numRequiredSignatures,_Transaction_numReadonlySignedAccounts,_Transaction_numReadonlyUnsignedAccounts,_Transaction_signedKeys,_Transaction_readonlySignedKeys,_Transaction_unsignedKeys,_Transaction_readonlyUnsignedKeys,_Transaction_instructions,_Transaction_sigMap,_Transaction_cache,_TransactionV0_addressTableLookups,__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(receiver,state,value,kind,f){if("m"===kind)throw new TypeError("Private method is not writable");if("a"===kind&&!f)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof state?receiver!==state||!f:!state.has(receiver))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===kind?f.call(receiver,value):f?f.value=value:state.set(receiver,value),value},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(receiver,state,kind,f){if("a"===kind&&!f)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof state?receiver!==state||!f:!state.has(receiver))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===kind?f:"a"===kind?f.call(receiver):f?f.value:state.get(receiver)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.TransactionV0=exports.Transaction=void 0;const nacl=require("tweetnacl"),bs58_1=require("@secux/utility/lib/bs58"),layout_1=require("./layout"),utils_1=require("./utils");class Transaction{constructor(blockHash){_Transaction_recentBlockhash.set(this,void 0),_Transaction_numRequiredSignatures.set(this,0),_Transaction_numReadonlySignedAccounts.set(this,0),_Transaction_numReadonlyUnsignedAccounts.set(this,0),_Transaction_signedKeys.set(this,[]),_Transaction_readonlySignedKeys.set(this,[]),_Transaction_unsignedKeys.set(this,[]),_Transaction_readonlyUnsignedKeys.set(this,[]),_Transaction_instructions.set(this,[]),_Transaction_sigMap.set(this,{}),_Transaction_cache.set(this,void 0),__classPrivateFieldSet(this,_Transaction_recentBlockhash,blockHash,"f")}static from(data){const byteArray=[...data],signatureCount=decodeLength(byteArray),signatures=byteArray.splice(0,64*signatureCount),tx=Transaction.fromMessage(Buffer.from(byteArray));for(const publickey of __classPrivateFieldGet(tx,_Transaction_signedKeys,"f")){const signature=Buffer.from(signatures.splice(0,64));signature.findIndex((n=>0!==n))<0||(__classPrivateFieldGet(tx,_Transaction_sigMap,"f")[publickey]=signature)}return tx}static fromMessage(data){const prefix=data[0],tx=prefix===(127&prefix)?new Transaction(""):new TransactionV0(""),byteArray=tx.deserializeMessage(data);return __classPrivateFieldSet(tx,_Transaction_cache,data.slice(0,data.length-byteArray.length),"f"),tx}addInstruction(ins){__classPrivateFieldGet(this,_Transaction_instructions,"f").push(ins);for(const account of ins.accounts){const pk=account.publickey;account.isSigner?account.isWritable?__classPrivateFieldGet(this,_Transaction_signedKeys,"f").push(pk):__classPrivateFieldGet(this,_Transaction_readonlySignedKeys,"f").push(pk):account.isWritable?__classPrivateFieldGet(this,_Transaction_unsignedKeys,"f").push(pk):__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f").push(pk)}__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f").push(ins.programId)}dataForSign(feePayer){return __classPrivateFieldGet(this,_Transaction_cache,"f")||__classPrivateFieldSet(this,_Transaction_cache,this.serializeMessage(feePayer),"f"),__classPrivateFieldGet(this,_Transaction_cache,"f")}addSignature(publickey,signature){if(!__classPrivateFieldGet(this,_Transaction_cache,"f"))throw Error("message serialization needed");if(64!==signature.length)throw Error(`invalid signature length, got ${signature.toString("hex")}`);if(!__classPrivateFieldGet(this,_Transaction_signedKeys,"f").find((x=>x===publickey)))throw Error(`invalid account for siging, got ${bs58_1.Base58.encode(Buffer.from(publickey,"hex"))}`);__classPrivateFieldGet(this,_Transaction_sigMap,"f")[publickey]=signature}serialize(){if(!__classPrivateFieldGet(this,_Transaction_cache,"f"))throw Error("message serialization needed");const sigCount=encodeLength(__classPrivateFieldGet(this,_Transaction_signedKeys,"f").length),size=sigCount.length+64*__classPrivateFieldGet(this,_Transaction_signedKeys,"f").length+__classPrivateFieldGet(this,_Transaction_cache,"f").length;if(size>1232)throw Error(`transaction too large (maximum: 1232), got ${size}`);const wire=Buffer.alloc(size);let offset=sigCount.copy(wire);for(const key of __classPrivateFieldGet(this,_Transaction_signedKeys,"f")){const publickey=Buffer.from(key,"hex"),sig=__classPrivateFieldGet(this,_Transaction_sigMap,"f")[key];if(sig){if(!nacl.sign.detached.verify(__classPrivateFieldGet(this,_Transaction_cache,"f"),sig,publickey))throw Error(`invalid signature, got ${sig.toString("hex")} for ${key}`);sig.copy(wire,offset)}offset+=64}return __classPrivateFieldGet(this,_Transaction_cache,"f").copy(wire,offset),wire}get Signers(){if(!__classPrivateFieldGet(this,_Transaction_cache,"f"))throw Error("message serialization needed");return[...__classPrivateFieldGet(this,_Transaction_signedKeys,"f")]}get Version(){}get numRequiredSignatures(){return __classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")}get numReadonlySignedAccounts(){return __classPrivateFieldGet(this,_Transaction_numReadonlySignedAccounts,"f")}get numReadonlyUnsignedAccounts(){return __classPrivateFieldGet(this,_Transaction_numReadonlyUnsignedAccounts,"f")}get recentBlockhash(){return __classPrivateFieldGet(this,_Transaction_recentBlockhash,"f")}serializeMessage(feePayer){const{accountKeys,instructions}=this.makeIndexed(feePayer),signData=Buffer.allocUnsafe(1232);let offset=0;const tx={numRequiredSignatures:Buffer.from([__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")]),numReadonlySignedAccounts:Buffer.from([__classPrivateFieldGet(this,_Transaction_numReadonlySignedAccounts,"f")]),numReadonlyUnsignedAccounts:Buffer.from([__classPrivateFieldGet(this,_Transaction_numReadonlyUnsignedAccounts,"f")]),keyCount:encodeLength(accountKeys.length),keys:accountKeys.map((x=>Buffer.from(x,"hex"))),recentBlockhash:bs58_1.Base58.decode(__classPrivateFieldGet(this,_Transaction_recentBlockhash,"f"))};offset+=(0,layout_1.signDataLayout)(tx).encode(tx,signData);const encodedInstructions=this.encodeInstructions(instructions);return offset+encodedInstructions.length>1232&&Error("Exceed the maximum over-the-wire size"),offset+=encodeLength(instructions.length).copy(signData,offset),offset+=encodedInstructions.copy(signData,offset),signData.slice(0,offset)}deserializeMessage(data){const byteArray=[...data];__classPrivateFieldSet(this,_Transaction_numRequiredSignatures,byteArray.shift(),"f"),__classPrivateFieldSet(this,_Transaction_numReadonlySignedAccounts,byteArray.shift(),"f"),__classPrivateFieldSet(this,_Transaction_numReadonlyUnsignedAccounts,byteArray.shift(),"f");const addKey=(src,size)=>{for(let i=0;i<size;i++){const key=byteArray.splice(0,32);src.push(Buffer.from(key).toString("hex"))}},accountSize=decodeLength(byteArray);addKey(__classPrivateFieldGet(this,_Transaction_signedKeys,"f"),__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")-__classPrivateFieldGet(this,_Transaction_numReadonlySignedAccounts,"f")),addKey(__classPrivateFieldGet(this,_Transaction_readonlySignedKeys,"f"),__classPrivateFieldGet(this,_Transaction_numReadonlySignedAccounts,"f")),addKey(__classPrivateFieldGet(this,_Transaction_unsignedKeys,"f"),__classPrivateFieldGet(this,_Transaction_numReadonlyUnsignedAccounts,"f")),addKey(__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f"),accountSize-__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")-__classPrivateFieldGet(this,_Transaction_numReadonlyUnsignedAccounts,"f")),__classPrivateFieldSet(this,_Transaction_recentBlockhash,bs58_1.Base58.encode(byteArray.splice(0,32)),"f");const accountKeys=[...__classPrivateFieldGet(this,_Transaction_signedKeys,"f"),...__classPrivateFieldGet(this,_Transaction_readonlySignedKeys,"f"),...__classPrivateFieldGet(this,_Transaction_unsignedKeys,"f"),...__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f")];__classPrivateFieldGet(this,_Transaction_instructions,"f").length=0;const insSize=decodeLength(byteArray);for(let i=0;i<insSize;i++){const programIdIndex=byteArray.shift(),programId=accountKeys[programIdIndex],accountsLen=decodeLength(byteArray),accounts=byteArray.splice(0,accountsLen).map((idx=>({publickey:accountKeys[idx],isSigner:idx<__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f"),isWritable:idx<__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")-__classPrivateFieldGet(this,_Transaction_numReadonlySignedAccounts,"f")||idx>=__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")&&idx<accountKeys.length-__classPrivateFieldGet(this,_Transaction_numReadonlyUnsignedAccounts,"f")}))),dataLen=decodeLength(byteArray),data=Buffer.from(byteArray.splice(0,dataLen));__classPrivateFieldGet(this,_Transaction_instructions,"f").push({programId,accounts,data})}return byteArray}makeIndexed(feePayer){if(0===__classPrivateFieldGet(this,_Transaction_instructions,"f").length)throw Error("No instructions provided");const set=new Set;feePayer&&set.add(feePayer);const sortOptions={localeMatcher:"best fit",usage:"sort",sensitivity:"variant",ignorePunctuation:!1,numeric:!1,caseFirst:"lower"},customSort=list=>list.sort(((a,b)=>{const _a=bs58_1.Base58.encode(Buffer.from(a,"hex")),_b=bs58_1.Base58.encode(Buffer.from(b,"hex"));return _a.localeCompare(_b,"en",sortOptions)}));__classPrivateFieldSet(this,_Transaction_signedKeys,customSort(__classPrivateFieldGet(this,_Transaction_signedKeys,"f")),"f"),__classPrivateFieldGet(this,_Transaction_signedKeys,"f").forEach((x=>set.add(x))),__classPrivateFieldSet(this,_Transaction_signedKeys,[...set],"f"),__classPrivateFieldSet(this,_Transaction_readonlySignedKeys,customSort(__classPrivateFieldGet(this,_Transaction_readonlySignedKeys,"f")),"f"),__classPrivateFieldGet(this,_Transaction_readonlySignedKeys,"f").forEach((x=>set.add(x))),__classPrivateFieldSet(this,_Transaction_readonlySignedKeys,[...set].slice(__classPrivateFieldGet(this,_Transaction_signedKeys,"f").length),"f"),__classPrivateFieldSet(this,_Transaction_numRequiredSignatures,set.size,"f"),__classPrivateFieldSet(this,_Transaction_numReadonlySignedAccounts,__classPrivateFieldGet(this,_Transaction_readonlySignedKeys,"f").length,"f"),__classPrivateFieldSet(this,_Transaction_unsignedKeys,customSort(__classPrivateFieldGet(this,_Transaction_unsignedKeys,"f")),"f"),__classPrivateFieldGet(this,_Transaction_unsignedKeys,"f").forEach((x=>set.add(x))),__classPrivateFieldSet(this,_Transaction_unsignedKeys,[...set].slice(__classPrivateFieldGet(this,_Transaction_numRequiredSignatures,"f")),"f");let cur=set.size;__classPrivateFieldSet(this,_Transaction_readonlyUnsignedKeys,customSort(__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f")),"f"),__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f").forEach((x=>set.add(x))),__classPrivateFieldSet(this,_Transaction_readonlyUnsignedKeys,[...set].slice(cur),"f"),__classPrivateFieldSet(this,_Transaction_numReadonlyUnsignedAccounts,__classPrivateFieldGet(this,_Transaction_readonlyUnsignedKeys,"f").length,"f");const accountKeys=[...set],indices={};accountKeys.forEach(((x,i)=>indices[x]=i));const instructions=__classPrivateFieldGet(this,_Transaction_instructions,"f").map((ins=>{const programIdIndex=accountKeys.findIndex((x=>x===ins.programId));if(-1===programIdIndex)throw Error("Cannot reference program id");return{programIdIndex,keyIndicesCount:encodeLength(ins.accounts.length),keyIndices:Buffer.from(ins.accounts.map((x=>indices[x.publickey]))),dataLength:encodeLength(ins.data.length),data:ins.data}}));return{accountKeys,instructions}}encodeInstructions(instructions){const data=Buffer.allocUnsafe(1232);let offset=0;for(const ins of instructions){offset+=(0,layout_1.instructionLayout)(ins).encode(ins,data,offset)}return data.slice(0,offset)}}exports.Transaction=Transaction,_Transaction_recentBlockhash=new WeakMap,_Transaction_numRequiredSignatures=new WeakMap,_Transaction_numReadonlySignedAccounts=new WeakMap,_Transaction_numReadonlyUnsignedAccounts=new WeakMap,_Transaction_signedKeys=new WeakMap,_Transaction_readonlySignedKeys=new WeakMap,_Transaction_unsignedKeys=new WeakMap,_Transaction_readonlyUnsignedKeys=new WeakMap,_Transaction_instructions=new WeakMap,_Transaction_sigMap=new WeakMap,_Transaction_cache=new WeakMap;class TransactionV0 extends Transaction{constructor(){super(...arguments),_TransactionV0_addressTableLookups.set(this,[])}addAddressLookup(lookup){/[0-9a-fA-F]{64}/.test(lookup.accountKey)||(lookup.accountKey=(0,utils_1.toPublickey)(lookup.accountKey)),__classPrivateFieldGet(this,_TransactionV0_addressTableLookups,"f").push(lookup)}serializeMessage(feePayer){const{accountKeys,instructions}=this.makeIndexed(feePayer),serializedMessage=Buffer.allocUnsafe(1232),message={prefix:128,header:{numRequiredSignatures:this.numRequiredSignatures,numReadonlySignedAccounts:this.numReadonlySignedAccounts,numReadonlyUnsignedAccounts:this.numReadonlyUnsignedAccounts},staticAccountKeysLength:encodeLength(accountKeys.length),staticAccountKeys:accountKeys.map((x=>Buffer.from(x,"hex"))),recentBlockhash:bs58_1.Base58.decode(this.recentBlockhash),instructionsLength:encodeLength(instructions.length),serializedInstructions:this.encodeInstructions(instructions),addressTableLookupsLength:encodeLength(__classPrivateFieldGet(this,_TransactionV0_addressTableLookups,"f").length),serializedAddressTableLookups:this.encodeAddressTableLookups(__classPrivateFieldGet(this,_TransactionV0_addressTableLookups,"f"))},offset=(0,layout_1.messageLayout)(message).encode(message,serializedMessage);return serializedMessage.slice(0,offset)}deserializeMessage(data){const prefix=data[0],version=127&prefix;if(prefix===version)throw Error("expected versioned message but received legacy message");if(version!==this.Version)throw Error(`expected versioned message with version ${this.Version} but found version ${version}`);const byteArray=super.deserializeMessage(data.slice(1));__classPrivateFieldGet(this,_TransactionV0_addressTableLookups,"f").length=0;const addressTableLookupsCount=decodeLength(byteArray);for(let i=0;i<addressTableLookupsCount;i++){const key=byteArray.splice(0,32),accountKey=Buffer.from(key).toString("hex"),writableLength=decodeLength(byteArray),writableIndexes=byteArray.splice(0,writableLength),readonlyLength=decodeLength(byteArray),readonlyIndexes=byteArray.splice(0,readonlyLength);__classPrivateFieldGet(this,_TransactionV0_addressTableLookups,"f").push({accountKey,writableIndexes,readonlyIndexes})}return byteArray}encodeAddressTableLookups(table){const data=Buffer.allocUnsafe(1232);let offset=0;for(const{accountKey,writableIndexes,readonlyIndexes}of table){const lookup={accountKey:Buffer.from(accountKey,"hex"),encodedWritableIndexesLength:encodeLength(writableIndexes.length),writableIndexes,encodedReadonlyIndexesLength:encodeLength(readonlyIndexes.length),readonlyIndexes};offset+=(0,layout_1.addressTableLookupLayout)(lookup).encode(lookup,data,offset)}return data.slice(0,offset)}get Version(){return 0}}function encodeLength(len){const bytes=[];for(;;){let elem=127&len;if(0==(len>>=7)){bytes.push(elem);break}elem|=128,bytes.push(elem)}return Buffer.from(bytes)}function decodeLength(bytes){let len=0,size=0;for(;;){let elem=bytes.shift();if(len|=(127&elem)<<7*size,size+=1,!(128&elem))break}return len}exports.TransactionV0=TransactionV0,_TransactionV0_addressTableLookups=new WeakMap;