fix: Update transaction format; include numCreditOnlySignedAccounts and numCreditOnlyUnsignedAccounts bytes, remove programIds

This commit is contained in:
Tyera Eulberg
2019-05-23 10:42:11 -06:00
committed by Michael Vines
parent 53958a8b1e
commit 34cb08bd84
2 changed files with 117 additions and 128 deletions

View File

@ -153,14 +153,10 @@ export class Transaction {
const keys = this.signatures.map(({publicKey}) => publicKey.toString()); const keys = this.signatures.map(({publicKey}) => publicKey.toString());
let numRequiredSignatures = 0; let numRequiredSignatures = 0;
let numCreditOnlySignedAccounts = 0;
let numCreditOnlyUnsignedAccounts = 0;
const programIds = [];
this.instructions.forEach(instruction => { this.instructions.forEach(instruction => {
const programId = instruction.programId.toString();
if (!programIds.includes(programId)) {
programIds.push(programId);
}
instruction.keys.forEach(keySignerPair => { instruction.keys.forEach(keySignerPair => {
const keyStr = keySignerPair.pubkey.toString(); const keyStr = keySignerPair.pubkey.toString();
if (!keys.includes(keyStr)) { if (!keys.includes(keyStr)) {
@ -170,6 +166,12 @@ export class Transaction {
keys.push(keyStr); keys.push(keyStr);
} }
}); });
const programId = instruction.programId.toString();
if (!keys.includes(programId)) {
keys.push(programId);
numCreditOnlyUnsignedAccounts += 1;
}
}); });
if (numRequiredSignatures > this.signatures.length) { if (numRequiredSignatures > this.signatures.length) {
@ -183,9 +185,6 @@ export class Transaction {
let keyCount = []; let keyCount = [];
shortvec.encodeLength(keyCount, keys.length); shortvec.encodeLength(keyCount, keys.length);
let programIdCount = [];
shortvec.encodeLength(programIdCount, programIds.length);
const instructions = this.instructions.map(instruction => { const instructions = this.instructions.map(instruction => {
const {data, programId} = instruction; const {data, programId} = instruction;
let keyIndicesCount = []; let keyIndicesCount = [];
@ -193,7 +192,7 @@ export class Transaction {
let dataCount = []; let dataCount = [];
shortvec.encodeLength(dataCount, instruction.data.length); shortvec.encodeLength(dataCount, instruction.data.length);
return { return {
programIdIndex: programIds.indexOf(programId.toString()), programIdIndex: keys.indexOf(programId.toString()),
keyIndicesCount: Buffer.from(keyIndicesCount), keyIndicesCount: Buffer.from(keyIndicesCount),
keyIndices: Buffer.from( keyIndices: Buffer.from(
instruction.keys.map(keyObj => instruction.keys.map(keyObj =>
@ -247,27 +246,22 @@ export class Transaction {
const signDataLayout = BufferLayout.struct([ const signDataLayout = BufferLayout.struct([
BufferLayout.blob(1, 'numRequiredSignatures'), BufferLayout.blob(1, 'numRequiredSignatures'),
BufferLayout.blob(1, 'numCreditOnlySignedAccounts'),
BufferLayout.blob(1, 'numCreditOnlyUnsignedAccounts'),
BufferLayout.blob(keyCount.length, 'keyCount'), BufferLayout.blob(keyCount.length, 'keyCount'),
BufferLayout.seq(Layout.publicKey('key'), keys.length, 'keys'), BufferLayout.seq(Layout.publicKey('key'), keys.length, 'keys'),
Layout.publicKey('recentBlockhash'), Layout.publicKey('recentBlockhash'),
BufferLayout.blob(programIdCount.length, 'programIdCount'),
BufferLayout.seq(
Layout.publicKey('programId'),
programIds.length,
'programIds',
),
]); ]);
const transaction = { const transaction = {
numRequiredSignatures: Buffer.from([this.signatures.length]), numRequiredSignatures: Buffer.from([this.signatures.length]),
numCreditOnlySignedAccounts: Buffer.from([numCreditOnlySignedAccounts]),
numCreditOnlyUnsignedAccounts: Buffer.from([
numCreditOnlyUnsignedAccounts,
]),
keyCount: Buffer.from(keyCount), keyCount: Buffer.from(keyCount),
keys: keys.map(key => new PublicKey(key).toBuffer()), keys: keys.map(key => new PublicKey(key).toBuffer()),
recentBlockhash: Buffer.from(bs58.decode(recentBlockhash)), recentBlockhash: Buffer.from(bs58.decode(recentBlockhash)),
programIdCount: Buffer.from(programIdCount),
programIds: programIds.map(programId =>
new PublicKey(programId).toBuffer(),
),
}; };
let signData = Buffer.alloc(2048); let signData = Buffer.alloc(2048);
@ -438,6 +432,8 @@ export class Transaction {
} }
byteArray = byteArray.slice(1); // Skip numRequiredSignatures byte byteArray = byteArray.slice(1); // Skip numRequiredSignatures byte
byteArray = byteArray.slice(1); // Skip numCreditOnlySignedAccounts byte
byteArray = byteArray.slice(1); // Skip numCreditOnlyUnsignedAccounts byte
const accountCount = shortvec.decodeLength(byteArray); const accountCount = shortvec.decodeLength(byteArray);
let accounts = []; let accounts = [];
@ -450,14 +446,6 @@ export class Transaction {
const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH); const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH);
byteArray = byteArray.slice(PUBKEY_LENGTH); byteArray = byteArray.slice(PUBKEY_LENGTH);
const programIdCount = shortvec.decodeLength(byteArray);
let programs = [];
for (let i = 0; i < programIdCount; i++) {
const program = byteArray.slice(0, PUBKEY_LENGTH);
byteArray = byteArray.slice(PUBKEY_LENGTH);
programs.push(program);
}
const instructionCount = shortvec.decodeLength(byteArray); const instructionCount = shortvec.decodeLength(byteArray);
let instructions = []; let instructions = [];
for (let i = 0; i < instructionCount; i++) { for (let i = 0; i < instructionCount; i++) {
@ -484,7 +472,7 @@ export class Transaction {
for (let i = 0; i < instructionCount; i++) { for (let i = 0; i < instructionCount; i++) {
let instructionData = { let instructionData = {
keys: [], keys: [],
programId: new PublicKey(programs[instructions[i].programIndex]), programId: new PublicKey(accounts[instructions[i].programIndex]),
data: Buffer.from(instructions[i].data), data: Buffer.from(instructions[i].data),
}; };
for (let j = 0; j < instructions[i].accountIndex.length; j++) { for (let j = 0; j < instructions[i].accountIndex.length; j++) {

View File

@ -93,72 +93,74 @@ test('parse wire format and serialize', () => {
const wireTransaction = Buffer.from([ const wireTransaction = Buffer.from([
1, 1,
91,
132, 132,
50, 173,
204,
17,
25,
230,
33,
52,
8,
149,
124,
56,
114,
17,
236,
92,
93,
53,
234,
122,
120,
219,
193,
255,
2,
14,
87,
12,
207,
99,
241,
32,
151,
102,
70,
60,
218,
73,
232,
68,
33,
94,
134,
117,
138,
182,
179,
118,
249,
132,
52,
41,
162,
44,
0,
43,
193,
242,
120,
108,
4,
163,
191,
6,
1, 1,
2, 218,
94,
253,
18,
27,
79,
207,
114,
27,
167,
127,
17,
202,
183,
204,
12,
69,
243,
25,
206,
165,
116,
182,
64,
185,
168,
238,
168,
193,
140,
86,
83,
107,
252,
239,
80,
40,
91,
44,
6,
160,
84,
43,
227,
63,
170,
255,
185,
132,
242,
82,
46,
124,
217,
127,
147,
24,
254,
157,
13,
1,
0,
1,
3,
19, 19,
152, 152,
246, 246,
@ -223,6 +225,38 @@ test('parse wire format and serialize', () => {
74, 74,
243, 243,
252, 252,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
196, 196,
154, 154,
231, 231,
@ -256,40 +290,7 @@ test('parse wire format and serialize', () => {
82, 82,
235, 235,
1, 1,
0, 2,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
2, 2,
0, 0,
1, 1,