Skip to content

Commit a91c7fa

Browse files
authored
Merge pull request #66 from near-examples/updateExample
The example was updated to use sign from chainsing
2 parents d531c16 + b49eb82 commit a91c7fa

File tree

4 files changed

+114
-146
lines changed

4 files changed

+114
-146
lines changed

src/components/Bitcoin.jsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { useState, useEffect } from "react";
22

33
import { useDebounce } from "../hooks/debounce";
44
import PropTypes from "prop-types";
5-
import { SIGNET_CONTRACT, MPC_CONTRACT, NetworkId } from "../config";
5+
import { SIGNET_CONTRACT, NetworkId } from "../config";
66
import { useWalletSelector } from "@near-wallet-selector/react-hook";
7-
import { chainAdapters, utils } from "chainsig.js";
7+
import { chainAdapters } from "chainsig.js";
88
import { bigIntToDecimal } from "../utils/bigIntToDecimal";
99

1010
const btcRpcAdapter = new chainAdapters.btc.BTCRpcAdapters.Mempool(
@@ -14,8 +14,9 @@ const btcRpcAdapter = new chainAdapters.btc.BTCRpcAdapters.Mempool(
1414
const Bitcoin = new chainAdapters.btc.Bitcoin({
1515
network: NetworkId,
1616
btcRpcAdapter,
17-
contract: SIGNET_CONTRACT,
17+
contract: SIGNET_CONTRACT
1818
})
19+
1920
export function BitcoinView({ props: { setStatus } }) {
2021
const { signedAccountId, signAndSendTransactions } = useWalletSelector();
2122

@@ -74,32 +75,17 @@ export function BitcoinView({ props: { setStatus } }) {
7475
"🕒 Asking MPC to sign the transaction, this might take a while..."
7576
);
7677
try {
77-
const mpcTransactions = hashesToSign.map(
78-
(hash) => ({
79-
receiverId: MPC_CONTRACT,
80-
actions: [
81-
{
82-
type: 'FunctionCall',
83-
params: {
84-
methodName: "sign",
85-
args: {
86-
request: {
87-
payload: hash,
88-
path: derivationPath,
89-
key_version: 0,
90-
},
91-
},
92-
gas: "250000000000000",
93-
deposit: 1,
94-
},
95-
},
96-
],
97-
})
98-
)
99-
100-
const sentTxs = await signAndSendTransactions({ transactions: mpcTransactions });
101-
const rsvSignatures = sentTxs.map(tx => utils.cryptography.toRSV(tx))
102-
78+
79+
const rsvSignatures = await SIGNET_CONTRACT.sign({
80+
payloads: hashesToSign,
81+
path: derivationPath,
82+
keyType: "Ecdsa",
83+
signerAccount: {
84+
accountId: signedAccountId,
85+
signAndSendTransactions
86+
}
87+
});
88+
10389
if (!rsvSignatures) {
10490
throw new Error("No signature received");
10591
}

src/components/EVM/EVM.jsx

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import Web3 from "web3";
88

99
import { SIGNET_CONTRACT, MPC_CONTRACT } from "../../config";
1010
import { useWalletSelector } from "@near-wallet-selector/react-hook";
11-
import { chainAdapters, utils } from "chainsig.js";
11+
import { chainAdapters } from "chainsig.js";
1212
import { createPublicClient, http } from "viem";
1313
import { bigIntToDecimal } from "../../utils/bigIntToDecimal";
1414

15-
1615
export function EVMView({
1716
props: { setStatus, rpcUrl, contractAddress, explorerUrl },
1817
}) {
19-
const { callFunction, signedAccountId } = useWalletSelector();
18+
const { signedAccountId, signAndSendTransactions } = useWalletSelector();
2019

2120
const [loading, setLoading] = useState(false);
2221
const [step, setStep] = useState("request");
@@ -26,7 +25,6 @@ export function EVMView({
2625
const [action, setAction] = useState("transfer");
2726
const [derivation, setDerivation] = useState("ethereum-1");
2827
const [signedTransaction, setSignedTransaction] = useState(null);
29-
3028
const [gasPriceInGwei, setGasPriceInGwei] = useState("");
3129
const [txCost, setTxCost] = useState("");
3230

@@ -38,10 +36,10 @@ export function EVMView({
3836
transport: http(rpcUrl),
3937
});
4038

41-
const Evm = new chainAdapters.evm.EVM({
39+
const Evm = (new chainAdapters.evm.EVM({
4240
publicClient,
43-
contract: SIGNET_CONTRACT,
44-
})
41+
contract: SIGNET_CONTRACT
42+
}));
4543

4644
useEffect(() => {
4745
async function fetchEthereumGasPrice() {
@@ -99,7 +97,7 @@ export function EVMView({
9997
setSenderAddress(address);
10098
setSenderLabel(address);
10199
const balance = await Evm.getBalance(address);
102-
setBalance(bigIntToDecimal(balance.balance,balance.decimals));
100+
setBalance(bigIntToDecimal(balance.balance, balance.decimals));
103101
};
104102

105103
async function chainSignature() {
@@ -111,36 +109,32 @@ export function EVMView({
111109
setStatus(
112110
`🕒 Asking ${MPC_CONTRACT} to sign the transaction, this might take a while`
113111
);
114-
112+
115113
try {
116-
const signature = await callFunction({
117-
contractId: MPC_CONTRACT,
118-
method: "sign",
119-
args: {
120-
request: {
121-
payload: hashesToSign[0],
122-
path: derivationPath,
123-
key_version: 0,
124-
},
125-
},
126-
gas: "250000000000000", // 250 Tgas
127-
deposit: 1,
114+
115+
const rsvSignatures = await SIGNET_CONTRACT.sign({
116+
payloads: hashesToSign,
117+
path: derivationPath,
118+
keyType: "Ecdsa",
119+
signerAccount: {
120+
accountId: signedAccountId,
121+
signAndSendTransactions
122+
}
128123
});
129124

130-
setSignedTransaction(
131-
Evm.finalizeTransactionSigning({
132-
transaction: transaction,
133-
rsvSignatures: [utils.cryptography.toRSV(signature)],
134-
})
135-
);
125+
const txSerialized = Evm.finalizeTransactionSigning({
126+
transaction,
127+
rsvSignatures,
128+
})
129+
130+
setSignedTransaction(txSerialized);
136131

137132
setStatus(
138133
`✅ Signed payload ready to be relayed to the Ethereum network`
139134
);
140135
setStep("relay");
141136
} catch (e) {
142-
console.trace(e);
143-
137+
console.log(e);
144138
setStatus(`❌ Error: ${e.message}`);
145139
setLoading(false);
146140
}

src/components/Solana.jsx

Lines changed: 75 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@ import PropTypes from "prop-types";
33
import { useWalletSelector } from "@near-wallet-selector/react-hook";
44
import { useEffect, useState } from "react";
55
import { useDebounce } from "../hooks/debounce";
6-
import { SIGNET_CONTRACT, MPC_CONTRACT } from "../config";
6+
import { SIGNET_CONTRACT } from "../config";
77
import { chainAdapters } from "chainsig.js";
88
import { Connection as SolanaConnection } from '@solana/web3.js'
99
import { bigIntToDecimal } from "../utils/bigIntToDecimal";
1010
import { 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-
1812
const 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+
})
2418
export 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 />

src/config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ export const NetworkId = 'testnet';
1414
export const MPC_CONTRACT = 'v1.signer-prod.testnet'
1515
export const MPC_KEY = 'secp256k1:4NfTiv3UsGahebgTaHyD9vF8KYKMBnfd6kh94mK6xv8fGBiJB8TBtFMP5WWXz6B89Ac1fbpzPwAvoyQebemHFwx3';
1616

17-
export const SIGNET_CONTRACT = new contracts.near.ChainSignatureContract({
17+
export const SIGNET_CONTRACT = new contracts.ChainSignatureContract({
1818
networkId: NetworkId,
1919
contractId: MPC_CONTRACT,
20-
accountId: '',
2120
})
2221

2322
export const ABI = [

0 commit comments

Comments
 (0)