Skip to content

Commit 93d13a9

Browse files
committed
fix: update to post-v0.9 Transaction wire format
1 parent 8cddfd7 commit 93d13a9

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

src/transaction.js

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,71 @@ export class Transaction {
8585
if (!lastId) {
8686
throw new Error('Transaction lastId required');
8787
}
88+
const programIds = [programId];
89+
const instructions = [
90+
{
91+
programId: 0,
92+
accountsLength: keys.length,
93+
accounts: [...keys.keys()],
94+
userdataLength: userdata.length,
95+
userdata,
96+
},
97+
];
98+
99+
const instructionLayout = BufferLayout.struct([
100+
BufferLayout.u8('programId'),
101+
102+
BufferLayout.u32('accountsLength'),
103+
BufferLayout.u32('accountsLengthPadding'),
104+
BufferLayout.seq(
105+
BufferLayout.u8('account'),
106+
BufferLayout.offset(BufferLayout.u32(), -8),
107+
'accounts'
108+
),
109+
BufferLayout.ns64('userdataLength'),
110+
BufferLayout.blob(userdata.length, 'userdata'),
111+
]);
88112

89113
const signDataLayout = BufferLayout.struct([
90-
BufferLayout.ns64('keysLength'),
114+
BufferLayout.u32('accountKeysLength'),
115+
BufferLayout.u32('accountKeysLengthPadding'),
91116
BufferLayout.seq(
92-
Layout.publicKey('key'),
93-
keys.length,
94-
'keys'
117+
Layout.publicKey('accountKey'),
118+
BufferLayout.offset(BufferLayout.u32(), -8),
119+
'accountKeys'
95120
),
96-
Layout.publicKey('programId'),
97121
Layout.publicKey('lastId'),
98122
BufferLayout.ns64('fee'),
99-
BufferLayout.ns64('userdataLength'),
100-
BufferLayout.blob(userdata.length, 'userdata'),
123+
124+
BufferLayout.u32('programIdsLength'),
125+
BufferLayout.u32('programIdsLengthPadding'),
126+
BufferLayout.seq(
127+
Layout.publicKey('programId'),
128+
BufferLayout.offset(BufferLayout.u32(), -8),
129+
'programIds'
130+
),
131+
132+
BufferLayout.u32('instructionsLength'),
133+
BufferLayout.u32('instructionsLengthPadding'),
134+
BufferLayout.seq(
135+
instructionLayout,
136+
BufferLayout.offset(BufferLayout.u32(), -8),
137+
'instructions'
138+
),
101139
]);
102140

103-
let signData = Buffer.alloc(2048);
104-
let length = signDataLayout.encode(
105-
{
106-
keysLength: keys.length,
107-
keys: keys.map((key) => key.toBuffer()),
108-
programId: programId.toBuffer(),
109-
lastId: Buffer.from(bs58.decode(lastId)),
110-
fee: 0,
111-
userdataLength: userdata.length,
112-
userdata,
113-
},
114-
signData
115-
);
141+
const transaction = {
142+
accountKeys: keys.map((key) => key.toBuffer()),
143+
lastId: Buffer.from(bs58.decode(lastId)),
144+
fee: 0,
145+
programIds: programIds.map((programId) => programId.toBuffer()),
146+
instructions,
147+
};
116148

117-
if (userdata.length === 0) {
118-
// If userdata is empty, strip the 64bit 'userdataLength' field from
119-
// the end of signData
120-
length -= 8;
121-
}
149+
let signData = Buffer.alloc(2048);
150+
const length = signDataLayout.encode(transaction, signData);
122151
signData = signData.slice(0, length);
152+
123153
return signData;
124154
}
125155

0 commit comments

Comments
 (0)