fix: support multiple Transaction signatures

This commit is contained in:
Michael Vines
2018-11-18 08:48:14 -08:00
parent 5311ed7f68
commit fa7e2722d1
13 changed files with 101 additions and 64 deletions

View File

@@ -41,7 +41,7 @@ export class BpfLoader {
elf.length,
BpfLoader.programId,
);
await sendAndConfirmTransaction(connection, owner, transaction);
await sendAndConfirmTransaction(connection, transaction, owner);
const loader = new Loader(connection, BpfLoader.programId);
await loader.load(programAccount, elf);

View File

@@ -368,8 +368,8 @@ export class Connection {
* Sign and send a transaction
*/
async sendTransaction(
from: Account,
transaction: Transaction,
...signers: Array<Account>
): Promise<TransactionSignature> {
for (;;) {
// Attempt to use the previous last id for up to 1 second
@@ -379,7 +379,7 @@ export class Connection {
this._lastIdInfo.seconds === seconds
) {
transaction.lastId = this._lastIdInfo.lastId;
transaction.sign(from);
transaction.sign(...signers);
if (!transaction.signature) {
throw new Error('!signature'); // should never happen
}

View File

@@ -72,7 +72,7 @@ export class Loader {
userdata,
});
transactions.push(
sendAndConfirmTransaction(this.connection, program, transaction),
sendAndConfirmTransaction(this.connection, transaction, program),
);
// Run up to 8 Loads in parallel to prevent too many parallel transactions from
@@ -116,6 +116,6 @@ export class Loader {
userdata,
});
transaction.add(SystemProgram.spawn(program.publicKey));
await sendAndConfirmTransaction(this.connection, program, transaction);
await sendAndConfirmTransaction(this.connection, transaction, program);
}
}

View File

@@ -44,7 +44,7 @@ export class NativeLoader {
bytes.length + 1,
NativeLoader.programId,
);
await sendAndConfirmTransaction(connection, owner, transaction);
await sendAndConfirmTransaction(connection, transaction, owner);
const loader = new Loader(connection, NativeLoader.programId);
await loader.load(programAccount, bytes);

View File

@@ -233,14 +233,14 @@ export class Token {
1 + userdata.length,
programId,
);
await sendAndConfirmTransaction(connection, owner, transaction);
await sendAndConfirmTransaction(connection, transaction, owner);
transaction = new Transaction().add({
keys: [tokenAccount.publicKey, initialAccountPublicKey],
programId,
userdata,
});
await sendAndConfirmTransaction(connection, tokenAccount, transaction);
await sendAndConfirmTransaction(connection, transaction, tokenAccount);
return [token, initialAccountPublicKey];
}
@@ -282,7 +282,7 @@ export class Token {
1 + TokenAccountInfoLayout.span,
this.programId,
);
await sendAndConfirmTransaction(this.connection, owner, transaction);
await sendAndConfirmTransaction(this.connection, transaction, owner);
// Initialize the token account
const keys = [tokenAccount.publicKey, owner.publicKey, this.token];
@@ -295,7 +295,7 @@ export class Token {
userdata,
});
await sendAndConfirmTransaction(this.connection, tokenAccount, transaction);
await sendAndConfirmTransaction(this.connection, transaction, tokenAccount);
return tokenAccount.publicKey;
}
@@ -377,7 +377,6 @@ export class Token {
): Promise<?TransactionSignature> {
return await sendAndConfirmTransaction(
this.connection,
owner,
new Transaction().add(
await this.transferInstruction(
owner.publicKey,
@@ -386,6 +385,7 @@ export class Token {
amount,
),
),
owner,
);
}
@@ -405,10 +405,10 @@ export class Token {
): Promise<void> {
await sendAndConfirmTransaction(
this.connection,
owner,
new Transaction().add(
this.approveInstruction(owner.publicKey, account, delegate, amount),
),
owner,
);
}
@@ -441,10 +441,10 @@ export class Token {
): Promise<void> {
await sendAndConfirmTransaction(
this.connection,
owner,
new Transaction().add(
this.setOwnerInstruction(owner.publicKey, account, newOwner),
),
owner,
);
}

View File

@@ -1,6 +1,6 @@
// @flow
import assert from 'assert';
import invariant from 'assert';
import * as BufferLayout from 'buffer-layout';
import nacl from 'tweetnacl';
import bs58 from 'bs58';
@@ -61,25 +61,39 @@ export class TransactionInstruction {
* List of Transaction object fields that may be initialized at construction
*
* @typedef {Object} TransactionCtorFields
* @property {?Buffer} signature
* @property {?Array<PublicKey>} keys
* @property {?PublicKey} programId
* @property {?number} fee
* @property {?Buffer} userdata
*/
type TransactionCtorFields = {|
fee?: number,
|};
/**
* @private
*/
type SignaturePubkeyPair = {|
signature: Buffer | null,
publicKey: PublicKey,
|};
/**
* Transaction class
*/
export class Transaction {
/**
* Current signature of the transaction. Typically created by invoking the
* `sign()` method
* Signatures for the transaction. Typically created by invoking the
* `sign()` method one or more times.
*/
signature: ?Buffer;
signatures: Array<SignaturePubkeyPair> = [];
/**
* The first (primary) Transaction signature
*/
get signature(): Buffer | null {
if (this.signatures.length > 0) {
return this.signatures[0].signature;
}
return null;
}
/**
* The instructions to atomically execute
@@ -128,7 +142,7 @@ export class Transaction {
throw new Error('No instructions provided');
}
const keys = [];
const keys = this.signatures.map(({publicKey}) => publicKey.toString());
const programIds = [];
this.instructions.forEach(instruction => {
const programId = instruction.programId.toString();
@@ -153,8 +167,8 @@ export class Transaction {
});
instructions.forEach(instruction => {
assert(instruction.programIdIndex >= 0);
instruction.keyIndices.forEach(keyIndex => assert(keyIndex >= 0));
invariant(instruction.programIdIndex >= 0);
instruction.keyIndices.forEach(keyIndex => invariant(keyIndex >= 0));
});
const instructionLayout = BufferLayout.struct([
@@ -222,14 +236,34 @@ export class Transaction {
}
/**
* Sign the Transaction with the specified account
* 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.
*
* The Transaction must be assigned a valid `lastId` before invoking this method
*/
sign(from: Account) {
sign(...signers: Array<Account>) {
if (signers.length === 0) {
throw new Error('No signers');
}
const signatures: Array<SignaturePubkeyPair> = signers.map(account => {
return {
signature: null,
publicKey: account.publicKey,
};
});
this.signatures = signatures;
const signData = this._getSignData();
this.signature = nacl.sign.detached(signData, from.secretKey);
assert(this.signature.length === 64);
signers.forEach((account, index) => {
const signature = nacl.sign.detached(signData, account.secretKey);
invariant(signature.length === 64);
signatures[index].signature = signature;
});
}
/**
@@ -238,19 +272,27 @@ export class Transaction {
* The Transaction must have a valid `signature` before invoking this method
*/
serialize(): Buffer {
const {signature} = this;
if (!signature) {
const {signatures} = this;
if (!signatures) {
throw new Error('Transaction has not been signed');
}
const signData = this._getSignData();
const wireTransaction = Buffer.alloc(
8 + signature.length + signData.length,
8 + signatures.length * 64 + signData.length,
);
invariant(signatures.length < 256);
wireTransaction.writeUInt8(signatures.length, 0);
signatures.forEach(({signature}, index) => {
invariant(signature !== null, `null signature`);
invariant(signature.length === 64, `signature has invalid length`);
Buffer.from(signature).copy(wireTransaction, 8 + index * 64);
});
signData.copy(wireTransaction, 8 + signatures.length * 64);
invariant(
wireTransaction.length < 512,
`${wireTransaction.length}, ${signatures.length}`,
);
wireTransaction.writeUInt8(1, 0); // TODO: Support multiple transaction signatures
Buffer.from(signature).copy(wireTransaction, 8);
signData.copy(wireTransaction, 8 + signature.length);
return wireTransaction;
}
@@ -259,7 +301,7 @@ export class Transaction {
* @private
*/
get keys(): Array<PublicKey> {
assert(this.instructions.length === 1);
invariant(this.instructions.length === 1);
return this.instructions[0].keys;
}
@@ -268,7 +310,7 @@ export class Transaction {
* @private
*/
get programId(): PublicKey {
assert(this.instructions.length === 1);
invariant(this.instructions.length === 1);
return this.instructions[0].programId;
}
@@ -277,7 +319,7 @@ export class Transaction {
* @private
*/
get userdata(): Buffer {
assert(this.instructions.length === 1);
invariant(this.instructions.length === 1);
return this.instructions[0].userdata;
}
}

View File

@@ -11,15 +11,14 @@ import type {TransactionSignature} from '../transaction';
*/
export async function sendAndConfirmTransaction(
connection: Connection,
from: Account,
transaction: Transaction,
runtimeErrorOk: boolean = false,
...signers: Array<Account>
): Promise<?TransactionSignature> {
let sendRetries = 10;
let signature;
for (;;) {
const start = Date.now();
signature = await connection.sendTransaction(from, transaction);
signature = await connection.sendTransaction(transaction, ...signers);
// Wait up to a couple seconds for a confirmation
let status = 'SignatureNotFound';
@@ -41,10 +40,7 @@ export async function sendAndConfirmTransaction(
}
}
if (
status === 'Confirmed' ||
(status === 'ProgramRuntimeError' && runtimeErrorOk)
) {
if (status === 'Confirmed') {
break;
}