# 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 - Recommended
  • window.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>
1
2
3
4
5
6

# Only get the accountId

try {
  const { accountId } = window.safepalwallet.near.requestSignIn();
} catch (_) {
  // something error
}
1
2
3
4
5

Example of return value:

{
  "accountId": "388246...5594",
}
1
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;
1

# isSignedIn()

Check whether the current account has connected

window.safepalwallet.near.isSignedIn(): boolean;
1

# getAccountId()

Get the accountId of current connected

window.safepalwallet.near.getAccountId(): string;
1

# signMessage

near.signMessage({ message: string, recipient: string, nonce: Buffer }): Response;
1

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);
1
2
3
4
5
6
7

Example of return value:

{
    "accountId": "388246...5594",
    "publicKey": "ed25519:4ob3hL...vf5nK",
    "signature": "mK3vN8+XpqLwZcHj2QRsTdF6PnMx/tYgKbCoUjWiEa7vB4reFzGhVtNmLkPqS/OwJxHnVcMpLr9Qy3uTkZwfAg=="
}
1
2
3
4
5

# Contract interaction

# signAndSendTransaction()

near.signAndSendTransaction({ receiverId: string, actions: Action[]}): Response;
1

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);
1
2
3
4
5
6
7
8
9
10
11
12

Example of return value:

{
    "method": "signAndSendTransaction",
    "txHash": "9kXm7P...3zRq",
    "code": 0
}
1
2
3
4
5

Note: dapp needs to obtain the result of transaction broadcast through txHash

# requestSignTransactions()

near.requestSignTransactions({transactions: Transaction[]}): Response;
1

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 });
1
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"
}
1
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
});
1
2
3

# signOut

The SafePal wallet has disconnected

window.safepalwallet.near.on("signOut", (() => {
  // do something
});
1
2
3

# accountChanged

Listen to the current account changed

window.safepalwallet.near.on("accountChanged", ((accountId) => {
  // accountId: the accountId after change
});
1
2
3