@@ -3,27 +3,21 @@ import PropTypes from "prop-types";
33import { useWalletSelector } from "@near-wallet-selector/react-hook" ;
44import { useEffect , useState } from "react" ;
55import { useDebounce } from "../hooks/debounce" ;
6- import { SIGNET_CONTRACT , MPC_CONTRACT } from "../config" ;
6+ import { SIGNET_CONTRACT } from "../config" ;
77import { chainAdapters } from "chainsig.js" ;
88import { Connection as SolanaConnection } from '@solana/web3.js'
99import { bigIntToDecimal } from "../utils/bigIntToDecimal" ;
1010import { decimalToBigInt } from "../utils/decimalToBigInt" ;
1111
12- function uint8ArrayToHex ( uint8Array ) {
13- return Array . from ( uint8Array )
14- . map ( byte => byte . toString ( 16 ) . padStart ( 2 , '0' ) )
15- . join ( '' ) ;
16- }
17-
1812const connection = new SolanaConnection ( "https://api.devnet.solana.com" ) ;
19- const solana = new chainAdapters . solana . Solana ( {
13+
14+ const Solana = new chainAdapters . solana . Solana ( {
2015 solanaConnection : connection ,
2116 contract : SIGNET_CONTRACT
22- } )
23-
17+ } )
2418export function SolanaView ( { props : { setStatus } } ) {
25- const { callFunction , signedAccountId } = useWalletSelector ( ) ;
26-
19+ const { signedAccountId , signAndSendTransactions } = useWalletSelector ( ) ;
20+
2721 const [ receiver , setReceiver ] = useState ( "G58AYKiiNy7wwjPAeBAQWTM6S1kJwP3MQ3wRWWhhSJxA" ) ;
2822 const [ amount , setAmount ] = useState ( 1 ) ;
2923 const [ loading , setLoading ] = useState ( false ) ;
@@ -45,103 +39,98 @@ export function SolanaView({ props: { setStatus } }) {
4539 setStatus ( "Querying your address and balance" ) ;
4640 setSenderAddress ( `Deriving address from path ${ derivationPath } ...` ) ;
4741
48- const { publicKey } = await solana . deriveAddressAndPublicKey ( signedAccountId , derivationPath ) ;
42+ const { publicKey } = await Solana . deriveAddressAndPublicKey ( signedAccountId , derivationPath ) ;
4943
5044 setSenderAddress ( publicKey ) ;
5145
52- const balance = await solana . getBalance ( publicKey ) ;
46+ const balance = await Solana . getBalance ( publicKey ) ;
5347
5448 setStatus (
55- `Your Solana address is:${ publicKey } , balance: ${ bigIntToDecimal ( balance . balance , balance . decimals ) } sol`
49+ `Your Solana address is:${ publicKey } , balance: ${ bigIntToDecimal ( balance . balance , balance . decimals ) } sol`
5650 ) ;
5751 }
5852 } , [ signedAccountId , derivationPath , setStatus ] ) ;
5953
60- async function chainSignature ( ) {
61- setStatus ( "🏗️ Creating transaction" ) ;
62-
63- const { transaction :{ transaction} } = await solana . prepareTransactionForSigning ( {
64- from : senderAddress ,
65- to : receiver ,
66- amount : decimalToBigInt ( amount , 9 ) ,
67- } )
68-
69- setStatus (
70- "🕒 Asking MPC to sign the transaction, this might take a while..."
71- ) ;
72-
73- try {
74- const rsvSignatures = await callFunction ( {
75- contractId : MPC_CONTRACT ,
76- method : "sign" ,
77- args : {
78- request : {
79- payload_v2 : { "Eddsa" : uint8ArrayToHex ( transaction . serializeMessage ( ) ) } ,
80- path : derivationPath ,
81- domain_id : 1 ,
82- } ,
83- } ,
84- gas : "250000000000000" , // 250 Tgas
85- deposit : 1 ,
86- } ) ;
87-
88- if ( ! rsvSignatures || ! rsvSignatures . signature ) {
89- throw new Error ( "Failed to sign transaction" ) ;
54+ async function chainSignature ( ) {
55+ setStatus ( "🏗️ Creating transaction" ) ;
56+
57+ const { transaction : { transaction } } = await Solana . prepareTransactionForSigning ( {
58+ from : senderAddress ,
59+ to : receiver ,
60+ amount : decimalToBigInt ( amount , 9 ) ,
61+ } )
62+
63+ setStatus (
64+ "🕒 Asking MPC to sign the transaction, this might take a while..."
65+ ) ;
66+
67+ try {
68+ const rsvSignatures = await SIGNET_CONTRACT . sign ( {
69+ payloads : [ transaction . serializeMessage ( ) ] ,
70+ path : derivationPath ,
71+ keyType : "Eddsa" ,
72+ signerAccount : {
73+ accountId : signedAccountId ,
74+ signAndSendTransactions
9075 }
76+ } ) ;
9177
92- const txSerialized = solana . finalizeTransactionSigning ( {
93- transaction,
94- rsvSignatures,
95- senderAddress
96- } )
97- await solana . broadcastTx ( txSerialized ) ;
98-
99- setStatus ( "✅ Signed payload ready to be relayed to the Solana network" ) ;
100- setSignedTransaction ( transaction . serialize ( ) . toString ( 'base64' ) ) ;
101- setStep ( "relay" ) ;
102- } catch ( e ) {
103- console . log ( e ) ;
104- setStatus ( `❌ Error: ${ e . message } ` ) ;
105- setLoading ( false ) ;
78+ if ( ! rsvSignatures [ 0 ] || ! rsvSignatures [ 0 ] . signature ) {
79+ throw new Error ( "Failed to sign transaction" ) ;
10680 }
81+
82+ const txSerialized = Solana . finalizeTransactionSigning ( {
83+ transaction,
84+ rsvSignatures : rsvSignatures [ 0 ] ,
85+ senderAddress
86+ } )
87+
88+ setStatus ( "✅ Signed payload ready to be relayed to the Solana network" ) ;
89+ setSignedTransaction ( txSerialized ) ;
90+ setStep ( "relay" ) ;
91+ } catch ( e ) {
92+ console . log ( e ) ;
93+ setStatus ( `❌ Error: ${ e . message } ` ) ;
94+ setLoading ( false ) ;
10795 }
108-
109- async function relayTransaction ( ) {
110- setLoading ( true ) ;
96+ }
97+
98+ async function relayTransaction ( ) {
99+ setLoading ( true ) ;
100+ setStatus (
101+ "🔗 Relaying transaction to the Solana network... this might take a while"
102+ ) ;
103+
104+ try {
105+
106+ const txHash = await Solana . broadcastTx ( signedTransaction ) ;
107+
111108 setStatus (
112- "🔗 Relaying transaction to the Solana network... this might take a while"
109+ < >
110+ < a
111+ href = { `https://explorer.solana.com/tx/${ txHash . hash } ?cluster=devnet` }
112+ target = "_blank"
113+ >
114+ { " " }
115+ ✅ Successfully Broadcasted{ " " }
116+ </ a >
117+ </ >
113118 ) ;
114-
115- try {
116-
117- const txHash = await solana . broadcastTx ( signedTransaction ) ;
118-
119- setStatus (
120- < >
121- < a
122- href = { `https://explorer.solana.com/tx/${ txHash . hash } ?cluster=devnet` }
123- target = "_blank"
124- >
125- { " " }
126- ✅ Successfully Broadcasted{ " " }
127- </ a >
128- </ >
129- ) ;
130- } catch ( e ) {
131- setStatus ( `❌ Error: ${ e . message } ` ) ;
132- }
133-
134- setStep ( "request" ) ;
135- setLoading ( false ) ;
119+ } catch ( e ) {
120+ setStatus ( `❌ Error: ${ e . message } ` ) ;
136121 }
137122
123+ setStep ( "request" ) ;
124+ setLoading ( false ) ;
125+ }
126+
138127 const UIChainSignature = async ( ) => {
139128 setLoading ( true ) ;
140129 await chainSignature ( ) ;
141130 setLoading ( false ) ;
142131 } ;
143132
144- return ( < >
133+ return ( < >
145134 < div className = "alert alert-info text-center" role = "alert" >
146135 You are working with < strong > DevTest</ strong > .
147136 < br />
0 commit comments