@@ -17,10 +17,10 @@ const requireApi = (peer) => {
1717} ;
1818
1919export async function getStatus ( peer ) {
20- const subnetBootstrapHex = b4a . isBuffer ( peer . bootstrap )
21- ? b4a . toString ( peer . bootstrap , "hex" )
22- : peer . bootstrap != null
23- ? String ( peer . bootstrap )
20+ const subnetBootstrapHex = b4a . isBuffer ( peer . config . bootstrap )
21+ ? b4a . toString ( peer . config . bootstrap , "hex" )
22+ : peer . config . bootstrap != null
23+ ? String ( peer . config . bootstrap )
2424 : null ;
2525
2626 const peerMsbAddress = peer . msbClient . pubKeyHexToAddress ( peer . wallet . publicKey ) ;
@@ -123,19 +123,67 @@ export async function contractGenerateNonce(peer) {
123123export async function contractPrepareTx ( peer , { prepared_command, address, nonce } = { } ) {
124124 const api = requireApi ( peer ) ;
125125 if ( ! isObject ( prepared_command ) ) throw new Error ( "prepared_command must be an object." ) ;
126+ if ( typeof prepared_command . type !== "string" || prepared_command . type . length < 1 ) {
127+ throw new Error ( "prepared_command.type must be a non-empty string." ) ;
128+ }
129+ if ( prepared_command . value === undefined ) {
130+ throw new Error ( "prepared_command.value is missing." ) ;
131+ }
126132 const addr = asHex32 ( address , "address" ) ;
127133 const n = asHex32 ( nonce , "nonce" ) ;
128134
129135 if ( peer ?. protocol . instance ?. safeJsonStringify == null ) {
130136 throw new Error ( "safeJsonStringify is not available on protocol instance." ) ;
131137 }
132138
133- const json = peer . protocol . instance . safeJsonStringify ( prepared_command ) ;
134- if ( json == null ) throw new Error ( "Failed to stringify prepared_command." ) ;
139+ const prepared_json = peer . protocol . instance . safeJsonStringify ( prepared_command ) ;
140+ if ( prepared_json == null ) throw new Error ( "Failed to stringify prepared_command." ) ;
141+
142+ const command_hash = await createHash ( prepared_json ) ;
143+
144+ const networkId = peer . msbClient . networkId ;
145+ const msbBootstrap = peer . msbClient . bootstrapHex ;
146+ const txv = await peer . msbClient . getTxvHex ( ) ;
147+ const subnetBootstrap =
148+ b4a . isBuffer ( peer . config . bootstrap )
149+ ? b4a . toString ( peer . config . bootstrap , "hex" )
150+ : peer . config . bootstrap != null
151+ ? String ( peer . config . bootstrap )
152+ : null ;
153+
154+ const incomingWritingKey = peer . writerLocalKey ?? api . getPeerWriterKey ?. ( ) ;
155+ if ( ! incomingWritingKey || ! / ^ [ 0 - 9 a - f ] { 64 } $ / i. test ( String ( incomingWritingKey ) ) ) {
156+ throw new Error ( "Peer writer key is not available." ) ;
157+ }
158+ if ( ! subnetBootstrap || ! / ^ [ 0 - 9 a - f ] { 64 } $ / i. test ( String ( subnetBootstrap ) ) ) {
159+ throw new Error ( "Peer subnet bootstrap is not available." ) ;
160+ }
161+
162+ const tx = await peer . protocol . instance . generateTx (
163+ networkId ,
164+ txv ,
165+ String ( incomingWritingKey ) . toLowerCase ( ) ,
166+ command_hash ,
167+ String ( subnetBootstrap ) . toLowerCase ( ) ,
168+ String ( msbBootstrap ) . toLowerCase ( ) ,
169+ n
170+ ) ;
135171
136- const command_hash = await createHash ( json ) ;
137- const tx = await api . generateTx ( addr , command_hash , n ) ;
138- return { tx, command_hash } ;
172+ return {
173+ tx,
174+ prepared_command,
175+ command_hash,
176+ msb : {
177+ networkId,
178+ txv,
179+ iw : String ( incomingWritingKey ) . toLowerCase ( ) ,
180+ ch : command_hash ,
181+ bs : String ( subnetBootstrap ) . toLowerCase ( ) ,
182+ mbs : String ( msbBootstrap ) . toLowerCase ( ) ,
183+ in : n ,
184+ operationType : 12 ,
185+ } ,
186+ } ;
139187}
140188
141189export async function contractTx ( peer , { tx, prepared_command, address, signature, nonce, sim = false } = { } ) {
0 commit comments