refactor: use buffer-layout to clean up buffer encoding

This commit is contained in:
Michael Vines
2018-10-06 11:13:58 -07:00
parent a596e99b4a
commit 17b73306fe
7 changed files with 137 additions and 85 deletions

View File

@@ -1,9 +1,11 @@
// @flow
import assert from 'assert';
import * as BufferLayout from 'buffer-layout';
import nacl from 'tweetnacl';
import bs58 from 'bs58';
import * as Layout from './layout';
import type {Account} from './account';
import type {PublicKey} from './publickey';
@@ -54,7 +56,7 @@ export class Transaction {
/**
* Program Id to execute
*/
programId: ?PublicKey;
programId: PublicKey;
/**
* A recent transaction id. Must be populated by the caller
@@ -79,53 +81,46 @@ export class Transaction {
* @private
*/
_getSignData(): Buffer {
const {lastId} = this;
const {lastId, keys, programId, userdata} = this;
if (!lastId) {
throw new Error('Transaction lastId required');
}
// Start with a Buffer that should be large enough to fit any Transaction
const transactionData = Buffer.alloc(2048);
const signDataLayout = BufferLayout.struct([
BufferLayout.ns64('keysLength'),
BufferLayout.seq(
Layout.publicKey('key'),
keys.length,
'keys'
),
Layout.publicKey('programId'),
Layout.publicKey('lastId'),
BufferLayout.ns64('fee'),
BufferLayout.ns64('userdataLength'),
BufferLayout.blob(userdata.length, 'userdata'),
]);
let pos = 0;
let signData = Buffer.alloc(2048);
let length = signDataLayout.encode(
{
keysLength: keys.length,
keys: keys.map((key) => key.toBuffer()),
programId: programId.toBuffer(),
lastId: Buffer.from(bs58.decode(lastId)),
fee: 0,
userdataLength: userdata.length,
userdata,
},
signData
);
// serialize `this.keys`
transactionData.writeUInt32LE(this.keys.length, pos); // u64
pos += 8;
for (let key of this.keys) {
const keyBytes = key.toBuffer();
keyBytes.copy(transactionData, pos);
pos += 32;
if (userdata.length === 0) {
// If userdata is empty, strip the 64bit 'userdataLength' field from
// the end of signData
length -= 8;
}
// serialize `this.programId`
if (this.programId) {
const keyBytes = this.programId.toBuffer();
keyBytes.copy(transactionData, pos);
}
pos += 32;
// serialize `this.lastId`
{
const lastIdBytes = Buffer.from(bs58.decode(lastId));
assert(lastIdBytes.length === 32);
lastIdBytes.copy(transactionData, pos);
pos += 32;
}
// serialize `this.fee`
transactionData.writeUInt32LE(this.fee, pos); // u64
pos += 8;
// serialize `this.userdata`
if (this.userdata.length > 0) {
transactionData.writeUInt32LE(this.userdata.length, pos); // u64
pos += 8;
this.userdata.copy(transactionData, pos);
pos += this.userdata.length;
}
return transactionData.slice(0, pos);
signData = signData.slice(0, length);
return signData;
}
/**
@@ -134,8 +129,8 @@ export class Transaction {
* The Transaction must be assigned a valid `lastId` before invoking this method
*/
sign(from: Account) {
const transactionData = this._getSignData();
this.signature = nacl.sign.detached(transactionData, from.secretKey);
const signData = this._getSignData();
this.signature = nacl.sign.detached(signData, from.secretKey);
assert(this.signature.length === 64);
}
@@ -150,13 +145,13 @@ export class Transaction {
throw new Error('Transaction has not been signed');
}
const transactionData = this._getSignData();
const signData = this._getSignData();
const wireTransaction = Buffer.alloc(
signature.length + transactionData.length
signature.length + signData.length
);
Buffer.from(signature).copy(wireTransaction, 0);
transactionData.copy(wireTransaction, signature.length);
signData.copy(wireTransaction, signature.length);
return wireTransaction;
}
}