@@ -34,6 +34,8 @@ func main() {
3434 "encodeTransaction" : js .FuncOf (encodeTransaction ),
3535 "signTransaction" : js .FuncOf (signTransaction ),
3636 "encodeUnlockHash" : js .FuncOf (encodeUnlockHash ),
37+ "v2TxnSigHash" : js .FuncOf (v2TxnSigHash ),
38+ "v2SignTransaction" : js .FuncOf (v2SignTransaction ),
3739 })
3840
3941 c := make (chan bool , 1 )
@@ -242,6 +244,101 @@ func generateAddresses(this js.Value, args []js.Value) any {
242244 return nil
243245}
244246
247+ func v2TxnSigHash (this js.Value , args []js.Value ) any {
248+ if err := checkArgs (args , js .TypeString , js .TypeString , js .TypeNumber ); err != nil {
249+ return err .Error ()
250+ }
251+
252+ w := api .NewClient (SIASCAN_ADDRESS , "" )
253+ jsonTxn := args [0 ].String ()
254+
255+ var txn types.V2Transaction
256+ if err := json .Unmarshal ([]byte (jsonTxn ), & txn ); err != nil {
257+ return fmt .Sprintf ("error parsing transaction: %s" , err )
258+ }
259+
260+ cs , err := w .ConsensusTipState ()
261+ if err != nil {
262+ return fmt .Sprintf ("error getting consensus state: %s" , err )
263+ }
264+
265+ sigHash := cs .InputSigHash (txn )
266+ return sigHash .String ()
267+ }
268+
269+ func v2SignTransaction (this js.Value , args []js.Value ) any {
270+ if err := checkArgs (args , js .TypeString , js .TypeString , js .TypeObject , js .TypeFunction ); err != nil {
271+ return err .Error ()
272+ }
273+
274+ w := api .NewClient (SIASCAN_ADDRESS , "" )
275+
276+ phrase := args [0 ].String ()
277+ jsonTxn := args [1 ].String ()
278+ sigIndices := make ([]uint64 , args [2 ].Length ())
279+ callback := args [3 ]
280+
281+ for i := range sigIndices {
282+ sigIndices [i ] = uint64 (args [2 ].Index (i ).Int ())
283+ }
284+
285+ var txn types.V2Transaction
286+ if err := json .Unmarshal ([]byte (jsonTxn ), & txn ); err != nil {
287+ callback .Invoke (err .Error (), js .Null ())
288+ return err .Error ()
289+ }
290+
291+ if len (sigIndices ) != len (txn .SiacoinInputs )+ len (txn .SiafundInputs ) {
292+ err := fmt .Errorf ("expected %d signatures, got %d" , len (txn .SiacoinInputs )+ len (txn .SiafundInputs ), len (sigIndices ))
293+ callback .Invoke (err .Error (), js .Null ())
294+ return err .Error ()
295+ }
296+
297+ go func () {
298+ cs , err := w .ConsensusTipState ()
299+ if err != nil {
300+ callback .Invoke (fmt .Sprintf ("error getting consensus state: %s" , err ), js .Null ())
301+ return
302+ }
303+ sigHash := cs .InputSigHash (txn )
304+
305+ var seed [32 ]byte
306+ defer clear (seed [:])
307+ if err := phraseToSeed (phrase , & seed ); err != nil {
308+ callback .Invoke (err .Error (), js .Null ())
309+ return
310+ }
311+
312+ for i := range txn .SiacoinInputs {
313+ // pop the first index from sigIndices
314+ index := sigIndices [0 ]
315+ sigIndices = sigIndices [1 :]
316+ // sign the input
317+ sk := wallet .KeyFromSeed (& seed , index )
318+ sig := sk .SignHash (sigHash )
319+ txn .SiacoinInputs [i ].SatisfiedPolicy .Signatures = []types.Signature {sig }
320+ }
321+
322+ for i := range txn .SiafundInputs {
323+ // pop the first index from sigIndices
324+ index := sigIndices [0 ]
325+ sigIndices = sigIndices [1 :]
326+ // sign the input
327+ sk := wallet .KeyFromSeed (& seed , index )
328+ sig := sk .SignHash (sigHash )
329+ txn .SiafundInputs [i ].SatisfiedPolicy .Signatures = []types.Signature {sig }
330+ }
331+
332+ obj , err := interfaceToJSON (txn )
333+ if err != nil {
334+ callback .Invoke (fmt .Sprintf ("error encoding signed transaction: %s" , err ), js .Null ())
335+ return
336+ }
337+ callback .Invoke (js .Null (), obj )
338+ }()
339+ return nil
340+ }
341+
245342func recoverAddresses (this js.Value , args []js.Value ) any {
246343 if err := checkArgs (args , js .TypeString , js .TypeString , js .TypeNumber , js .TypeNumber , js .TypeNumber , js .TypeFunction ); err != nil {
247344 return err .Error ()
0 commit comments