style: run prettier on all files

This commit is contained in:
Ben Kremer 2022-10-18 12:37:52 +02:00
parent fcec9e477a
commit b31232f2f6
21 changed files with 429 additions and 369 deletions

View File

@ -2,7 +2,7 @@
const nextConfig = { const nextConfig = {
reactStrictMode: true, reactStrictMode: true,
swcMinify: true, swcMinify: true,
distDir: 'build' distDir: "build",
}; };
module.exports = nextConfig; module.exports = nextConfig;

View File

@ -1,25 +1,27 @@
import { JsonRpcRequest } from "@walletconnect/jsonrpc-utils"; import { JsonRpcRequest } from "@walletconnect/jsonrpc-utils";
import { ChainsMap } from "caip-api"; import { ChainsMap } from "caip-api";
import { NamespaceMetadata, ChainMetadata, ChainRequestRender } from "../helpers"; import {
NamespaceMetadata,
ChainMetadata,
ChainRequestRender,
} from "../helpers";
export const NearMetadata: NamespaceMetadata = { export const NearMetadata: NamespaceMetadata = {
"testnet": { testnet: {
logo: "https://avatars.githubusercontent.com/u/7613128?s=200&v=4", logo: "https://avatars.githubusercontent.com/u/7613128?s=200&v=4",
rgb: "27, 31, 53", rgb: "27, 31, 53",
} },
}; };
// TODO: add `near` namespace to `caip-api` package to avoid manual specification here. // TODO: add `near` namespace to `caip-api` package to avoid manual specification here.
export const NearChainData: ChainsMap = { export const NearChainData: ChainsMap = {
"testnet": { testnet: {
"name": "NEAR Testnet", name: "NEAR Testnet",
"id": "near:testnet", id: "near:testnet",
"rpc": [ rpc: ["https://rpc.testnet.near.org"],
"https://rpc.testnet.near.org" slip44: 397,
], testnet: true,
"slip44": 397,
"testnet": true
}, },
}; };
@ -32,7 +34,9 @@ export function getChainMetadata(chainId: string): ChainMetadata {
return metadata; return metadata;
} }
export function getChainRequestRender(request: JsonRpcRequest): ChainRequestRender[] { export function getChainRequestRender(
request: JsonRpcRequest
): ChainRequestRender[] {
let params = [{ label: "Method", value: request.method }]; let params = [{ label: "Method", value: request.method }];
switch (request.method) { switch (request.method) {
@ -47,4 +51,4 @@ export function getChainRequestRender(request: JsonRpcRequest): ChainRequestRend
break; break;
} }
return params; return params;
} }

View File

@ -20,7 +20,7 @@ const SelectContainer = styled.select`
position: absolute; position: absolute;
bottom: 40px; bottom: 40px;
left: 50px; left: 50px;
direction: ltr; direction: ltr;
unicode-bidi: embed; unicode-bidi: embed;
`; `;
@ -30,24 +30,34 @@ const SelectOption = styled.option`
const Dropdown = (props: DropdownProps) => { const Dropdown = (props: DropdownProps) => {
const { relayerRegion, setRelayerRegion } = props; const { relayerRegion, setRelayerRegion } = props;
const [openSelect, setOpenSelect] = useState(false) const [openSelect, setOpenSelect] = useState(false);
const selectRef = React.createRef() const selectRef = React.createRef();
const openDropdown = () => { const openDropdown = () => {
setOpenSelect(!openSelect) setOpenSelect(!openSelect);
} };
return ( return (
<div style={{paddingTop: 72, width: 250, display: "flex", justifyContent: "center", alignItems: "flex-end"}}> <div
<button onClick={openDropdown} style={{background: "transparent"}}> style={{
<Icon size={30} src={"/assets/settings.svg"} /> paddingTop: 72,
width: 250,
display: "flex",
justifyContent: "center",
alignItems: "flex-end",
}}
>
<button onClick={openDropdown} style={{ background: "transparent" }}>
<Icon size={30} src={"/assets/settings.svg"} />
</button> </button>
{openSelect && ( {openSelect && (
<SelectContainer <SelectContainer
value={relayerRegion} value={relayerRegion}
onChange={(e) => setRelayerRegion?.(e?.target?.value)} onChange={(e) => setRelayerRegion?.(e?.target?.value)}
> >
<option selected disabled>Relayer Region:</option> <option selected disabled>
Relayer Region:
</option>
{REGIONALIZED_RELAYER_ENDPOINTS.map((e, i) => { {REGIONALIZED_RELAYER_ENDPOINTS.map((e, i) => {
return ( return (
<SelectOption key={i} value={e.value}> <SelectOption key={i} value={e.value}>
@ -55,8 +65,8 @@ const Dropdown = (props: DropdownProps) => {
</SelectOption> </SelectOption>
); );
})} })}
</SelectContainer>)} </SelectContainer>
)}
</div> </div>
); );
}; };

View File

@ -20,7 +20,7 @@ export const DEFAULT_TEST_CHAINS = [
"eip155:44787", "eip155:44787",
"solana:8E9rvCKLFQia2Y35HXjjpWzj8weVo44K", "solana:8E9rvCKLFQia2Y35HXjjpWzj8weVo44K",
"polkadot:e143f23803ac50e8f6f8e62695d1ce9e", "polkadot:e143f23803ac50e8f6f8e62695d1ce9e",
"near:testnet" "near:testnet",
]; ];
export const DEFAULT_CHAINS = [...DEFAULT_MAIN_CHAINS, ...DEFAULT_TEST_CHAINS]; export const DEFAULT_CHAINS = [...DEFAULT_MAIN_CHAINS, ...DEFAULT_TEST_CHAINS];
@ -86,12 +86,12 @@ export enum DEFAULT_POLKADOT_EVENTS {}
/** /**
* NEAR * NEAR
*/ */
export enum DEFAULT_NEAR_METHODS { export enum DEFAULT_NEAR_METHODS {
NEAR_SIGN_IN = 'near_signIn', NEAR_SIGN_IN = "near_signIn",
NEAR_SIGN_OUT = 'near_signOut', NEAR_SIGN_OUT = "near_signOut",
NEAR_GET_ACCOUNTS = 'near_getAccounts', NEAR_GET_ACCOUNTS = "near_getAccounts",
NEAR_SIGN_AND_SEND_TRANSACTION = 'near_signAndSendTransaction', NEAR_SIGN_AND_SEND_TRANSACTION = "near_signAndSendTransaction",
NEAR_SIGN_AND_SEND_TRANSACTIONS = 'near_signAndSendTransactions', NEAR_SIGN_AND_SEND_TRANSACTIONS = "near_signAndSendTransactions",
} }
export enum DEFAULT_NEAR_EVENTS {} export enum DEFAULT_NEAR_EVENTS {}

View File

@ -238,7 +238,7 @@ export function ClientContextProvider({
const createClient = useCallback(async () => { const createClient = useCallback(async () => {
try { try {
setIsInitializing(true); setIsInitializing(true);
const _client = await Client.init({ const _client = await Client.init({
logger: DEFAULT_LOGGER, logger: DEFAULT_LOGGER,
relayUrl: relayerRegion, relayUrl: relayerRegion,

View File

@ -628,13 +628,19 @@ export function JsonRpcContextProvider({
const polkadotRpc = { const polkadotRpc = {
testSignTransaction: _createJsonRpcRequestHandler( testSignTransaction: _createJsonRpcRequestHandler(
async (chainId: string, address: string): Promise<IFormattedRpcResponse> => { async (
chainId: string,
address: string
): Promise<IFormattedRpcResponse> => {
// Below example is a scale encoded payload for system.remark("this is a test wallet-connect remark") transaction. // Below example is a scale encoded payload for system.remark("this is a test wallet-connect remark") transaction.
// decode url: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/extrinsics/decode/0x00019074686973206973206120746573742077616c6c65742d636f6e6e6563742072656d61726b // decode url: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/extrinsics/decode/0x00019074686973206973206120746573742077616c6c65742d636f6e6e6563742072656d61726b
const transactionPayload = const transactionPayload =
"0x00019074686973206973206120746573742077616c6c65742d636f6e6e6563742072656d61726b05010000222400000d00000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3dc1f37ce7899cf20f63f5ea343f33e9e7b229c7e245049c2a7afc236861fc8b4"; "0x00019074686973206973206120746573742077616c6c65742d636f6e6e6563742072656d61726b05010000222400000d00000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3dc1f37ce7899cf20f63f5ea343f33e9e7b229c7e245049c2a7afc236861fc8b4";
try { try {
const result = await client!.request<{ payload: string; signature: string }>({ const result = await client!.request<{
payload: string;
signature: string;
}>({
chainId, chainId,
topic: session!.topic, topic: session!.topic,
request: { request: {
@ -645,11 +651,15 @@ export function JsonRpcContextProvider({
}, },
}, },
}); });
// sr25519 signatures need to wait for WASM to load // sr25519 signatures need to wait for WASM to load
await cryptoWaitReady(); await cryptoWaitReady();
const { isValid: valid } = signatureVerify(transactionPayload, result.signature, address); const { isValid: valid } = signatureVerify(
transactionPayload,
result.signature,
address
);
return { return {
method: DEFAULT_POLKADOT_METHODS.POLKADOT_SIGN_TRANSACTION, method: DEFAULT_POLKADOT_METHODS.POLKADOT_SIGN_TRANSACTION,
address, address,
@ -659,11 +669,13 @@ export function JsonRpcContextProvider({
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
}, }
), ),
testSignMessage: _createJsonRpcRequestHandler( testSignMessage: _createJsonRpcRequestHandler(
async (chainId: string, address: string): Promise<IFormattedRpcResponse> => { async (
chainId: string,
address: string
): Promise<IFormattedRpcResponse> => {
const message = `This is an example message to be signed - ${Date.now()}`; const message = `This is an example message to be signed - ${Date.now()}`;
try { try {
@ -681,7 +693,11 @@ export function JsonRpcContextProvider({
// sr25519 signatures need to wait for WASM to load // sr25519 signatures need to wait for WASM to load
await cryptoWaitReady(); await cryptoWaitReady();
const { isValid: valid } = signatureVerify(message, result.signature, address); const { isValid: valid } = signatureVerify(
message,
result.signature,
address
);
return { return {
method: DEFAULT_POLKADOT_METHODS.POLKADOT_SIGN_MESSAGE, method: DEFAULT_POLKADOT_METHODS.POLKADOT_SIGN_MESSAGE,
@ -692,7 +708,7 @@ export function JsonRpcContextProvider({
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
}, }
), ),
}; };
@ -700,8 +716,11 @@ export function JsonRpcContextProvider({
const nearRpc = { const nearRpc = {
testSignAndSendTransaction: _createJsonRpcRequestHandler( testSignAndSendTransaction: _createJsonRpcRequestHandler(
async (chainId: string, address: string): Promise<IFormattedRpcResponse> => { async (
const method = DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION chainId: string,
address: string
): Promise<IFormattedRpcResponse> => {
const method = DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION;
const result = await client!.request({ const result = await client!.request({
topic: session!.topic, topic: session!.topic,
chainId, chainId,
@ -711,16 +730,18 @@ export function JsonRpcContextProvider({
transaction: { transaction: {
signerId: address, signerId: address,
receiverId: "guest-book.testnet", receiverId: "guest-book.testnet",
actions: [{ actions: [
type: "FunctionCall", {
params: { type: "FunctionCall",
methodName: "addMessage", params: {
args: { text: "Hello from Wallet Connect!" }, methodName: "addMessage",
gas: "30000000000000", args: { text: "Hello from Wallet Connect!" },
deposit: "0", gas: "30000000000000",
} deposit: "0",
}] },
} },
],
},
}, },
}, },
}); });
@ -734,8 +755,11 @@ export function JsonRpcContextProvider({
} }
), ),
testSignAndSendTransactions: _createJsonRpcRequestHandler( testSignAndSendTransactions: _createJsonRpcRequestHandler(
async (chainId: string, address: string): Promise<IFormattedRpcResponse> => { async (
const method = DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTIONS chainId: string,
address: string
): Promise<IFormattedRpcResponse> => {
const method = DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTIONS;
const result = await client!.request({ const result = await client!.request({
topic: session!.topic, topic: session!.topic,
chainId, chainId,
@ -746,31 +770,35 @@ export function JsonRpcContextProvider({
{ {
signerId: address, signerId: address,
receiverId: "guest-book.testnet", receiverId: "guest-book.testnet",
actions: [{ actions: [
type: "FunctionCall", {
params: { type: "FunctionCall",
methodName: "addMessage", params: {
args: { text: "Hello from Wallet Connect! (1/2)" }, methodName: "addMessage",
gas: "30000000000000", args: { text: "Hello from Wallet Connect! (1/2)" },
deposit: "0", gas: "30000000000000",
} deposit: "0",
}] },
},
],
}, },
{ {
signerId: address, signerId: address,
receiverId: "guest-book.testnet", receiverId: "guest-book.testnet",
actions: [{ actions: [
type: "FunctionCall", {
params: { type: "FunctionCall",
methodName: "addMessage", params: {
args: { text: "Hello from Wallet Connect! (2/2)" }, methodName: "addMessage",
gas: "30000000000000", args: { text: "Hello from Wallet Connect! (2/2)" },
deposit: "0", gas: "30000000000000",
} deposit: "0",
}] },
} },
],
},
], ],
} },
}, },
}); });
@ -778,7 +806,9 @@ export function JsonRpcContextProvider({
method, method,
address, address,
valid: true, valid: true,
result: JSON.stringify((result as any).map((r: any) => r.transaction)), result: JSON.stringify(
(result as any).map((r: any) => r.transaction)
),
}; };
} }
), ),

View File

@ -214,17 +214,29 @@ const Home: NextPage = () => {
}; };
const getNearActions = (): AccountAction[] => { const getNearActions = (): AccountAction[] => {
const onSignAndSendTransaction = async (chainId: string, address: string) => { const onSignAndSendTransaction = async (
chainId: string,
address: string
) => {
openRequestModal(); openRequestModal();
await nearRpc.testSignAndSendTransaction(chainId, address); await nearRpc.testSignAndSendTransaction(chainId, address);
}; };
const onSignAndSendTransactions = async (chainId: string, address: string) => { const onSignAndSendTransactions = async (
chainId: string,
address: string
) => {
openRequestModal(); openRequestModal();
await nearRpc.testSignAndSendTransactions(chainId, address); await nearRpc.testSignAndSendTransactions(chainId, address);
}; };
return [ return [
{ method: DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION, callback: onSignAndSendTransaction }, {
{ method: DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTIONS, callback: onSignAndSendTransactions }, method: DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION,
callback: onSignAndSendTransaction,
},
{
method: DEFAULT_NEAR_METHODS.NEAR_SIGN_AND_SEND_TRANSACTIONS,
callback: onSignAndSendTransactions,
},
]; ];
}; };
@ -305,9 +317,9 @@ const Home: NextPage = () => {
{"Connect"} {"Connect"}
</SConnectButton> </SConnectButton>
<Dropdown <Dropdown
relayerRegion={relayerRegion} relayerRegion={relayerRegion}
setRelayerRegion={setRelayerRegion} setRelayerRegion={setRelayerRegion}
/> />
</SButtonContainer> </SButtonContainer>
</SLanding> </SLanding>
) : ( ) : (

View File

@ -5,7 +5,8 @@
"dev": "next dev -p 3001", "dev": "next dev -p 3001",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"lint": "next lint" "lint": "next lint",
"prettier:write": "prettier --write '**/*.{js,ts,jsx,tsx}'"
}, },
"dependencies": { "dependencies": {
"@cosmjs/amino": "0.28.4", "@cosmjs/amino": "0.28.4",

View File

@ -25,4 +25,3 @@ export default function AccountPicker() {
</select> </select>
) )
} }

View File

@ -17,7 +17,7 @@ const CHAIN_METADATA = {
...EIP155_MAINNET_CHAINS, ...EIP155_MAINNET_CHAINS,
...EIP155_TEST_CHAINS, ...EIP155_TEST_CHAINS,
...SOLANA_TEST_CHAINS, ...SOLANA_TEST_CHAINS,
...NEAR_TEST_CHAINS, ...NEAR_TEST_CHAINS
} }
/** /**

View File

@ -17,7 +17,7 @@ const CHAIN_METADATA = {
...EIP155_MAINNET_CHAINS, ...EIP155_MAINNET_CHAINS,
...EIP155_TEST_CHAINS, ...EIP155_TEST_CHAINS,
...SOLANA_TEST_CHAINS, ...SOLANA_TEST_CHAINS,
...NEAR_TEST_CHAINS, ...NEAR_TEST_CHAINS
} }
/** /**

View File

@ -16,18 +16,17 @@ export const NEAR_MAINNET_CHAINS = {
} }
interface NearTestChains { interface NearTestChains {
[key: string]: ChainMetadata; [key: string]: ChainMetadata
} }
type ChainMetadata = { type ChainMetadata = {
chainId: string; chainId: string
name: string; name: string
logo: string; logo: string
rgb: string; rgb: string
rpc: string; rpc: string
} }
export const NEAR_TEST_CHAINS: NearTestChains = { export const NEAR_TEST_CHAINS: NearTestChains = {
'near:testnet': { 'near:testnet': {
chainId: 'testnet', chainId: 'testnet',
@ -35,7 +34,7 @@ export const NEAR_TEST_CHAINS: NearTestChains = {
logo: '/chain-logos/near.png', logo: '/chain-logos/near.png',
rgb: '99, 125, 234', rgb: '99, 125, 234',
rpc: 'https://rpc.testnet.near.org' rpc: 'https://rpc.testnet.near.org'
}, }
} }
export const NEAR_CHAINS = { ...NEAR_MAINNET_CHAINS, ...NEAR_TEST_CHAINS } export const NEAR_CHAINS = { ...NEAR_MAINNET_CHAINS, ...NEAR_TEST_CHAINS }
@ -51,5 +50,5 @@ export const NEAR_SIGNING_METHODS = {
NEAR_SIGN_AND_SEND_TRANSACTION: 'near_signAndSendTransaction', NEAR_SIGN_AND_SEND_TRANSACTION: 'near_signAndSendTransaction',
NEAR_SIGN_TRANSACTIONS: 'near_signTransactions', NEAR_SIGN_TRANSACTIONS: 'near_signTransactions',
NEAR_SIGN_AND_SEND_TRANSACTIONS: 'near_signAndSendTransactions', NEAR_SIGN_AND_SEND_TRANSACTIONS: 'near_signAndSendTransactions',
NEAR_VERIFY_OWNER: 'near_verifyOwner', NEAR_VERIFY_OWNER: 'near_verifyOwner'
} }

View File

@ -28,4 +28,4 @@ export const REGIONALIZED_RELAYER_ENDPOINTS: RelayerType[] = [
value: 'wss://ap-southeast-1.relay.walletconnect.com/', value: 'wss://ap-southeast-1.relay.walletconnect.com/',
label: 'Asia Pacific' label: 'Asia Pacific'
} }
] ]

View File

@ -3,176 +3,178 @@ import {
providers, providers,
keyStores as nearKeyStores, keyStores as nearKeyStores,
transactions as nearTransactions, transactions as nearTransactions,
utils, utils
} from "near-api-js"; } from 'near-api-js'
import { AccessKeyView } from "near-api-js/lib/providers/provider"; import { AccessKeyView } from 'near-api-js/lib/providers/provider'
import { signClient } from "@/utils/WalletConnectUtil"; import { signClient } from '@/utils/WalletConnectUtil'
import { NEAR_TEST_CHAINS, TNearChain } from "@/data/NEARData"; import { NEAR_TEST_CHAINS, TNearChain } from '@/data/NEARData'
const MAX_ACCOUNTS = 2; const MAX_ACCOUNTS = 2
interface Account { interface Account {
accountId: string; accountId: string
publicKey: string; publicKey: string
} }
interface Transaction { interface Transaction {
signerId: string; signerId: string
receiverId: string; receiverId: string
actions: Array<nearTransactions.Action>; actions: Array<nearTransactions.Action>
} }
interface CreateTransactionsParams { interface CreateTransactionsParams {
chainId: string; chainId: string
transactions: Array<Transaction>; transactions: Array<Transaction>
} }
interface GetAccountsParams { interface GetAccountsParams {
topic: string; topic: string
} }
interface SignInParams { interface SignInParams {
chainId: string; chainId: string
topic: string; topic: string
permission: nearTransactions.FunctionCallPermission; permission: nearTransactions.FunctionCallPermission
accounts: Array<Account>; accounts: Array<Account>
} }
interface SignOutParams { interface SignOutParams {
chainId: string; chainId: string
topic: string; topic: string
accounts: Array<Account>; accounts: Array<Account>
} }
interface SignTransactionsParams { interface SignTransactionsParams {
chainId: string; chainId: string
topic: string; topic: string
transactions: Array<nearTransactions.Transaction>; transactions: Array<nearTransactions.Transaction>
} }
interface SignAndSendTransactionParams { interface SignAndSendTransactionParams {
chainId: string; chainId: string
topic: string; topic: string
transaction: nearTransactions.Transaction; transaction: nearTransactions.Transaction
} }
interface SignAndSendTransactionsParams { interface SignAndSendTransactionsParams {
chainId: string; chainId: string
topic: string; topic: string
transactions: Array<nearTransactions.Transaction>; transactions: Array<nearTransactions.Transaction>
} }
export class NearWallet { export class NearWallet {
private networkId: string; private networkId: string
private keyStore: nearKeyStores.KeyStore; private keyStore: nearKeyStores.KeyStore
static async init(networkId: string) { static async init(networkId: string) {
const keyStore = new nearKeyStores.BrowserLocalStorageKeyStore(); const keyStore = new nearKeyStores.BrowserLocalStorageKeyStore()
const accounts = await keyStore.getAccounts(networkId); const accounts = await keyStore.getAccounts(networkId)
for (let i = 0; i < Math.max(MAX_ACCOUNTS - accounts.length, 0); i += 1) { for (let i = 0; i < Math.max(MAX_ACCOUNTS - accounts.length, 0); i += 1) {
const { accountId, keyPair } = await NearWallet.createDevAccount(); const { accountId, keyPair } = await NearWallet.createDevAccount()
await keyStore.setKey(networkId, accountId, keyPair); await keyStore.setKey(networkId, accountId, keyPair)
} }
return new NearWallet(networkId, keyStore); return new NearWallet(networkId, keyStore)
} }
static async createDevAccount() { static async createDevAccount() {
const keyPair = utils.KeyPair.fromRandom("ed25519"); const keyPair = utils.KeyPair.fromRandom('ed25519')
const randomNumber = Math.floor(Math.random() * (99999999999999 - 10000000000000) + 10000000000000); const randomNumber = Math.floor(
const accountId = `dev-${Date.now()}-${randomNumber}`; Math.random() * (99999999999999 - 10000000000000) + 10000000000000
const publicKey = keyPair.getPublicKey().toString(); )
const accountId = `dev-${Date.now()}-${randomNumber}`
const publicKey = keyPair.getPublicKey().toString()
return fetch(`https://helper.testnet.near.org/account`, { return fetch(`https://helper.testnet.near.org/account`, {
method: "POST", method: 'POST',
headers: { "Content-Type": "application/json" }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
newAccountId: accountId, newAccountId: accountId,
newAccountPublicKey: publicKey, newAccountPublicKey: publicKey
}), })
}) }).then(res => {
.then((res) => { if (res.ok) {
if (res.ok) { return {
return { accountId,
accountId, keyPair
keyPair
};
} }
}
throw new Error("Failed to create NEAR dev account"); throw new Error('Failed to create NEAR dev account')
}); })
} }
private constructor(networkId: string, keyStore: nearKeyStores.KeyStore) { private constructor(networkId: string, keyStore: nearKeyStores.KeyStore) {
this.networkId = networkId; this.networkId = networkId
this.keyStore = keyStore; this.keyStore = keyStore
} }
getKeyStore() { getKeyStore() {
return this.keyStore; return this.keyStore
} }
// Retrieve all imported accounts from wallet. // Retrieve all imported accounts from wallet.
async getAllAccounts(): Promise<Array<Account>> { async getAllAccounts(): Promise<Array<Account>> {
const accountIds = await this.keyStore.getAccounts(this.networkId); const accountIds = await this.keyStore.getAccounts(this.networkId)
return Promise.all( return Promise.all(
accountIds.map(async (accountId) => { accountIds.map(async accountId => {
const keyPair = await this.keyStore.getKey(this.networkId, accountId); const keyPair = await this.keyStore.getKey(this.networkId, accountId)
return { return {
accountId, accountId,
publicKey: keyPair.getPublicKey().toString(), publicKey: keyPair.getPublicKey().toString()
}; }
}) })
); )
} }
private isAccountsValid(topic: string, accounts: Array<{ accountId: string; }>) { private isAccountsValid(topic: string, accounts: Array<{ accountId: string }>) {
const session = signClient.session.get(topic); const session = signClient.session.get(topic)
const validAccountIds = session.namespaces.near.accounts.map((accountId) => { const validAccountIds = session.namespaces.near.accounts.map(accountId => {
return accountId.split(":")[2]; return accountId.split(':')[2]
}) })
return accounts.every(({ accountId }) => { return accounts.every(({ accountId }) => {
return validAccountIds.includes(accountId); return validAccountIds.includes(accountId)
}); })
} }
private isTransactionsValid(topic: string, transactions: Array<nearTransactions.Transaction>) { private isTransactionsValid(topic: string, transactions: Array<nearTransactions.Transaction>) {
const accounts = transactions.map(({ signerId }) => ({ accountId: signerId })); const accounts = transactions.map(({ signerId }) => ({ accountId: signerId }))
return this.isAccountsValid(topic, accounts); return this.isAccountsValid(topic, accounts)
} }
async createTransactions({ chainId, transactions }: CreateTransactionsParams): Promise<Array<nearTransactions.Transaction>> { async createTransactions({
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc); chainId,
const txs: Array<nearTransactions.Transaction> = []; transactions
}: CreateTransactionsParams): Promise<Array<nearTransactions.Transaction>> {
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc)
const txs: Array<nearTransactions.Transaction> = []
const [block, accounts] = await Promise.all([ const [block, accounts] = await Promise.all([
provider.block({ finality: "final" }), provider.block({ finality: 'final' }),
this.getAllAccounts(), this.getAllAccounts()
]); ])
for (let i = 0; i < transactions.length; i += 1) { for (let i = 0; i < transactions.length; i += 1) {
const transaction = transactions[i]; const transaction = transactions[i]
const account = accounts.find( const account = accounts.find(x => x.accountId === transaction.signerId)
(x) => x.accountId === transaction.signerId
);
if (!account) { if (!account) {
throw new Error("Invalid signer id"); throw new Error('Invalid signer id')
} }
const accessKey = await provider.query<AccessKeyView>({ const accessKey = await provider.query<AccessKeyView>({
request_type: "view_access_key", request_type: 'view_access_key',
finality: "final", finality: 'final',
account_id: transaction.signerId, account_id: transaction.signerId,
public_key: account.publicKey, public_key: account.publicKey
}); })
txs.push( txs.push(
nearTransactions.createTransaction( nearTransactions.createTransaction(
@ -183,103 +185,103 @@ export class NearWallet {
transaction.actions, transaction.actions,
utils.serialize.base_decode(block.header.hash) utils.serialize.base_decode(block.header.hash)
) )
); )
} }
return txs; return txs
} }
async getAccounts({ topic }: GetAccountsParams): Promise<Array<Account>> { async getAccounts({ topic }: GetAccountsParams): Promise<Array<Account>> {
const session = signClient.session.get(topic); const session = signClient.session.get(topic)
return Promise.all( return Promise.all(
session.namespaces.near.accounts.map(async (account) => { session.namespaces.near.accounts.map(async account => {
const accountId = account.split(":")[2]; const accountId = account.split(':')[2]
const keyPair = await this.keyStore.getKey(this.networkId, accountId); const keyPair = await this.keyStore.getKey(this.networkId, accountId)
return { return {
accountId, accountId,
publicKey: keyPair.getPublicKey().toString() publicKey: keyPair.getPublicKey().toString()
}; }
}) })
); )
} }
async signIn({ chainId, topic, permission, accounts }: SignInParams): Promise<Array<Account>> { async signIn({ chainId, topic, permission, accounts }: SignInParams): Promise<Array<Account>> {
if (!this.isAccountsValid(topic, accounts)) { if (!this.isAccountsValid(topic, accounts)) {
throw new Error("Invalid accounts"); throw new Error('Invalid accounts')
} }
const result: Array<Account> = []; const result: Array<Account> = []
for (let i = 0; i < accounts.length; i += 1) { for (let i = 0; i < accounts.length; i += 1) {
const account = accounts[i]; const account = accounts[i]
try { try {
const [transaction] = await this.createTransactions({ const [transaction] = await this.createTransactions({
chainId, chainId,
transactions: [{ transactions: [
signerId: account.accountId, {
receiverId: account.accountId, signerId: account.accountId,
actions: [ receiverId: account.accountId,
nearTransactions.addKey( actions: [
utils.PublicKey.from(account.publicKey), nearTransactions.addKey(
nearTransactions.functionCallAccessKey( utils.PublicKey.from(account.publicKey),
permission.receiverId, nearTransactions.functionCallAccessKey(
permission.methodNames, permission.receiverId,
permission.allowance permission.methodNames,
permission.allowance
)
) )
) ]
] }
}] ]
}); })
await this.signAndSendTransaction({ chainId, topic, transaction }); await this.signAndSendTransaction({ chainId, topic, transaction })
result.push(account); result.push(account)
} catch (err) { } catch (err) {
console.log(`Failed to create FunctionCall access key for ${account.accountId}`); console.log(`Failed to create FunctionCall access key for ${account.accountId}`)
console.error(err); console.error(err)
} }
} }
return result; return result
} }
async signOut({ chainId, topic, accounts }: SignOutParams): Promise<Array<Account>> { async signOut({ chainId, topic, accounts }: SignOutParams): Promise<Array<Account>> {
if (!this.isAccountsValid(topic, accounts)) { if (!this.isAccountsValid(topic, accounts)) {
throw new Error("Invalid accounts"); throw new Error('Invalid accounts')
} }
const result: Array<Account> = []; const result: Array<Account> = []
for (let i = 0; i < accounts.length; i += 1) { for (let i = 0; i < accounts.length; i += 1) {
const account = accounts[i]; const account = accounts[i]
try { try {
const [transaction] = await this.createTransactions({ const [transaction] = await this.createTransactions({
chainId, chainId,
transactions: [{ transactions: [
signerId: account.accountId, {
receiverId: account.accountId, signerId: account.accountId,
actions: [ receiverId: account.accountId,
nearTransactions.deleteKey( actions: [nearTransactions.deleteKey(utils.PublicKey.from(account.publicKey))]
utils.PublicKey.from(account.publicKey) }
) ]
] })
}]
});
await this.signAndSendTransaction({ chainId, topic, transaction }); await this.signAndSendTransaction({ chainId, topic, transaction })
} catch (err) { } catch (err) {
console.log(`Failed to remove FunctionCall access key for ${account.accountId}`); console.log(`Failed to remove FunctionCall access key for ${account.accountId}`)
console.error(err); console.error(err)
result.push(account); result.push(account)
} }
} }
return result; return result
} }
async signTransactions({ async signTransactions({
@ -287,28 +289,28 @@ export class NearWallet {
topic, topic,
transactions transactions
}: SignTransactionsParams): Promise<Array<nearTransactions.SignedTransaction>> { }: SignTransactionsParams): Promise<Array<nearTransactions.SignedTransaction>> {
const networkId = chainId.split(":")[1]; const networkId = chainId.split(':')[1]
const signer = new InMemorySigner(this.keyStore); const signer = new InMemorySigner(this.keyStore)
const signedTxs: Array<nearTransactions.SignedTransaction> = []; const signedTxs: Array<nearTransactions.SignedTransaction> = []
if (!this.isTransactionsValid(topic, transactions)) { if (!this.isTransactionsValid(topic, transactions)) {
throw new Error("Invalid transactions"); throw new Error('Invalid transactions')
} }
for (let i = 0; i < transactions.length; i += 1) { for (let i = 0; i < transactions.length; i += 1) {
const transaction = transactions[i]; const transaction = transactions[i]
const [, signedTx] = await nearTransactions.signTransaction( const [, signedTx] = await nearTransactions.signTransaction(
transaction, transaction,
signer, signer,
transaction.signerId, transaction.signerId,
networkId networkId
); )
signedTxs.push(signedTx); signedTxs.push(signedTx)
} }
return signedTxs; return signedTxs
} }
async signAndSendTransaction({ async signAndSendTransaction({
@ -316,14 +318,14 @@ export class NearWallet {
topic, topic,
transaction transaction
}: SignAndSendTransactionParams): Promise<providers.FinalExecutionOutcome> { }: SignAndSendTransactionParams): Promise<providers.FinalExecutionOutcome> {
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc); const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc)
const [signedTx] = await this.signTransactions({ const [signedTx] = await this.signTransactions({
chainId, chainId,
topic, topic,
transactions: [transaction] transactions: [transaction]
}); })
return provider.sendTransaction(signedTx); return provider.sendTransaction(signedTx)
} }
async signAndSendTransactions({ async signAndSendTransactions({
@ -331,16 +333,16 @@ export class NearWallet {
topic, topic,
transactions transactions
}: SignAndSendTransactionsParams): Promise<Array<providers.FinalExecutionOutcome>> { }: SignAndSendTransactionsParams): Promise<Array<providers.FinalExecutionOutcome>> {
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc); const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc)
const signedTxs = await this.signTransactions({ chainId, topic, transactions }); const signedTxs = await this.signTransactions({ chainId, topic, transactions })
const results: Array<providers.FinalExecutionOutcome> = []; const results: Array<providers.FinalExecutionOutcome> = []
for (let i = 0; i < signedTxs.length; i += 1) { for (let i = 0; i < signedTxs.length; i += 1) {
const signedTx = signedTxs[i]; const signedTx = signedTxs[i]
results.push(await provider.sendTransaction(signedTx)); results.push(await provider.sendTransaction(signedTx))
} }
return results; return results
} }
} }

View File

@ -12,9 +12,8 @@ import { useSnapshot } from 'valtio'
import { NEAR_TEST_CHAINS } from '@/data/NEARData' import { NEAR_TEST_CHAINS } from '@/data/NEARData'
export default function HomePage() { export default function HomePage() {
const { testNets, eip155Address, cosmosAddress, solanaAddress, polkadotAddress, nearAddress } = useSnapshot( const { testNets, eip155Address, cosmosAddress, solanaAddress, polkadotAddress, nearAddress } =
SettingsStore.state useSnapshot(SettingsStore.state)
)
return ( return (
<Fragment> <Fragment>

View File

@ -44,12 +44,11 @@ export default function SettingsPage() {
<Divider y={2} /> <Divider y={2} />
<Row justify="space-between" align="center"> <Row justify="space-between" align="center">
<Text h4 css={{ marginBottom: '$5' }}> <Text h4 css={{ marginBottom: '$5' }}>
Relayer Region Relayer Region
</Text> </Text>
<RelayRegionPicker/> <RelayRegionPicker />
</Row> </Row>
<Divider y={2} /> <Divider y={2} />

View File

@ -105,7 +105,7 @@ export function isPolkadotChain(chain: string) {
/** /**
* Check if chain is part of NEAR standard * Check if chain is part of NEAR standard
*/ */
export function isNearChain(chain: string) { export function isNearChain(chain: string) {
return chain.includes('near') return chain.includes('near')
} }

View File

@ -3,9 +3,9 @@ import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils'
import { SignClientTypes } from '@walletconnect/types' import { SignClientTypes } from '@walletconnect/types'
import { getSdkError } from '@walletconnect/utils' import { getSdkError } from '@walletconnect/utils'
import { nearWallet } from '@/utils/NearWalletUtil' import { nearWallet } from '@/utils/NearWalletUtil'
import { InMemorySigner, transactions, utils, Connection } from "near-api-js"; import { InMemorySigner, transactions, utils, Connection } from 'near-api-js'
import { Transaction } from "@near-wallet-selector/core"; import { Transaction } from '@near-wallet-selector/core'
import { createAction } from "@near-wallet-selector/wallet-utils"; import { createAction } from '@near-wallet-selector/wallet-utils'
export async function approveNearRequest( export async function approveNearRequest(
requestEvent: SignClientTypes.EventArguments['session_request'] requestEvent: SignClientTypes.EventArguments['session_request']
@ -15,132 +15,135 @@ export async function approveNearRequest(
switch (request.method) { switch (request.method) {
case NEAR_SIGNING_METHODS.NEAR_SIGN_IN: { case NEAR_SIGNING_METHODS.NEAR_SIGN_IN: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const accounts = await nearWallet.signIn({ const accounts = await nearWallet.signIn({
chainId, chainId,
topic, topic,
permission: request.params.permission, permission: request.params.permission,
accounts: request.params.accounts, accounts: request.params.accounts
}); })
return formatJsonRpcResult(id, accounts); return formatJsonRpcResult(id, accounts)
} }
case NEAR_SIGNING_METHODS.NEAR_SIGN_OUT: { case NEAR_SIGNING_METHODS.NEAR_SIGN_OUT: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const accounts = await nearWallet.signOut({ const accounts = await nearWallet.signOut({
chainId, chainId,
topic, topic,
accounts: request.params.accounts accounts: request.params.accounts
}); })
return formatJsonRpcResult(id, accounts); return formatJsonRpcResult(id, accounts)
} }
case NEAR_SIGNING_METHODS.NEAR_GET_ACCOUNTS: { case NEAR_SIGNING_METHODS.NEAR_GET_ACCOUNTS: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const accounts = await nearWallet.getAccounts({ topic }); const accounts = await nearWallet.getAccounts({ topic })
return formatJsonRpcResult(id, accounts); return formatJsonRpcResult(id, accounts)
} }
case NEAR_SIGNING_METHODS.NEAR_SIGN_TRANSACTION: { case NEAR_SIGNING_METHODS.NEAR_SIGN_TRANSACTION: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const [signedTx] = await nearWallet.signTransactions({ const [signedTx] = await nearWallet.signTransactions({
chainId, chainId,
topic, topic,
transactions: [transactions.Transaction.decode( transactions: [transactions.Transaction.decode(Buffer.from(request.params.transaction))]
Buffer.from(request.params.transaction), })
)]
});
return formatJsonRpcResult(id, signedTx.encode()); return formatJsonRpcResult(id, signedTx.encode())
} }
case NEAR_SIGNING_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION: { case NEAR_SIGNING_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const [transaction] = await nearWallet.createTransactions({ const [transaction] = await nearWallet.createTransactions({
chainId, chainId,
transactions: [{ transactions: [
...params.request.params.transaction, {
actions: params.request.params.transaction.actions.map(createAction), ...params.request.params.transaction,
}] actions: params.request.params.transaction.actions.map(createAction)
}); }
]
})
const result = await nearWallet.signAndSendTransaction({ const result = await nearWallet.signAndSendTransaction({
chainId, chainId,
topic, topic,
transaction, transaction
}); })
return formatJsonRpcResult(id, result); return formatJsonRpcResult(id, result)
} }
case NEAR_SIGNING_METHODS.NEAR_SIGN_TRANSACTIONS: { case NEAR_SIGNING_METHODS.NEAR_SIGN_TRANSACTIONS: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const signedTxs = await nearWallet.signTransactions({ const signedTxs = await nearWallet.signTransactions({
chainId, chainId,
topic, topic,
transactions: params.request.params.transactions.map((tx: Uint8Array) => { transactions: params.request.params.transactions.map((tx: Uint8Array) => {
return transactions.Transaction.decode(Buffer.from(tx)); return transactions.Transaction.decode(Buffer.from(tx))
}), })
}); })
return formatJsonRpcResult(id, signedTxs.map((x) => x.encode())); return formatJsonRpcResult(
id,
signedTxs.map(x => x.encode())
)
} }
case NEAR_SIGNING_METHODS.NEAR_VERIFY_OWNER: { case NEAR_SIGNING_METHODS.NEAR_VERIFY_OWNER: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const accounts = await nearWallet.getAllAccounts(); const accounts = await nearWallet.getAllAccounts()
const account = accounts.find(acc => acc.accountId === params.request.params.accountId); const account = accounts.find(acc => acc.accountId === params.request.params.accountId)
if (!account) { if (!account) {
throw new Error(`Did not find account with id: ${params.request.params.accountId}`); throw new Error(`Did not find account with id: ${params.request.params.accountId}`)
} }
if (!NEAR_TEST_CHAINS[chainId]) { if (!NEAR_TEST_CHAINS[chainId]) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const signer = new InMemorySigner(nearWallet.getKeyStore()); const signer = new InMemorySigner(nearWallet.getKeyStore())
const networkId = chainId.split(':')[1]; const networkId = chainId.split(':')[1]
const connection = Connection.fromConfig({ const connection = Connection.fromConfig({
networkId, networkId,
provider: { type: 'JsonRpcProvider', args: { url: NEAR_TEST_CHAINS[chainId].rpc } }, provider: { type: 'JsonRpcProvider', args: { url: NEAR_TEST_CHAINS[chainId].rpc } },
signer signer
}); })
const blockInfo = await connection.provider.block({ finality: 'final' }); const blockInfo = await connection.provider.block({ finality: 'final' })
const publicKey = utils.PublicKey.from(account.publicKey); const publicKey = utils.PublicKey.from(account.publicKey)
const data = { const data = {
accountId: account.accountId, accountId: account.accountId,
@ -148,42 +151,42 @@ export async function approveNearRequest(
blockId: blockInfo.header.hash, blockId: blockInfo.header.hash,
publicKey: Buffer.from(publicKey.data).toString('base64'), publicKey: Buffer.from(publicKey.data).toString('base64'),
keyType: publicKey.keyType keyType: publicKey.keyType
}; }
const encoded = new Uint8Array(Buffer.from(JSON.stringify(data))); const encoded = new Uint8Array(Buffer.from(JSON.stringify(data)))
const signed = await signer.signMessage(encoded, account.accountId, networkId); const signed = await signer.signMessage(encoded, account.accountId, networkId)
return formatJsonRpcResult(id, { return formatJsonRpcResult(id, {
...data, ...data,
signature: Buffer.from(signed.signature).toString('base64'), signature: Buffer.from(signed.signature).toString('base64'),
keyType: signed.publicKey.keyType keyType: signed.publicKey.keyType
}); })
} }
case NEAR_SIGNING_METHODS.NEAR_SIGN_AND_SEND_TRANSACTIONS: { case NEAR_SIGNING_METHODS.NEAR_SIGN_AND_SEND_TRANSACTIONS: {
console.log("approve", { id, params }); console.log('approve', { id, params })
if (!chainId) { if (!chainId) {
throw new Error("Invalid chain id"); throw new Error('Invalid chain id')
} }
const transactions = await nearWallet.createTransactions({ const transactions = await nearWallet.createTransactions({
chainId, chainId,
transactions: params.request.params.transactions.map((transaction: Transaction) => ({ transactions: params.request.params.transactions.map((transaction: Transaction) => ({
...transaction, ...transaction,
actions: transaction.actions.map(createAction), actions: transaction.actions.map(createAction)
})) }))
}); })
const result = await nearWallet.signAndSendTransactions({ const result = await nearWallet.signAndSendTransactions({
chainId, chainId,
topic, topic,
transactions, transactions
}); })
return formatJsonRpcResult(id, result); return formatJsonRpcResult(id, result)
} }
default: default:
throw new Error(getSdkError("INVALID_METHOD").message) throw new Error(getSdkError('INVALID_METHOD').message)
} }
} }

View File

@ -1,21 +1,21 @@
import { NearWallet } from "@/lib/NearLib"; import { NearWallet } from '@/lib/NearLib'
export let nearAddresses: string[]; export let nearAddresses: string[]
export let nearWallet: NearWallet; export let nearWallet: NearWallet
/** /**
* Utilities * Utilities
*/ */
export async function createOrRestoreNearWallet() { export async function createOrRestoreNearWallet() {
// NEAR only supports dev accounts in testnet. // NEAR only supports dev accounts in testnet.
const wallet = await NearWallet.init("testnet"); const wallet = await NearWallet.init('testnet')
const accounts = await wallet.getAllAccounts(); const accounts = await wallet.getAllAccounts()
nearAddresses = accounts.map((x) => x.accountId); nearAddresses = accounts.map(x => x.accountId)
nearWallet = wallet; nearWallet = wallet
return { return {
nearWallet, nearWallet,
nearAddresses nearAddresses
} }
} }

View File

@ -6,7 +6,13 @@ import ModalStore from '@/store/ModalStore'
import { cosmosAddresses } from '@/utils/CosmosWalletUtil' import { cosmosAddresses } from '@/utils/CosmosWalletUtil'
import { eip155Addresses } from '@/utils/EIP155WalletUtil' import { eip155Addresses } from '@/utils/EIP155WalletUtil'
import { polkadotAddresses } from '@/utils/PolkadotWalletUtil' import { polkadotAddresses } from '@/utils/PolkadotWalletUtil'
import { isCosmosChain, isEIP155Chain, isSolanaChain, isPolkadotChain, isNearChain } from '@/utils/HelperUtil' import {
isCosmosChain,
isEIP155Chain,
isSolanaChain,
isPolkadotChain,
isNearChain
} from '@/utils/HelperUtil'
import { solanaAddresses } from '@/utils/SolanaWalletUtil' import { solanaAddresses } from '@/utils/SolanaWalletUtil'
import { signClient } from '@/utils/WalletConnectUtil' import { signClient } from '@/utils/WalletConnectUtil'
import { Button, Divider, Modal, Text } from '@nextui-org/react' import { Button, Divider, Modal, Text } from '@nextui-org/react'

View File

@ -6,8 +6,8 @@ import RequestModalContainer from '@/components/RequestModalContainer'
import ModalStore from '@/store/ModalStore' import ModalStore from '@/store/ModalStore'
import { approveNearRequest, rejectNearRequest } from '@/utils/NearRequestHandlerUtil' import { approveNearRequest, rejectNearRequest } from '@/utils/NearRequestHandlerUtil'
import { signClient } from '@/utils/WalletConnectUtil' import { signClient } from '@/utils/WalletConnectUtil'
import { NEAR_SIGNING_METHODS } from "@/data/NEARData"; import { NEAR_SIGNING_METHODS } from '@/data/NEARData'
import { transactions } from "near-api-js"; import { transactions } from 'near-api-js'
import { Button, Divider, Modal, Text } from '@nextui-org/react' import { Button, Divider, Modal, Text } from '@nextui-org/react'
import { Fragment } from 'react' import { Fragment } from 'react'
@ -26,76 +26,76 @@ export default function SessionSignNearModal() {
const { request, chainId } = params const { request, chainId } = params
const formatTransaction = (transaction: Uint8Array) => { const formatTransaction = (transaction: Uint8Array) => {
const tx = transactions.Transaction.decode(Buffer.from(transaction)); const tx = transactions.Transaction.decode(Buffer.from(transaction))
return { return {
signerId: tx.signerId, signerId: tx.signerId,
receiverId: tx.receiverId, receiverId: tx.receiverId,
publicKey: tx.publicKey.toString(), publicKey: tx.publicKey.toString(),
actions: tx.actions.map((action) => { actions: tx.actions.map(action => {
switch (action.enum) { switch (action.enum) {
case "createAccount": { case 'createAccount': {
return { return {
type: "CreateAccount", type: 'CreateAccount',
params: action.createAccount params: action.createAccount
}; }
} }
case "deployContract": { case 'deployContract': {
return { return {
type: "DeployContract", type: 'DeployContract',
params: { params: {
...action.deployContract, ...action.deployContract,
args: Buffer.from(action.deployContract.code).toString(), args: Buffer.from(action.deployContract.code).toString()
} }
}; }
} }
case "functionCall": { case 'functionCall': {
return { return {
type: "FunctionCall", type: 'FunctionCall',
params: { params: {
...action.functionCall, ...action.functionCall,
args: JSON.parse(Buffer.from(action.functionCall.args).toString()), args: JSON.parse(Buffer.from(action.functionCall.args).toString())
} }
}; }
} }
case "transfer": { case 'transfer': {
return { return {
type: "Transfer", type: 'Transfer',
params: action.transfer params: action.transfer
}; }
} }
case "stake": { case 'stake': {
return { return {
type: "Stake", type: 'Stake',
params: { params: {
...action.stake, ...action.stake,
publicKey: action.stake.publicKey.toString() publicKey: action.stake.publicKey.toString()
} }
}; }
} }
case "addKey": { case 'addKey': {
return { return {
type: "AddKey", type: 'AddKey',
params: { params: {
...action.addKey, ...action.addKey,
publicKey: action.addKey.publicKey.toString() publicKey: action.addKey.publicKey.toString()
} }
}; }
} }
case "deleteKey": { case 'deleteKey': {
return { return {
type: "DeleteKey", type: 'DeleteKey',
params: { params: {
...action.deleteKey, ...action.deleteKey,
publicKey: action.deleteKey.publicKey.toString() publicKey: action.deleteKey.publicKey.toString()
} }
}; }
} }
case "deleteAccount": { case 'deleteAccount': {
return { return {
type: "DeleteAccount", type: 'DeleteAccount',
params: action.deleteAccount params: action.deleteAccount
}; }
} }
default: default:
return { return {
@ -116,7 +116,7 @@ export default function SessionSignNearModal() {
...params.request, ...params.request,
params: { params: {
...params.request.params, ...params.request.params,
transaction: formatTransaction(params.request.params.transaction), transaction: formatTransaction(params.request.params.transaction)
} }
} }
} }
@ -127,16 +127,15 @@ export default function SessionSignNearModal() {
...params.request, ...params.request,
params: { params: {
...params.request.params, ...params.request.params,
transactions: params.request.params.transactions.map(formatTransaction), transactions: params.request.params.transactions.map(formatTransaction)
} }
} }
} }
default: default:
return params; return params
} }
} }
// Handle approve action (logic varies based on request method) // Handle approve action (logic varies based on request method)
async function onApprove() { async function onApprove() {
if (requestEvent) { if (requestEvent) {
@ -168,10 +167,7 @@ export default function SessionSignNearModal() {
<Divider y={2} /> <Divider y={2} />
<RequestDetailsCard <RequestDetailsCard chains={[chainId ?? '']} protocol={requestSession.relay.protocol} />
chains={[chainId ?? '']}
protocol={requestSession.relay.protocol}
/>
<Divider y={2} /> <Divider y={2} />