feat: support arbitrary instruction index for secp256k1 instructions

Co-authored-by: Michael Piazza <michael.piazza.mp@gmail.com>
This commit is contained in:
Josh 2021-08-10 15:39:26 -07:00 committed by GitHub
parent 83f0915e15
commit 3b506b568a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 6 deletions

View File

@ -23,6 +23,7 @@ export type CreateSecp256k1InstructionWithPublicKeyParams = {
message: Buffer | Uint8Array | Array<number>; message: Buffer | Uint8Array | Array<number>;
signature: Buffer | Uint8Array | Array<number>; signature: Buffer | Uint8Array | Array<number>;
recoveryId: number; recoveryId: number;
instructionIndex?: number;
}; };
/** /**
@ -33,6 +34,7 @@ export type CreateSecp256k1InstructionWithEthAddressParams = {
message: Buffer | Uint8Array | Array<number>; message: Buffer | Uint8Array | Array<number>;
signature: Buffer | Uint8Array | Array<number>; signature: Buffer | Uint8Array | Array<number>;
recoveryId: number; recoveryId: number;
instructionIndex?: number;
}; };
/** /**
@ -41,6 +43,7 @@ export type CreateSecp256k1InstructionWithEthAddressParams = {
export type CreateSecp256k1InstructionWithPrivateKeyParams = { export type CreateSecp256k1InstructionWithPrivateKeyParams = {
privateKey: Buffer | Uint8Array | Array<number>; privateKey: Buffer | Uint8Array | Array<number>;
message: Buffer | Uint8Array | Array<number>; message: Buffer | Uint8Array | Array<number>;
instructionIndex?: number;
}; };
const SECP256K1_INSTRUCTION_LAYOUT = BufferLayout.struct([ const SECP256K1_INSTRUCTION_LAYOUT = BufferLayout.struct([
@ -98,12 +101,14 @@ export class Secp256k1Program {
static createInstructionWithPublicKey( static createInstructionWithPublicKey(
params: CreateSecp256k1InstructionWithPublicKeyParams, params: CreateSecp256k1InstructionWithPublicKeyParams,
): TransactionInstruction { ): TransactionInstruction {
const {publicKey, message, signature, recoveryId} = params; const {publicKey, message, signature, recoveryId, instructionIndex} =
params;
return Secp256k1Program.createInstructionWithEthAddress({ return Secp256k1Program.createInstructionWithEthAddress({
ethAddress: Secp256k1Program.publicKeyToEthAddress(publicKey), ethAddress: Secp256k1Program.publicKeyToEthAddress(publicKey),
message, message,
signature, signature,
recoveryId, recoveryId,
instructionIndex,
}); });
} }
@ -114,7 +119,13 @@ export class Secp256k1Program {
static createInstructionWithEthAddress( static createInstructionWithEthAddress(
params: CreateSecp256k1InstructionWithEthAddressParams, params: CreateSecp256k1InstructionWithEthAddressParams,
): TransactionInstruction { ): TransactionInstruction {
const {ethAddress: rawAddress, message, signature, recoveryId} = params; const {
ethAddress: rawAddress,
message,
signature,
recoveryId,
instructionIndex = 0,
} = params;
let ethAddress; let ethAddress;
if (typeof rawAddress === 'string') { if (typeof rawAddress === 'string') {
@ -146,12 +157,12 @@ export class Secp256k1Program {
{ {
numSignatures, numSignatures,
signatureOffset, signatureOffset,
signatureInstructionIndex: 0, signatureInstructionIndex: instructionIndex,
ethAddressOffset, ethAddressOffset,
ethAddressInstructionIndex: 0, ethAddressInstructionIndex: instructionIndex,
messageDataOffset, messageDataOffset,
messageDataSize: message.length, messageDataSize: message.length,
messageInstructionIndex: 0, messageInstructionIndex: instructionIndex,
signature: toBuffer(signature), signature: toBuffer(signature),
ethAddress: toBuffer(ethAddress), ethAddress: toBuffer(ethAddress),
recoveryId, recoveryId,
@ -175,7 +186,7 @@ export class Secp256k1Program {
static createInstructionWithPrivateKey( static createInstructionWithPrivateKey(
params: CreateSecp256k1InstructionWithPrivateKeyParams, params: CreateSecp256k1InstructionWithPrivateKeyParams,
): TransactionInstruction { ): TransactionInstruction {
const {privateKey: pkey, message} = params; const {privateKey: pkey, message, instructionIndex} = params;
assert( assert(
pkey.length === PRIVATE_KEY_BYTES, pkey.length === PRIVATE_KEY_BYTES,
@ -195,6 +206,7 @@ export class Secp256k1Program {
message, message,
signature, signature,
recoveryId, recoveryId,
instructionIndex,
}); });
} catch (error) { } catch (error) {
throw new Error(`Error creating instruction; ${error}`); throw new Error(`Error creating instruction; ${error}`);