createInstructionWithPubkey static method

TransactionInstruction createInstructionWithPubkey({
  1. required Uint8List pubkey,
  2. required Uint8List message,
  3. required Uint8List signature,
  4. required int? instructionIndex,
})

Creates an ed25519 instruction with a public key and signature. The public key must be a buffer that is 32 bytes long, and the signature must be a buffer of 64 bytes.

Implementation

static TransactionInstruction createInstructionWithPubkey({
  required final Uint8List pubkey,
  required final Uint8List message,
  required final Uint8List signature,
  required final int? instructionIndex,
}) {
  check(
    pubkey.length == nacl.pubkeyLength,
    'The public Key must be ${nacl.pubkeyLength} bytes but received ${pubkey.length} bytes.'
  );

  check(
    signature.length == nacl.signatureLength,
    'The signature must be ${nacl.signatureLength} bytes but received ${signature.length} bytes.'
  );

  const numSignatures = 1;
  const pubkeyOffset = 16; // instruction size.
  final signatureOffset = pubkeyOffset + pubkey.length;
  final messageDataOffset = signatureOffset + signature.length;
  final index = instructionIndex ?? 0xffff; // An index of `u16::MAX` makes it default to the
                                            //current instruction.

  final BufferWriter buffer = BufferWriter.mutable();
  buffer.setUint8(numSignatures);
  buffer.setUint8(0); // padding,
  buffer.setUint16(signatureOffset);
  buffer.setUint16(index); // signatureInstructionIndex
  buffer.setUint16(pubkeyOffset);
  buffer.setUint16(index); // publicKeyInstructionIndex
  buffer.setUint16(messageDataOffset);
  buffer.setUint16(message.length);
  buffer.setUint16(index); // messageInstructionIndex

  buffer.setBuffer(pubkey);
  buffer.setBuffer(signature);
  buffer.setBuffer(message);

  return TransactionInstruction(
    keys: const [],
    programId: Ed25519Program.programId,
    data: buffer.toBuffer(slice: true).asUint8List(),
  );
}