fix: update to post-v0.9 Transaction wire format
This commit is contained in:
@ -85,41 +85,71 @@ export class Transaction {
|
|||||||
if (!lastId) {
|
if (!lastId) {
|
||||||
throw new Error('Transaction lastId required');
|
throw new Error('Transaction lastId required');
|
||||||
}
|
}
|
||||||
|
const programIds = [programId];
|
||||||
|
const instructions = [
|
||||||
|
{
|
||||||
|
programId: 0,
|
||||||
|
accountsLength: keys.length,
|
||||||
|
accounts: [...keys.keys()],
|
||||||
|
userdataLength: userdata.length,
|
||||||
|
userdata,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
const signDataLayout = BufferLayout.struct([
|
const instructionLayout = BufferLayout.struct([
|
||||||
BufferLayout.ns64('keysLength'),
|
BufferLayout.u8('programId'),
|
||||||
|
|
||||||
|
BufferLayout.u32('accountsLength'),
|
||||||
|
BufferLayout.u32('accountsLengthPadding'),
|
||||||
BufferLayout.seq(
|
BufferLayout.seq(
|
||||||
Layout.publicKey('key'),
|
BufferLayout.u8('account'),
|
||||||
keys.length,
|
BufferLayout.offset(BufferLayout.u32(), -8),
|
||||||
'keys'
|
'accounts'
|
||||||
),
|
),
|
||||||
Layout.publicKey('programId'),
|
|
||||||
Layout.publicKey('lastId'),
|
|
||||||
BufferLayout.ns64('fee'),
|
|
||||||
BufferLayout.ns64('userdataLength'),
|
BufferLayout.ns64('userdataLength'),
|
||||||
BufferLayout.blob(userdata.length, 'userdata'),
|
BufferLayout.blob(userdata.length, 'userdata'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
let signData = Buffer.alloc(2048);
|
const signDataLayout = BufferLayout.struct([
|
||||||
let length = signDataLayout.encode(
|
BufferLayout.u32('accountKeysLength'),
|
||||||
{
|
BufferLayout.u32('accountKeysLengthPadding'),
|
||||||
keysLength: keys.length,
|
BufferLayout.seq(
|
||||||
keys: keys.map((key) => key.toBuffer()),
|
Layout.publicKey('accountKey'),
|
||||||
programId: programId.toBuffer(),
|
BufferLayout.offset(BufferLayout.u32(), -8),
|
||||||
|
'accountKeys'
|
||||||
|
),
|
||||||
|
Layout.publicKey('lastId'),
|
||||||
|
BufferLayout.ns64('fee'),
|
||||||
|
|
||||||
|
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'
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const transaction = {
|
||||||
|
accountKeys: keys.map((key) => key.toBuffer()),
|
||||||
lastId: Buffer.from(bs58.decode(lastId)),
|
lastId: Buffer.from(bs58.decode(lastId)),
|
||||||
fee: 0,
|
fee: 0,
|
||||||
userdataLength: userdata.length,
|
programIds: programIds.map((programId) => programId.toBuffer()),
|
||||||
userdata,
|
instructions,
|
||||||
},
|
};
|
||||||
signData
|
|
||||||
);
|
|
||||||
|
|
||||||
if (userdata.length === 0) {
|
let signData = Buffer.alloc(2048);
|
||||||
// If userdata is empty, strip the 64bit 'userdataLength' field from
|
const length = signDataLayout.encode(transaction, signData);
|
||||||
// the end of signData
|
|
||||||
length -= 8;
|
|
||||||
}
|
|
||||||
signData = signData.slice(0, length);
|
signData = signData.slice(0, length);
|
||||||
|
|
||||||
return signData;
|
return signData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user