MCPcopy Create free account
hub / github.com/CashScript/cashscript / createUnlocker

Method createUnlocker

packages/cashscript/src/Contract.ts:180–211  ·  view source on GitHub ↗
(abiFunction: AbiFunction, selector?: number)

Source from the content-addressed store, hash-verified

178 }
179
180 private createUnlocker(abiFunction: AbiFunction, selector?: number): ContractFunctionUnlocker {
181 return (...args: FunctionArgument[]) => {
182 if (abiFunction.inputs.length !== args.length) {
183 throw new Error(`Incorrect number of arguments passed to function ${abiFunction.name}. Expected ${abiFunction.inputs.length} arguments (${abiFunction.inputs.map((input) => input.type)}) but got ${args.length}`);
184 }
185
186 const encodedArgs = args
187 .map((arg, i) => encodeFunctionArgument(arg, abiFunction.inputs[i].type));
188
189 const generateUnlockingBytecode = (
190 { transaction, sourceOutputs, inputIndex }: GenerateUnlockingBytecodeOptions,
191 ): Uint8Array => {
192 const bytecode = hexToBin(this.bytecode);
193
194 const completeArgs = encodedArgs.map((arg) => {
195 if (!(arg instanceof SignatureTemplate)) return arg;
196
197 // Generate transaction signature from SignatureTemplate
198 const preimage = createSighashPreimage(transaction, sourceOutputs, inputIndex, bytecode, arg.getHashType());
199 const sighash = hash256(preimage);
200 return arg.generateSignature(sighash);
201 });
202
203 const unlockingBytecode = createUnlockingBytecode(this.contractType, bytecode, completeArgs, selector);
204 return unlockingBytecode;
205 };
206
207 const generateLockingBytecode = (): Uint8Array => hexToBin(this.lockingBytecode);
208
209 return { generateUnlockingBytecode, generateLockingBytecode, contract: this, params: args, abiFunction };
210 };
211 }
212}
213
214// The public Contract type conditionally removes address/tokenAddress for p2s contracts.

Callers 1

constructorMethod · 0.95

Calls 1

encodeFunctionArgumentFunction · 0.85

Tested by

no test coverage detected