# Near
This feature is supported by the following versions of SafePal Wallet.
| platform | version | description |
|---|---|---|
| App(iOS) | >=4.10.2 | Mainnet |
| App(Android) | >=4.10.2 | Mainnet |
| Chrome Extension | >=2.33.0 | Mainnet |
# What is injected provider API?
The SafePal injected provider API is a JavaScript API that SafePal injects into websites visited by our users. Your DApp can use this API to request users' accounts, read data from blockchains users are connected to, and help users sign messages and transactions.
# The injected object
Dapps can access the injected object through the following methods:
window.safepalwallet.near- Recommendedwindow.near
# Connecting to SafePal Wallet
# requestSignIn()
/**
* @param {String} contractId contract account id
* @param {Array} methodNames methods on the contract should be allowed to be called.
* @returns { accountId, accessKey } accountId and signed in access key
*/
window.safepalwallet.near.requestSignIn(): Promise<Result>
2
3
4
5
6
# Only get the accountId
try {
const { accountId } = window.safepalwallet.near.requestSignIn();
} catch (_) {
// something error
}
2
3
4
5
Example of return value:
{
"accountId": "388246...5594",
}
2
3
# signOut()
Disconnect to the wallet. However, disconnection can also be initiated by the wallet in addition to the application.
window.safepalwallet.near.signOut(): void;
# isSignedIn()
Check whether the current account has connected
window.safepalwallet.near.isSignedIn(): boolean;
# getAccountId()
Get the accountId of current connected
window.safepalwallet.near.getAccountId(): string;
# signMessage
near.signMessage({ message: string, recipient: string, nonce: Buffer }): Response;
signMessage, demo:
const message = {
message: 'Sign this message to verify your identity',
recipient: 'safepal.near',
nonce: Buffer.from("a7f3e2d1c8b9a5f4e3d2c1b0a9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c3b2a1f0", "hex")
}
const result = await window.safepalwallet.near.signMessage(message);
2
3
4
5
6
7
Example of return value:
{
"accountId": "388246...5594",
"publicKey": "ed25519:4ob3hL...vf5nK",
"signature": "mK3vN8+XpqLwZcHj2QRsTdF6PnMx/tYgKbCoUjWiEa7vB4reFzGhVtNmLkPqS/OwJxHnVcMpLr9Qy3uTkZwfAg=="
}
2
3
4
5
# Contract interaction
# signAndSendTransaction()
near.signAndSendTransaction({ receiverId: string, actions: Action[]}): Response;
sign and send one single transaction, demo:
const tx = {
receiverId: 'wrap.near',
actions: [
{
methodName: 'ft_transfer',
args: {},
deposit: '5000000000000000000000',
},
],
}
const result = await window.safepalwallet.near.signAndSendTransaction(tx);
2
3
4
5
6
7
8
9
10
11
12
Example of return value:
{
"method": "signAndSendTransaction",
"txHash": "9kXm7P...3zRq",
"code": 0
}
2
3
4
5
Note: dapp needs to obtain the result of transaction broadcast through txHash
# requestSignTransactions()
near.requestSignTransactions({transactions: Transaction[]}): Response;
Batch sign transactions, demo:
const transactions = [
{
receiverId: 'wrap.near',
actions: [
{
methodName: 'near_deposit',
args: {},
deposit: '1000000000000000',
},
],
},
{
receiverId: 'wrap.near',
actions: [
{
methodName: 'ft_transfer',
args: {
receiver_id: '388246...5594',
amount: '10000000000',
},
deposit: '1',
},
],
},
]
const result = await window.safepalwallet.near.signAndSendTransaction({ transactions });
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Example of return value:
{
"txs": [
{
"signedTx": "5TyNmK...8pWz",
"txHash": "BvH2Qc...7nXj"
},
{
"signedTx": "7RfLpJ...3qYm",
"txHash": "DkZ9Vt...5wBh"
}
],
"code": 0,
"method": "requestSignTransactions"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
Note: Batch signature transaction wallets only sign and do not broadcast. The DAPP side needs to handle the logic of broadcasting.
# Events
# signIn
The SafePal wallet has connected
window.safepalwallet.near.on("signIn", ((accountId) => {
// accountId: current connected accountId
});
2
3
# signOut
The SafePal wallet has disconnected
window.safepalwallet.near.on("signOut", (() => {
// do something
});
2
3
# accountChanged
Listen to the current account changed
window.safepalwallet.near.on("accountChanged", ((accountId) => {
// accountId: the accountId after change
});
2
3