Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump near-api-js to latest version for NEAR Protocol #702

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions advanced/wallets/react-wallet-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@mui/material": "^5.14.10",
"@multiversx/sdk-core": "12.18.0",
"@multiversx/sdk-wallet": "4.2.0",
"@near-wallet-selector/wallet-utils": "^8.0.0",
"@near-wallet-selector/wallet-utils": "^8.9.12",
"@nextui-org/react": "1.0.8-beta.5",
"@polkadot/keyring": "^10.1.2",
"@polkadot/types": "^9.3.3",
Expand All @@ -46,7 +46,7 @@
"ethers": "5.7.2",
"framer-motion": "6.5.1",
"graphql": "^16.8.2",
"near-api-js": "^0.45.0",
"near-api-js": "^5.0.0",
"next": "12.1.5",
"permissionless": "0.1.43",
"react": "17.0.2",
Expand Down
18 changes: 13 additions & 5 deletions advanced/wallets/react-wallet-v2/src/lib/NearLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class NearWallet {
const randomNumber = Math.floor(
Math.random() * (99999999999999 - 10000000000000) + 10000000000000
)
const accountId = `dev-${Date.now()}-${randomNumber}`
const accountId = `dev-${Date.now()}-${randomNumber}.testnet`
const publicKey = keyPair.getPublicKey().toString()

fetch(`https://helper.testnet.near.org/account`, {
Expand Down Expand Up @@ -204,7 +204,9 @@ export class NearWallet {
chainId,
transactions
}: CreateTransactionsParams): Promise<Array<nearTransactions.Transaction>> {
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc)
const provider = new providers.JsonRpcProvider({
url: NEAR_TEST_CHAINS[chainId as TNearChain].rpc
})
const txs: Array<nearTransactions.Transaction> = []

const [block, accounts] = await Promise.all([
Expand All @@ -227,12 +229,14 @@ export class NearWallet {
public_key: account.publicKey
})

const nonce = BigInt(accessKey.nonce) + BigInt(i) + 1n

txs.push(
nearTransactions.createTransaction(
transaction.signerId,
utils.PublicKey.from(account.publicKey),
transaction.receiverId,
accessKey.nonce + i + 1,
nonce,
transaction.actions,
utils.serialize.base_decode(block.header.hash)
)
Expand Down Expand Up @@ -368,7 +372,9 @@ export class NearWallet {
topic,
transaction
}: SignAndSendTransactionParams): Promise<providers.FinalExecutionOutcome> {
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc)
const provider = new providers.JsonRpcProvider({
url: NEAR_TEST_CHAINS[chainId as TNearChain].rpc
})
const [signedTx] = await this.signTransactions({
chainId,
topic,
Expand All @@ -383,7 +389,9 @@ export class NearWallet {
topic,
transactions
}: SignAndSendTransactionsParams): Promise<Array<providers.FinalExecutionOutcome>> {
const provider = new providers.JsonRpcProvider(NEAR_TEST_CHAINS[chainId as TNearChain].rpc)
const provider = new providers.JsonRpcProvider({
url: NEAR_TEST_CHAINS[chainId as TNearChain].rpc
})
const signedTxs = await this.signTransactions({ chainId, topic, transactions })
const results: Array<providers.FinalExecutionOutcome> = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,20 @@ export async function approveNearRequest(
throw new Error('Invalid chain id')
}

const isBuffer = request.params.transaction?.type === 'Buffer'
const txValue = isBuffer
? request.params.transaction
: Object.values(request.params.transaction)

const [signedTx] = await nearWallet.signTransactions({
chainId,
topic,
transactions: [transactions.Transaction.decode(Buffer.from(request.params.transaction))]
transactions: [transactions.Transaction.decode(Buffer.from(txValue))]
})

return formatJsonRpcResult(id, signedTx.encode())
const txResultValue = isBuffer ? Buffer.from(signedTx.encode()) : signedTx.encode()

return formatJsonRpcResult(id, txResultValue)
}
case NEAR_SIGNING_METHODS.NEAR_SIGN_AND_SEND_TRANSACTION: {
console.log('approve', { id, params })
Expand Down Expand Up @@ -103,17 +110,23 @@ export async function approveNearRequest(
throw new Error('Invalid chain id')
}

const isBuffer = params.request.params.transactions[0]?.type === 'Buffer'

const signedTxs = await nearWallet.signTransactions({
chainId,
topic,
transactions: params.request.params.transactions.map((tx: Uint8Array) => {
return transactions.Transaction.decode(Buffer.from(tx))
// @ts-ignore
const txValue = isBuffer ? tx : Object.values(tx)
return transactions.Transaction.decode(Buffer.from(txValue))
})
})

return formatJsonRpcResult(
id,
signedTxs.map(x => x.encode())
signedTxs.map(x => {
return isBuffer ? Buffer.from(x.encode()) : x.encode()
})
)
}
case NEAR_SIGNING_METHODS.NEAR_VERIFY_OWNER: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default function SessionSignNearModal() {
const { request, chainId } = params

const formatTransaction = (transaction: Uint8Array) => {
const tx = transactions.Transaction.decode(Buffer.from(transaction))
// @ts-ignore
const txValue = transaction?.type === 'Buffer' ? transaction : Object.values(transaction)
const tx = transactions.Transaction.decode(Buffer.from(txValue))

return {
signerId: tx.signerId,
Expand All @@ -49,7 +51,7 @@ export default function SessionSignNearModal() {
type: 'DeployContract',
params: {
...action.deployContract,
args: Buffer.from(action.deployContract.code).toString()
args: Buffer.from(action.deployContract!.code).toString()
}
}
}
Expand All @@ -58,7 +60,7 @@ export default function SessionSignNearModal() {
type: 'FunctionCall',
params: {
...action.functionCall,
args: JSON.parse(Buffer.from(action.functionCall.args).toString())
args: JSON.parse(Buffer.from(action.functionCall!.args).toString())
}
}
}
Expand All @@ -73,7 +75,7 @@ export default function SessionSignNearModal() {
type: 'Stake',
params: {
...action.stake,
publicKey: action.stake.publicKey.toString()
publicKey: action.stake!.publicKey.toString()
}
}
}
Expand All @@ -82,7 +84,7 @@ export default function SessionSignNearModal() {
type: 'AddKey',
params: {
...action.addKey,
publicKey: action.addKey.publicKey.toString()
publicKey: action.addKey!.publicKey.toString()
}
}
}
Expand All @@ -91,7 +93,7 @@ export default function SessionSignNearModal() {
type: 'DeleteKey',
params: {
...action.deleteKey,
publicKey: action.deleteKey.publicKey.toString()
publicKey: action.deleteKey!.publicKey.toString()
}
}
}
Expand Down
Loading