2018-09-13 18:48:51 -07:00
|
|
|
// @flow
|
|
|
|
|
2018-11-18 08:48:14 -08:00
|
|
|
import invariant from 'assert';
|
2018-09-13 18:48:51 -07:00
|
|
|
import nacl from 'tweetnacl';
|
|
|
|
import bs58 from 'bs58';
|
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
import type {CompiledInstruction} from './message';
|
|
|
|
import {Message} from './message';
|
2018-10-23 19:13:59 -07:00
|
|
|
import {PublicKey} from './publickey';
|
2018-11-28 10:06:17 -08:00
|
|
|
import {Account} from './account';
|
2019-01-31 01:16:07 -08:00
|
|
|
import * as shortvec from './util/shortvec-encoding';
|
2019-03-04 08:06:33 -08:00
|
|
|
import type {Blockhash} from './blockhash';
|
2018-09-13 18:48:51 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {string} TransactionSignature
|
|
|
|
*/
|
|
|
|
export type TransactionSignature = string;
|
|
|
|
|
2019-08-30 16:22:11 -07:00
|
|
|
/**
|
|
|
|
* Default (empty) signature
|
|
|
|
*
|
|
|
|
* Signatures are 64 bytes in length
|
|
|
|
*/
|
2020-01-13 15:17:32 -08:00
|
|
|
const DEFAULT_SIGNATURE = Buffer.alloc(64).fill(0);
|
2019-08-30 16:22:11 -07:00
|
|
|
|
2018-11-27 18:05:23 -08:00
|
|
|
/**
|
|
|
|
* Maximum over-the-wire size of a Transaction
|
2019-05-28 08:51:46 -07:00
|
|
|
*
|
|
|
|
* 1280 is IPv6 minimum MTU
|
|
|
|
* 40 bytes is the size of the IPv6 header
|
|
|
|
* 8 bytes is the size of the fragment header
|
2018-11-27 18:05:23 -08:00
|
|
|
*/
|
2019-05-28 08:51:46 -07:00
|
|
|
export const PACKET_DATA_SIZE = 1280 - 40 - 8;
|
2018-11-27 18:05:23 -08:00
|
|
|
|
2019-11-16 08:28:14 -08:00
|
|
|
const PUBKEY_LENGTH = 32;
|
|
|
|
const SIGNATURE_LENGTH = 64;
|
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
/**
|
|
|
|
* Account metadata used to define instructions
|
|
|
|
*
|
|
|
|
* @typedef {Object} AccountMeta
|
|
|
|
* @property {PublicKey} pubkey An account's public key
|
|
|
|
* @property {boolean} isSigner True if an instruction requires a transaction signature matching `pubkey`
|
|
|
|
* @property {boolean} isWritable True if the `pubkey` can be loaded as a read-write account.
|
|
|
|
*/
|
|
|
|
export type AccountMeta = {
|
|
|
|
pubkey: PublicKey,
|
|
|
|
isSigner: boolean,
|
|
|
|
isWritable: boolean,
|
|
|
|
};
|
|
|
|
|
2018-10-23 15:17:43 -07:00
|
|
|
/**
|
|
|
|
* List of TransactionInstruction object fields that may be initialized at construction
|
|
|
|
*
|
|
|
|
* @typedef {Object} TransactionInstructionCtorFields
|
|
|
|
* @property {?Array<PublicKey>} keys
|
|
|
|
* @property {?PublicKey} programId
|
2019-03-14 13:27:47 -07:00
|
|
|
* @property {?Buffer} data
|
2018-10-23 15:17:43 -07:00
|
|
|
*/
|
2019-09-13 17:07:13 -07:00
|
|
|
export type TransactionInstructionCtorFields = {|
|
2020-04-30 09:05:29 -07:00
|
|
|
keys?: Array<AccountMeta>,
|
2018-11-04 11:41:21 -08:00
|
|
|
programId?: PublicKey,
|
2019-03-14 13:27:47 -07:00
|
|
|
data?: Buffer,
|
2018-10-23 15:17:43 -07:00
|
|
|
|};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transaction Instruction class
|
|
|
|
*/
|
|
|
|
export class TransactionInstruction {
|
|
|
|
/**
|
|
|
|
* Public keys to include in this transaction
|
2019-04-10 12:31:50 -07:00
|
|
|
* Boolean represents whether this pubkey needs to sign the transaction
|
2018-10-23 15:17:43 -07:00
|
|
|
*/
|
2020-04-30 09:05:29 -07:00
|
|
|
keys: Array<AccountMeta> = [];
|
2018-10-23 15:17:43 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Program Id to execute
|
|
|
|
*/
|
|
|
|
programId: PublicKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Program input
|
|
|
|
*/
|
2019-03-14 13:27:47 -07:00
|
|
|
data: Buffer = Buffer.alloc(0);
|
2018-10-23 15:17:43 -07:00
|
|
|
|
|
|
|
constructor(opts?: TransactionInstructionCtorFields) {
|
|
|
|
opts && Object.assign(this, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:46:09 -08:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
type SignaturePubkeyPair = {|
|
|
|
|
signature: Buffer | null,
|
|
|
|
publicKey: PublicKey,
|
|
|
|
|};
|
|
|
|
|
2018-09-18 12:46:59 -07:00
|
|
|
/**
|
|
|
|
* List of Transaction object fields that may be initialized at construction
|
2018-09-20 15:35:41 -07:00
|
|
|
*
|
|
|
|
* @typedef {Object} TransactionCtorFields
|
2020-04-30 09:05:29 -07:00
|
|
|
* @property {?Blockhash} recentBlockhash A recent blockhash
|
|
|
|
* @property {?Array<SignaturePubkeyPair>} signatures One or more signatures
|
2018-11-28 10:06:17 -08:00
|
|
|
*
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
|
|
|
type TransactionCtorFields = {|
|
2019-05-23 16:18:13 -07:00
|
|
|
recentBlockhash?: Blockhash | null,
|
2020-01-06 17:12:04 -08:00
|
|
|
nonceInfo?: NonceInformation | null,
|
2018-11-28 10:06:17 -08:00
|
|
|
signatures?: Array<SignaturePubkeyPair>,
|
2018-09-18 12:46:59 -07:00
|
|
|
|};
|
|
|
|
|
2020-01-06 17:12:04 -08:00
|
|
|
/**
|
|
|
|
* NonceInformation to be used to build a Transaction.
|
|
|
|
*
|
|
|
|
* @typedef {Object} NonceInformation
|
2020-04-30 09:05:29 -07:00
|
|
|
* @property {Blockhash} nonce The current Nonce blockhash
|
|
|
|
* @property {TransactionInstruction} nonceInstruction AdvanceNonceAccount Instruction
|
2020-01-06 17:12:04 -08:00
|
|
|
*/
|
|
|
|
type NonceInformation = {|
|
|
|
|
nonce: Blockhash,
|
|
|
|
nonceInstruction: TransactionInstruction,
|
|
|
|
|};
|
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
/**
|
2018-10-23 15:17:43 -07:00
|
|
|
* Transaction class
|
2018-09-13 18:48:51 -07:00
|
|
|
*/
|
|
|
|
export class Transaction {
|
|
|
|
/**
|
2018-11-18 08:48:14 -08:00
|
|
|
* Signatures for the transaction. Typically created by invoking the
|
2018-11-28 10:06:17 -08:00
|
|
|
* `sign()` method
|
2018-09-13 18:48:51 -07:00
|
|
|
*/
|
2018-11-18 08:48:14 -08:00
|
|
|
signatures: Array<SignaturePubkeyPair> = [];
|
|
|
|
|
|
|
|
/**
|
2019-05-08 09:33:04 -07:00
|
|
|
* The first (payer) Transaction signature
|
2018-11-18 08:48:14 -08:00
|
|
|
*/
|
|
|
|
get signature(): Buffer | null {
|
|
|
|
if (this.signatures.length > 0) {
|
|
|
|
return this.signatures[0].signature;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-13 18:48:51 -07:00
|
|
|
|
2018-09-18 12:46:59 -07:00
|
|
|
/**
|
2018-10-23 15:17:43 -07:00
|
|
|
* The instructions to atomically execute
|
2018-09-18 12:46:59 -07:00
|
|
|
*/
|
2018-10-23 15:17:43 -07:00
|
|
|
instructions: Array<TransactionInstruction> = [];
|
2018-09-18 12:46:59 -07:00
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
/**
|
|
|
|
* A recent transaction id. Must be populated by the caller
|
|
|
|
*/
|
2019-05-23 16:18:13 -07:00
|
|
|
recentBlockhash: Blockhash | null;
|
2018-09-13 18:48:51 -07:00
|
|
|
|
2020-01-06 17:12:04 -08:00
|
|
|
/**
|
|
|
|
* Optional Nonce information. If populated, transaction will use a durable
|
|
|
|
* Nonce hash instead of a recentBlockhash. Must be populated by the caller
|
|
|
|
*/
|
|
|
|
nonceInfo: NonceInformation | null;
|
|
|
|
|
2018-10-23 19:13:59 -07:00
|
|
|
/**
|
|
|
|
* Construct an empty Transaction
|
|
|
|
*/
|
2018-09-18 12:46:59 -07:00
|
|
|
constructor(opts?: TransactionCtorFields) {
|
|
|
|
opts && Object.assign(this, opts);
|
|
|
|
}
|
|
|
|
|
2018-10-23 19:13:59 -07:00
|
|
|
/**
|
2018-11-28 10:06:17 -08:00
|
|
|
* Add one or more instructions to this Transaction
|
2018-10-23 19:13:59 -07:00
|
|
|
*/
|
2018-11-28 10:06:17 -08:00
|
|
|
add(
|
2019-05-23 16:18:13 -07:00
|
|
|
...items: Array<
|
|
|
|
Transaction | TransactionInstruction | TransactionInstructionCtorFields,
|
|
|
|
>
|
2018-11-28 10:06:17 -08:00
|
|
|
): Transaction {
|
|
|
|
if (items.length === 0) {
|
|
|
|
throw new Error('No instructions');
|
2018-10-23 15:17:43 -07:00
|
|
|
}
|
2018-11-28 10:06:17 -08:00
|
|
|
|
2020-07-30 10:48:25 -07:00
|
|
|
items.forEach((item: any) => {
|
|
|
|
if ('instructions' in item) {
|
2018-11-28 10:06:17 -08:00
|
|
|
this.instructions = this.instructions.concat(item.instructions);
|
2020-07-30 10:48:25 -07:00
|
|
|
} else if ('data' in item && 'programId' in item && 'keys' in item) {
|
2019-05-23 16:18:13 -07:00
|
|
|
this.instructions.push(item);
|
2018-11-28 10:06:17 -08:00
|
|
|
} else {
|
|
|
|
this.instructions.push(new TransactionInstruction(item));
|
|
|
|
}
|
|
|
|
});
|
2018-10-23 15:17:43 -07:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
/**
|
2020-04-30 09:05:29 -07:00
|
|
|
* Compile transaction data
|
2018-09-13 18:48:51 -07:00
|
|
|
*/
|
2020-04-30 09:05:29 -07:00
|
|
|
compileMessage(): Message {
|
2020-01-06 17:12:04 -08:00
|
|
|
const {nonceInfo} = this;
|
2020-01-07 16:57:56 -08:00
|
|
|
if (nonceInfo && this.instructions[0] != nonceInfo.nonceInstruction) {
|
2020-01-06 17:12:04 -08:00
|
|
|
this.recentBlockhash = nonceInfo.nonce;
|
|
|
|
this.instructions.unshift(nonceInfo.nonceInstruction);
|
|
|
|
}
|
2019-03-04 08:06:33 -08:00
|
|
|
const {recentBlockhash} = this;
|
|
|
|
if (!recentBlockhash) {
|
|
|
|
throw new Error('Transaction recentBlockhash required');
|
2018-09-13 18:48:51 -07:00
|
|
|
}
|
2018-10-23 15:17:43 -07:00
|
|
|
|
2018-10-23 19:13:59 -07:00
|
|
|
if (this.instructions.length < 1) {
|
|
|
|
throw new Error('No instructions provided');
|
2018-10-23 15:17:43 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 09:42:01 -08:00
|
|
|
let numReadonlySignedAccounts = 0;
|
|
|
|
let numReadonlyUnsignedAccounts = 0;
|
2019-01-31 01:16:07 -08:00
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
const programIds: string[] = [];
|
|
|
|
const accountMetas: AccountMeta[] = [];
|
2018-10-23 19:13:59 -07:00
|
|
|
this.instructions.forEach(instruction => {
|
2020-04-30 09:05:29 -07:00
|
|
|
instruction.keys.forEach(accountMeta => {
|
|
|
|
accountMetas.push(accountMeta);
|
2019-04-10 12:31:50 -07:00
|
|
|
});
|
2019-05-23 09:42:11 -07:00
|
|
|
|
|
|
|
const programId = instruction.programId.toString();
|
2019-05-24 15:07:16 -07:00
|
|
|
if (!programIds.includes(programId)) {
|
|
|
|
programIds.push(programId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-01 19:26:36 -07:00
|
|
|
// Append programID account metas
|
|
|
|
programIds.forEach(programId => {
|
|
|
|
accountMetas.push({
|
|
|
|
pubkey: new PublicKey(programId),
|
|
|
|
isSigner: false,
|
|
|
|
isWritable: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Prefix accountMetas with feePayer here whenever that gets implemented
|
|
|
|
|
|
|
|
// Sort. Prioritizing first by signer, then by writable
|
2020-04-30 09:05:29 -07:00
|
|
|
accountMetas.sort(function (x, y) {
|
2020-01-07 16:57:56 -08:00
|
|
|
const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1;
|
2020-01-08 12:59:58 -08:00
|
|
|
const checkWritable =
|
|
|
|
x.isWritable === y.isWritable ? 0 : x.isWritable ? -1 : 1;
|
2020-01-07 16:57:56 -08:00
|
|
|
return checkSigner || checkWritable;
|
|
|
|
});
|
|
|
|
|
2020-07-01 19:26:36 -07:00
|
|
|
// Cull duplicate account metas
|
|
|
|
const uniqueMetas: AccountMeta[] = [];
|
|
|
|
accountMetas.forEach(accountMeta => {
|
|
|
|
const pubkeyString = accountMeta.pubkey.toString();
|
|
|
|
const uniqueIndex = uniqueMetas.findIndex(x => {
|
|
|
|
return x.pubkey.toString() === pubkeyString;
|
|
|
|
});
|
|
|
|
if (uniqueIndex > -1) {
|
|
|
|
uniqueMetas[uniqueIndex].isWritable =
|
|
|
|
uniqueMetas[uniqueIndex].isWritable || accountMeta.isWritable;
|
|
|
|
} else {
|
|
|
|
uniqueMetas.push(accountMeta);
|
2020-01-07 16:57:56 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-01 19:26:36 -07:00
|
|
|
this.signatures.forEach(signature => {
|
|
|
|
const sigPubkeyString = signature.publicKey.toString();
|
|
|
|
const uniqueIndex = uniqueMetas.findIndex(x => {
|
|
|
|
return x.pubkey.toString() === sigPubkeyString;
|
|
|
|
});
|
|
|
|
if (uniqueIndex > -1) {
|
|
|
|
uniqueMetas[uniqueIndex].isSigner = true;
|
|
|
|
} else {
|
|
|
|
uniqueMetas.unshift({
|
|
|
|
pubkey: new PublicKey(sigPubkeyString),
|
|
|
|
isSigner: true,
|
|
|
|
isWritable: true,
|
|
|
|
});
|
2019-05-23 09:42:11 -07:00
|
|
|
}
|
2018-10-23 19:13:59 -07:00
|
|
|
});
|
|
|
|
|
2020-07-01 19:26:36 -07:00
|
|
|
// Split out signing from nonsigning keys and count readonlys
|
|
|
|
const signedKeys: string[] = [];
|
|
|
|
const unsignedKeys: string[] = [];
|
|
|
|
uniqueMetas.forEach(({pubkey, isSigner, isWritable}) => {
|
|
|
|
if (isSigner) {
|
|
|
|
// Promote the first signer to writable as it is the fee payer
|
|
|
|
const first = signedKeys.length === 0;
|
|
|
|
signedKeys.push(pubkey.toString());
|
|
|
|
if (!first && !isWritable) {
|
|
|
|
numReadonlySignedAccounts += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unsignedKeys.push(pubkey.toString());
|
|
|
|
if (!isWritable) {
|
|
|
|
numReadonlyUnsignedAccounts += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize signature array, if needed
|
|
|
|
if (this.signatures.length === 0) {
|
|
|
|
const signatures: Array<SignaturePubkeyPair> = [];
|
|
|
|
signedKeys.forEach(pubkey => {
|
|
|
|
signatures.push({signature: null, publicKey: new PublicKey(pubkey)});
|
|
|
|
});
|
|
|
|
this.signatures = signatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountKeys = signedKeys.concat(unsignedKeys);
|
2020-04-30 09:05:29 -07:00
|
|
|
const instructions: CompiledInstruction[] = this.instructions.map(
|
|
|
|
instruction => {
|
|
|
|
const {data, programId} = instruction;
|
|
|
|
return {
|
2020-06-10 22:15:14 -07:00
|
|
|
programIdIndex: accountKeys.indexOf(programId.toString()),
|
2020-04-30 09:05:29 -07:00
|
|
|
accounts: instruction.keys.map(keyObj =>
|
2020-06-10 22:15:14 -07:00
|
|
|
accountKeys.indexOf(keyObj.pubkey.toString()),
|
2019-04-10 12:31:50 -07:00
|
|
|
),
|
2020-04-30 09:05:29 -07:00
|
|
|
data: bs58.encode(data),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
2018-10-23 19:13:59 -07:00
|
|
|
|
|
|
|
instructions.forEach(instruction => {
|
2018-11-18 08:48:14 -08:00
|
|
|
invariant(instruction.programIdIndex >= 0);
|
2020-04-30 09:05:29 -07:00
|
|
|
instruction.accounts.forEach(keyIndex => invariant(keyIndex >= 0));
|
2018-10-23 19:13:59 -07:00
|
|
|
});
|
2018-10-10 10:46:15 -07:00
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
return new Message({
|
|
|
|
header: {
|
|
|
|
numRequiredSignatures: this.signatures.length,
|
|
|
|
numReadonlySignedAccounts,
|
|
|
|
numReadonlyUnsignedAccounts,
|
|
|
|
},
|
2020-06-10 22:15:14 -07:00
|
|
|
accountKeys,
|
2020-04-30 09:05:29 -07:00
|
|
|
recentBlockhash,
|
|
|
|
instructions,
|
2019-01-31 01:16:07 -08:00
|
|
|
});
|
2020-04-30 09:05:29 -07:00
|
|
|
}
|
2018-10-10 10:46:15 -07:00
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
/**
|
|
|
|
* Get a buffer of the Transaction data that need to be covered by signatures
|
|
|
|
*/
|
|
|
|
serializeMessage(): Buffer {
|
|
|
|
return this.compileMessage().serialize();
|
2018-09-13 18:48:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-18 08:48:14 -08:00
|
|
|
* Sign the Transaction with the specified accounts. Multiple signatures may
|
|
|
|
* be applied to a Transaction. The first signature is considered "primary"
|
|
|
|
* and is used when testing for Transaction confirmation.
|
|
|
|
*
|
|
|
|
* Transaction fields should not be modified after the first call to `sign`,
|
|
|
|
* as doing so may invalidate the signature and cause the Transaction to be
|
|
|
|
* rejected.
|
2018-09-13 18:48:51 -07:00
|
|
|
*
|
2019-03-04 08:06:33 -08:00
|
|
|
* The Transaction must be assigned a valid `recentBlockhash` before invoking this method
|
2018-09-13 18:48:51 -07:00
|
|
|
*/
|
2018-11-18 08:48:14 -08:00
|
|
|
sign(...signers: Array<Account>) {
|
2018-11-28 10:06:17 -08:00
|
|
|
this.signPartial(...signers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Partially sign a Transaction with the specified accounts. The `Account`
|
|
|
|
* inputs will be used to sign the Transaction immediately, while any
|
|
|
|
* `PublicKey` inputs will be referenced in the signed Transaction but need to
|
|
|
|
* be filled in later by calling `addSigner()` with the matching `Account`.
|
|
|
|
*
|
|
|
|
* All the caveats from the `sign` method apply to `signPartial`
|
|
|
|
*/
|
|
|
|
signPartial(...partialSigners: Array<PublicKey | Account>) {
|
|
|
|
if (partialSigners.length === 0) {
|
2018-11-18 08:48:14 -08:00
|
|
|
throw new Error('No signers');
|
|
|
|
}
|
2020-07-30 10:48:25 -07:00
|
|
|
|
|
|
|
function partialSignerPublicKey(accountOrPublicKey: any): PublicKey {
|
|
|
|
if ('publicKey' in accountOrPublicKey) {
|
|
|
|
return accountOrPublicKey.publicKey;
|
|
|
|
}
|
|
|
|
return accountOrPublicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
function signerAccount(accountOrPublicKey: any): ?Account {
|
|
|
|
if (
|
|
|
|
'publicKey' in accountOrPublicKey &&
|
|
|
|
'secretKey' in accountOrPublicKey
|
|
|
|
) {
|
|
|
|
return accountOrPublicKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 10:06:17 -08:00
|
|
|
const signatures: Array<SignaturePubkeyPair> = partialSigners.map(
|
2020-07-30 10:48:25 -07:00
|
|
|
accountOrPublicKey => ({
|
|
|
|
signature: null,
|
|
|
|
publicKey: partialSignerPublicKey(accountOrPublicKey),
|
|
|
|
}),
|
2018-11-28 10:06:17 -08:00
|
|
|
);
|
2018-11-18 08:48:14 -08:00
|
|
|
this.signatures = signatures;
|
2020-04-24 09:20:27 -07:00
|
|
|
const signData = this.serializeMessage();
|
2018-11-18 08:48:14 -08:00
|
|
|
|
2018-11-28 10:06:17 -08:00
|
|
|
partialSigners.forEach((accountOrPublicKey, index) => {
|
2020-07-30 10:48:25 -07:00
|
|
|
const account = signerAccount(accountOrPublicKey);
|
|
|
|
if (account) {
|
|
|
|
const signature = nacl.sign.detached(signData, account.secretKey);
|
|
|
|
invariant(signature.length === 64);
|
|
|
|
signatures[index].signature = Buffer.from(signature);
|
2018-11-28 10:06:17 -08:00
|
|
|
}
|
2018-11-18 08:48:14 -08:00
|
|
|
});
|
2018-09-13 18:48:51 -07:00
|
|
|
}
|
|
|
|
|
2018-11-28 10:06:17 -08:00
|
|
|
/**
|
|
|
|
* Fill in a signature for a partially signed Transaction. The `signer` must
|
|
|
|
* be the corresponding `Account` for a `PublicKey` that was previously provided to
|
|
|
|
* `signPartial`
|
|
|
|
*/
|
|
|
|
addSigner(signer: Account) {
|
2020-04-24 09:20:27 -07:00
|
|
|
const signData = this.serializeMessage();
|
2020-04-23 14:44:19 -07:00
|
|
|
const signature = nacl.sign.detached(signData, signer.secretKey);
|
|
|
|
this.addSignature(signer.publicKey, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an externally created signature to a transaction
|
|
|
|
*/
|
|
|
|
addSignature(pubkey: PublicKey, signature: Buffer) {
|
|
|
|
invariant(signature.length === 64);
|
|
|
|
|
2018-11-28 10:06:17 -08:00
|
|
|
const index = this.signatures.findIndex(sigpair =>
|
2020-04-23 14:44:19 -07:00
|
|
|
pubkey.equals(sigpair.publicKey),
|
2018-11-28 10:06:17 -08:00
|
|
|
);
|
|
|
|
if (index < 0) {
|
2020-04-23 14:44:19 -07:00
|
|
|
throw new Error(`Unknown signer: ${pubkey.toString()}`);
|
2018-11-28 10:06:17 -08:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:16:07 -08:00
|
|
|
this.signatures[index].signature = Buffer.from(signature);
|
2018-11-28 10:06:17 -08:00
|
|
|
}
|
|
|
|
|
2020-03-03 12:39:00 -08:00
|
|
|
/**
|
|
|
|
* Verify signatures of a complete, signed Transaction
|
|
|
|
*/
|
|
|
|
verifySignatures(): boolean {
|
2020-08-10 23:35:56 -07:00
|
|
|
return this._verifySignatures(this.serializeMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_verifySignatures(signData: Buffer): boolean {
|
2020-03-03 12:39:00 -08:00
|
|
|
let verified = true;
|
|
|
|
for (const {signature, publicKey} of this.signatures) {
|
|
|
|
if (
|
|
|
|
!nacl.sign.detached.verify(signData, signature, publicKey.toBuffer())
|
|
|
|
) {
|
|
|
|
verified = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return verified;
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:48:51 -07:00
|
|
|
/**
|
|
|
|
* Serialize the Transaction in the wire format.
|
|
|
|
*
|
|
|
|
* The Transaction must have a valid `signature` before invoking this method
|
|
|
|
*/
|
|
|
|
serialize(): Buffer {
|
2018-11-18 08:48:14 -08:00
|
|
|
const {signatures} = this;
|
2020-08-10 23:35:56 -07:00
|
|
|
if (!signatures || signatures.length === 0) {
|
2018-09-13 18:48:51 -07:00
|
|
|
throw new Error('Transaction has not been signed');
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:20:27 -07:00
|
|
|
const signData = this.serializeMessage();
|
2020-08-10 23:35:56 -07:00
|
|
|
if (!this._verifySignatures(signData)) {
|
|
|
|
throw new Error('Transaction has not been signed correctly');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._serialize(signData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_serialize(signData: Buffer): Buffer {
|
|
|
|
const {signatures} = this;
|
2019-01-31 01:16:07 -08:00
|
|
|
const signatureCount = [];
|
|
|
|
shortvec.encodeLength(signatureCount, signatures.length);
|
|
|
|
const transactionLength =
|
|
|
|
signatureCount.length + signatures.length * 64 + signData.length;
|
2019-02-02 10:00:20 -08:00
|
|
|
const wireTransaction = Buffer.alloc(transactionLength);
|
2018-11-18 08:48:14 -08:00
|
|
|
invariant(signatures.length < 256);
|
2019-02-02 10:00:20 -08:00
|
|
|
Buffer.from(signatureCount).copy(wireTransaction, 0);
|
2018-11-18 08:48:14 -08:00
|
|
|
signatures.forEach(({signature}, index) => {
|
2019-08-30 16:22:11 -07:00
|
|
|
if (signature !== null) {
|
|
|
|
invariant(signature.length === 64, `signature has invalid length`);
|
|
|
|
Buffer.from(signature).copy(
|
|
|
|
wireTransaction,
|
|
|
|
signatureCount.length + index * 64,
|
|
|
|
);
|
|
|
|
}
|
2018-11-18 08:48:14 -08:00
|
|
|
});
|
2019-01-31 01:16:07 -08:00
|
|
|
signData.copy(
|
|
|
|
wireTransaction,
|
2019-02-02 10:00:20 -08:00
|
|
|
signatureCount.length + signatures.length * 64,
|
2019-01-31 01:16:07 -08:00
|
|
|
);
|
2018-11-18 08:48:14 -08:00
|
|
|
invariant(
|
2018-11-27 18:05:23 -08:00
|
|
|
wireTransaction.length <= PACKET_DATA_SIZE,
|
|
|
|
`Transaction too large: ${wireTransaction.length} > ${PACKET_DATA_SIZE}`,
|
2018-11-16 19:28:57 -08:00
|
|
|
);
|
2018-09-13 18:48:51 -07:00
|
|
|
return wireTransaction;
|
|
|
|
}
|
2018-10-23 15:17:43 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated method
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
get keys(): Array<PublicKey> {
|
2018-11-18 08:48:14 -08:00
|
|
|
invariant(this.instructions.length === 1);
|
2019-04-10 12:31:50 -07:00
|
|
|
return this.instructions[0].keys.map(keyObj => keyObj.pubkey);
|
2018-10-23 15:17:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated method
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
get programId(): PublicKey {
|
2018-11-18 08:48:14 -08:00
|
|
|
invariant(this.instructions.length === 1);
|
2018-10-23 15:17:43 -07:00
|
|
|
return this.instructions[0].programId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated method
|
|
|
|
* @private
|
|
|
|
*/
|
2019-03-14 13:27:47 -07:00
|
|
|
get data(): Buffer {
|
2018-11-18 08:48:14 -08:00
|
|
|
invariant(this.instructions.length === 1);
|
2019-03-14 13:27:47 -07:00
|
|
|
return this.instructions[0].data;
|
2018-10-23 15:17:43 -07:00
|
|
|
}
|
2019-01-31 01:16:07 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a wire transaction into a Transaction object.
|
|
|
|
*/
|
2020-02-12 16:25:22 -08:00
|
|
|
static from(buffer: Buffer | Uint8Array | Array<number>): Transaction {
|
2019-01-31 01:16:07 -08:00
|
|
|
// Slice up wire data
|
|
|
|
let byteArray = [...buffer];
|
|
|
|
|
|
|
|
const signatureCount = shortvec.decodeLength(byteArray);
|
|
|
|
let signatures = [];
|
|
|
|
for (let i = 0; i < signatureCount; i++) {
|
|
|
|
const signature = byteArray.slice(0, SIGNATURE_LENGTH);
|
|
|
|
byteArray = byteArray.slice(SIGNATURE_LENGTH);
|
2020-01-13 15:17:32 -08:00
|
|
|
signatures.push(bs58.encode(Buffer.from(signature)));
|
2019-01-31 01:16:07 -08:00
|
|
|
}
|
|
|
|
|
2019-05-23 17:24:38 -07:00
|
|
|
const numRequiredSignatures = byteArray.shift();
|
2019-11-06 09:42:01 -08:00
|
|
|
const numReadonlySignedAccounts = byteArray.shift();
|
|
|
|
const numReadonlyUnsignedAccounts = byteArray.shift();
|
2019-04-10 12:31:50 -07:00
|
|
|
|
2019-01-31 01:16:07 -08:00
|
|
|
const accountCount = shortvec.decodeLength(byteArray);
|
2020-06-10 22:15:14 -07:00
|
|
|
let accountKeys = [];
|
2019-01-31 01:16:07 -08:00
|
|
|
for (let i = 0; i < accountCount; i++) {
|
|
|
|
const account = byteArray.slice(0, PUBKEY_LENGTH);
|
|
|
|
byteArray = byteArray.slice(PUBKEY_LENGTH);
|
2020-06-10 22:15:14 -07:00
|
|
|
accountKeys.push(bs58.encode(Buffer.from(account)));
|
2019-01-31 01:16:07 -08:00
|
|
|
}
|
|
|
|
|
2019-03-04 08:06:33 -08:00
|
|
|
const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
|
2019-01-31 01:16:07 -08:00
|
|
|
byteArray = byteArray.slice(PUBKEY_LENGTH);
|
|
|
|
|
|
|
|
const instructionCount = shortvec.decodeLength(byteArray);
|
|
|
|
let instructions = [];
|
|
|
|
for (let i = 0; i < instructionCount; i++) {
|
|
|
|
let instruction = {};
|
2020-04-01 12:11:10 -07:00
|
|
|
instruction.programIdIndex = byteArray.shift();
|
2019-11-16 08:28:14 -08:00
|
|
|
const accountCount = shortvec.decodeLength(byteArray);
|
|
|
|
instruction.accounts = byteArray.slice(0, accountCount);
|
|
|
|
byteArray = byteArray.slice(accountCount);
|
2019-03-14 13:27:47 -07:00
|
|
|
const dataLength = shortvec.decodeLength(byteArray);
|
2020-01-13 15:17:32 -08:00
|
|
|
const data = byteArray.slice(0, dataLength);
|
|
|
|
instruction.data = bs58.encode(Buffer.from(data));
|
2019-03-14 13:27:47 -07:00
|
|
|
byteArray = byteArray.slice(dataLength);
|
2019-01-31 01:16:07 -08:00
|
|
|
instructions.push(instruction);
|
|
|
|
}
|
|
|
|
|
2020-06-10 22:15:14 -07:00
|
|
|
const messageArgs = {
|
2020-04-30 09:05:29 -07:00
|
|
|
header: {
|
|
|
|
numRequiredSignatures,
|
|
|
|
numReadonlySignedAccounts,
|
|
|
|
numReadonlyUnsignedAccounts,
|
|
|
|
},
|
|
|
|
recentBlockhash: bs58.encode(Buffer.from(recentBlockhash)),
|
2020-06-10 22:15:14 -07:00
|
|
|
accountKeys,
|
2019-11-16 08:28:14 -08:00
|
|
|
instructions,
|
2020-04-30 09:05:29 -07:00
|
|
|
};
|
|
|
|
|
2020-06-10 22:15:14 -07:00
|
|
|
return Transaction.populate(new Message(messageArgs), signatures);
|
2019-11-16 08:28:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-10 22:15:14 -07:00
|
|
|
* Populate Transaction object from message and signatures
|
2019-11-16 08:28:14 -08:00
|
|
|
*/
|
2020-06-10 22:15:14 -07:00
|
|
|
static populate(message: Message, signatures: Array<string>): Transaction {
|
2019-11-16 08:28:14 -08:00
|
|
|
const transaction = new Transaction();
|
2020-04-30 09:05:29 -07:00
|
|
|
transaction.recentBlockhash = message.recentBlockhash;
|
|
|
|
signatures.forEach((signature, index) => {
|
2019-01-31 01:16:07 -08:00
|
|
|
const sigPubkeyPair = {
|
2019-08-30 16:22:11 -07:00
|
|
|
signature:
|
2020-04-30 09:05:29 -07:00
|
|
|
signature == bs58.encode(DEFAULT_SIGNATURE)
|
2019-08-30 16:22:11 -07:00
|
|
|
? null
|
2020-04-30 09:05:29 -07:00
|
|
|
: bs58.decode(signature),
|
|
|
|
publicKey: message.accountKeys[index],
|
2019-01-31 01:16:07 -08:00
|
|
|
};
|
|
|
|
transaction.signatures.push(sigPubkeyPair);
|
2020-04-30 09:05:29 -07:00
|
|
|
});
|
2019-05-23 17:24:38 -07:00
|
|
|
|
2020-04-30 09:05:29 -07:00
|
|
|
message.instructions.forEach(instruction => {
|
|
|
|
const keys = instruction.accounts.map(account => {
|
|
|
|
const pubkey = message.accountKeys[account];
|
|
|
|
return {
|
2019-04-10 12:31:50 -07:00
|
|
|
pubkey,
|
|
|
|
isSigner: transaction.signatures.some(
|
|
|
|
keyObj => keyObj.publicKey.toString() === pubkey.toString(),
|
|
|
|
),
|
2020-04-30 09:05:29 -07:00
|
|
|
isWritable: message.isAccountWritable(account),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
transaction.instructions.push(
|
|
|
|
new TransactionInstruction({
|
|
|
|
keys,
|
|
|
|
programId: message.accountKeys[instruction.programIdIndex],
|
|
|
|
data: bs58.decode(instruction.data),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-01-31 01:16:07 -08:00
|
|
|
return transaction;
|
|
|
|
}
|
2018-09-14 08:27:40 -07:00
|
|
|
}
|