|
| 1 | +# @near-js/client |
| 2 | + |
| 3 | +This package provides a simple interface for interacting with the Near blockchain. As a modern, tree-shakeable package, |
| 4 | +it is intended to replace usage of `near-api-js`. |
| 5 | + |
| 6 | +### Installation |
| 7 | +```shell |
| 8 | +# npm |
| 9 | +npm i -s @near-js/client |
| 10 | + |
| 11 | +# pnpm |
| 12 | +pnpm add @near-js/client |
| 13 | +``` |
| 14 | + |
| 15 | +### Usage |
| 16 | + |
| 17 | + |
| 18 | +### Dependency Initialization |
| 19 | +This package uses a common interface for specifying dependencies for RPC providers and cryptographic signing, allowing |
| 20 | +a more flexible approach to composing functionality. |
| 21 | + |
| 22 | +#### RPC provider |
| 23 | +RPC providers are required for any blockchain interaction, e.g. block queries, transaction publishing. |
| 24 | + |
| 25 | +This interface makes use of the `FallbackRpcProvider`, making it configurable with multiple RPC endpoint URLs |
| 26 | +which can be cycled through when the current URL returns an error. |
| 27 | + |
| 28 | +Specifying by URL endpoints: |
| 29 | +```ts |
| 30 | +import { getProviderByEndpoints } from '@near-js/client'; |
| 31 | + |
| 32 | +const rpcProvider = getProviderByEndpoints('https://rpc.tld', 'https://fallback-rpc.tld'); |
| 33 | +``` |
| 34 | + |
| 35 | +Specifying by network (uses default RPC endpoints specified in code): |
| 36 | +```ts |
| 37 | +import { getProviderByNetwork } from '@near-js/client'; |
| 38 | + |
| 39 | +const rpcProvider = getProviderByNetwork('testnet'); |
| 40 | +``` |
| 41 | + |
| 42 | +Shortcut methods: |
| 43 | +```ts |
| 44 | +import { getTestnetRpcProvider } from '@near-js/client'; |
| 45 | + |
| 46 | +// equivalent to getProviderByNetwork('testnet'); |
| 47 | +const rpcProvider = getTestnetRpcProvider(); |
| 48 | +``` |
| 49 | + |
| 50 | +#### Message Signer |
| 51 | +Implementers of the `MessageSigner` interface can be initialized from the existing `KeyStore` classes or using private keys. |
| 52 | + |
| 53 | +Using an existing keystore: |
| 54 | +```ts |
| 55 | +import { getSignerFromKeystore } from '@near-js/client'; |
| 56 | +import { BrowserLocalStorageKeyStore } from '@near-js/keystores-browser'; |
| 57 | + |
| 58 | +const keystore = new BrowserLocalStorageKeyStore(); |
| 59 | +const signer = getSignerFromKeystore('account.near', 'mainnet', keystore); |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +Using a private key string: |
| 64 | +```ts |
| 65 | +import { getSignerFromKeystore } from '@near-js/client'; |
| 66 | +import { BrowserLocalStorageKeyStore } from '@near-js/keystores-browser'; |
| 67 | + |
| 68 | +const signer = getSignerFromPrivateKey('ed25519:...'); |
| 69 | +``` |
| 70 | + |
| 71 | +An access key-based signer is also available. Initialized using a `MessageSigner` implementer, this signer caches the |
| 72 | +access key record and maintains a local, auto-incremented value for its nonce: |
| 73 | +```ts |
| 74 | +import { getAccessKeySigner, getMainnetRpcProvider, getSignerFromPrivateKey } from '@near-js/client'; |
| 75 | +import { BrowserLocalStorageKeyStore } from '@near-js/keystores-browser'; |
| 76 | + |
| 77 | +const keystore = new BrowserLocalStorageKeyStore(); |
| 78 | +const signer = getSignerFromKeystore('account.near', 'mainnet', keystore); |
| 79 | +const accessKeySigner = getAccessKeySigner({ |
| 80 | + accout: 'account.near', |
| 81 | + deps: { |
| 82 | + signer, |
| 83 | + rpcProvider: getMainnetRpcProvider(), |
| 84 | + }, |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +### View Methods |
| 89 | +Several functions are provided for working with builtin account methods and smart contract view methods. |
| 90 | + |
| 91 | +Executing a view method on a smart contract and return the entire response: |
| 92 | +```ts |
| 93 | +import { callViewMethod, getTestnetRpcProvider } from '@near-js/client'; |
| 94 | + |
| 95 | +const response = await callViewMethod({ |
| 96 | + account: 'guest-book.testnet', |
| 97 | + method: 'getMessages', |
| 98 | + deps: { rpcProvider: getTestnetRpcProvider() }, |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +Shorthand function to call the method and return only the parsed value: |
| 103 | +```ts |
| 104 | +import { getTestnetRpcProvider, view } from '@near-js/client'; |
| 105 | + |
| 106 | +interface GuestBookMessage { |
| 107 | + premium: boolean; |
| 108 | + sender: string; |
| 109 | + text: string; |
| 110 | +} |
| 111 | + |
| 112 | +// parse the returned buffer and parse as JSON |
| 113 | +const data = await view<GuestBookMessage[]>({ |
| 114 | + account: 'guest-book.testnet', |
| 115 | + method: 'getMessages', |
| 116 | + deps: { rpcProvider: getTestnetRpcProvider() }, |
| 117 | +}); |
| 118 | +``` |
| 119 | + |
| 120 | +Client method for requesting access keys: |
| 121 | +```ts |
| 122 | +import { getAccessKeys, getTestnetRpcProvider } from '@near-js/client'; |
| 123 | + |
| 124 | +const { fullAccessKeys, functionCallAccessKeys } = await getAccessKeys({ |
| 125 | + account: 'account.testnet', |
| 126 | + deps: { rpcProvider: getTestnetRpcProvider() }, |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +### Transactions |
| 131 | +New `*Composer` classes facilitate transaction building and signing: |
| 132 | +```ts |
| 133 | +import { |
| 134 | + getSignerFromKeystore, |
| 135 | + getTestnetRpcProvider, |
| 136 | + SignedTransactionComposer, |
| 137 | +} from '@near-js/client'; |
| 138 | +import { KeyPairEd25519 } from '@near-js/crypto'; |
| 139 | +import { BrowserLocalStorageKeyStore } from '@near-js/keystores-browser'; |
| 140 | + |
| 141 | +const keystore = new BrowserLocalStorageKeyStore(); |
| 142 | +const signer = getSignerFromKeystore('account.testnet', 'testnet', keystore); |
| 143 | + |
| 144 | +const oldPublicKey = await signer.getPublicKey(); |
| 145 | +const newKeyPair = KeyPairEd25519.fromRandom(); |
| 146 | + |
| 147 | +const composer = SignedTransactionComposer.init({ |
| 148 | + sender: 'account.testnet', |
| 149 | + receiver: 'receiver.testnet', |
| 150 | + deps: { |
| 151 | + signer, |
| 152 | + rpcProvider: getMainnetRpcProvider(), |
| 153 | + } |
| 154 | +}); |
| 155 | + |
| 156 | +// add new key and remove old key in single transaction |
| 157 | +await composer |
| 158 | + .addFullAccessKey(newKeyPair.publicKey) |
| 159 | + .deleteKey(oldPublicKey.toString()) |
| 160 | + .signAndSend(); |
| 161 | + |
| 162 | +keystore.setKey('testnet', 'account.testnet', newKeyPair); |
| 163 | +``` |
| 164 | + |
| 165 | +For convenience, there are also functions wrapping individual actions, e.g. `transfer`: |
| 166 | +```ts |
| 167 | +import { |
| 168 | + getSignerFromKeystore, |
| 169 | + getTestnetRpcProvider, |
| 170 | + transfer, |
| 171 | +} from '@near-js/client'; |
| 172 | +import { KeyPairEd25519 } from '@near-js/crypto'; |
| 173 | +import { BrowserLocalStorageKeyStore } from '@near-js/keystores-browser'; |
| 174 | + |
| 175 | +const keystore = new BrowserLocalStorageKeyStore(); |
| 176 | +const signer = getSignerFromKeystore('account.testnet', 'testnet', keystore); |
| 177 | + |
| 178 | +await transfer({ |
| 179 | + sender: 'account.testnet', |
| 180 | + receiver: 'receiver.testnet', |
| 181 | + amount: 1000n, // in yoctoNear |
| 182 | + deps: { |
| 183 | + rpcProvider: getTestnetRpcProvider(), |
| 184 | + signer, |
| 185 | + } |
| 186 | +}); |
| 187 | +``` |
| 188 | + |
| 189 | +# License |
| 190 | + |
| 191 | +This repository is distributed under the terms of both the MIT license and the Apache License (Version 2.0). |
| 192 | +See [LICENSE](https://github.com/near/near-api-js/blob/master/LICENSE) and [LICENSE-APACHE](https://github.com/near/near-api-js/blob/master/LICENSE-APACHE) for details. |
0 commit comments