@@ -85,41 +85,71 @@ export class Transaction {
85
85
if ( ! lastId ) {
86
86
throw new Error ( 'Transaction lastId required' ) ;
87
87
}
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
+ ] ) ;
88
112
89
113
const signDataLayout = BufferLayout . struct ( [
90
- BufferLayout . ns64 ( 'keysLength' ) ,
114
+ BufferLayout . u32 ( 'accountKeysLength' ) ,
115
+ BufferLayout . u32 ( 'accountKeysLengthPadding' ) ,
91
116
BufferLayout . seq (
92
- Layout . publicKey ( 'key ' ) ,
93
- keys . length ,
94
- 'keys '
117
+ Layout . publicKey ( 'accountKey ' ) ,
118
+ BufferLayout . offset ( BufferLayout . u32 ( ) , - 8 ) ,
119
+ 'accountKeys '
95
120
) ,
96
- Layout . publicKey ( 'programId' ) ,
97
121
Layout . publicKey ( 'lastId' ) ,
98
122
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
+ ) ,
101
139
] ) ;
102
140
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
+ } ;
116
148
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 ) ;
122
151
signData = signData . slice ( 0 , length ) ;
152
+
123
153
return signData ;
124
154
}
125
155
0 commit comments