Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
fix: update to post-v0.9 Transaction wire format
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Oct 10, 2018
1 parent 8cddfd7 commit 93d13a9
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,71 @@ export class Transaction {
if (!lastId) {
throw new Error('Transaction lastId required');
}
const programIds = [programId];
const instructions = [
{
programId: 0,
accountsLength: keys.length,
accounts: [...keys.keys()],
userdataLength: userdata.length,
userdata,
},
];

const instructionLayout = BufferLayout.struct([
BufferLayout.u8('programId'),

BufferLayout.u32('accountsLength'),
BufferLayout.u32('accountsLengthPadding'),
BufferLayout.seq(
BufferLayout.u8('account'),
BufferLayout.offset(BufferLayout.u32(), -8),
'accounts'
),
BufferLayout.ns64('userdataLength'),
BufferLayout.blob(userdata.length, 'userdata'),
]);

const signDataLayout = BufferLayout.struct([
BufferLayout.ns64('keysLength'),
BufferLayout.u32('accountKeysLength'),
BufferLayout.u32('accountKeysLengthPadding'),
BufferLayout.seq(
Layout.publicKey('key'),
keys.length,
'keys'
Layout.publicKey('accountKey'),
BufferLayout.offset(BufferLayout.u32(), -8),
'accountKeys'
),
Layout.publicKey('programId'),
Layout.publicKey('lastId'),
BufferLayout.ns64('fee'),
BufferLayout.ns64('userdataLength'),
BufferLayout.blob(userdata.length, 'userdata'),

BufferLayout.u32('programIdsLength'),
BufferLayout.u32('programIdsLengthPadding'),
BufferLayout.seq(
Layout.publicKey('programId'),
BufferLayout.offset(BufferLayout.u32(), -8),
'programIds'
),

BufferLayout.u32('instructionsLength'),
BufferLayout.u32('instructionsLengthPadding'),
BufferLayout.seq(
instructionLayout,
BufferLayout.offset(BufferLayout.u32(), -8),
'instructions'
),
]);

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
);
const transaction = {
accountKeys: keys.map((key) => key.toBuffer()),
lastId: Buffer.from(bs58.decode(lastId)),
fee: 0,
programIds: programIds.map((programId) => programId.toBuffer()),
instructions,
};

if (userdata.length === 0) {
// If userdata is empty, strip the 64bit 'userdataLength' field from
// the end of signData
length -= 8;
}
let signData = Buffer.alloc(2048);
const length = signDataLayout.encode(transaction, signData);
signData = signData.slice(0, length);

return signData;
}

Expand Down

0 comments on commit 93d13a9

Please sign in to comment.