@@ -30,27 +30,20 @@ export async function signatureFromTxHash(
30
30
} ,
31
31
body : JSON . stringify ( payload ) ,
32
32
} ) ;
33
+ if ( ! response . ok ) {
34
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
35
+ }
36
+
37
+ const json : JSONRPCResponse < FinalExecutionOutcome > = await response . json ( ) ;
33
38
34
- const jsonResponse = ( await response . json ( ) ) as JSONRPCResponse ;
35
- let base64Sig = jsonResponse . result . status ?. SuccessValue ;
36
- // TODO: Find an example when successValue isn't available and we need to enter this block.
37
- if ( base64Sig === "" ) {
38
- // Extract receipts_outcome
39
- const receiptsOutcome = jsonResponse . result . receipts_outcome ;
40
- // Map to get SuccessValue
41
- const successValues = receiptsOutcome . map (
42
- // eslint-disable-next-line
43
- ( outcome : any ) => outcome . outcome . status . SuccessValue
44
- ) ;
45
- // Find the first non-empty value
46
- base64Sig = successValues . find ( ( value ) => value && value . trim ( ) . length > 0 ) ;
39
+ if ( json . error ) {
40
+ throw new Error ( `JSON-RPC error: ${ json . error . message } ` ) ;
47
41
}
48
- if ( base64Sig ) {
49
- const decodedValue = Buffer . from ( base64Sig , "base64" ) . toString ( "utf-8" ) ;
50
- const signature : MPCSignature = JSON . parse ( decodedValue ) ;
51
- return transformSignature ( signature ) ;
42
+
43
+ if ( json . result ) {
44
+ return signatureFromOutcome ( json . result ) ;
52
45
} else {
53
- throw new Error ( `No valid values found in transaction receipt ${ txHash } ` ) ;
46
+ throw new Error ( `No FinalExecutionOutcome in response: ${ json } ` ) ;
54
47
}
55
48
}
56
49
@@ -70,8 +63,13 @@ export function signatureFromOutcome(
70
63
outcome : FinalExecutionOutcome | Partial < FinalExecutionOutcome >
71
64
) : Signature {
72
65
// TODO: Find example outcome when status is not of this casted type.
73
- const b64Sig = ( outcome . status as FinalExecutionStatus ) . SuccessValue ! ;
74
- const decodedValue = Buffer . from ( b64Sig , "base64" ) . toString ( "utf-8" ) ;
75
- const signature = JSON . parse ( decodedValue ) ;
76
- return transformSignature ( signature ) ;
66
+ const b64Sig = ( outcome . status as FinalExecutionStatus ) . SuccessValue ;
67
+ if ( b64Sig ) {
68
+ const decodedValue = Buffer . from ( b64Sig , "base64" ) . toString ( "utf-8" ) ;
69
+ const signature = JSON . parse ( decodedValue ) ;
70
+ return transformSignature ( signature ) ;
71
+ }
72
+ throw new Error (
73
+ `No detectable signature found in transaction ${ outcome . transaction_outcome ?. id } `
74
+ ) ;
77
75
}
0 commit comments