near-account-utils-sdk 1.0.7855

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +65 -0
  2. package/index.js +121 -0
  3. package/package.json +29 -0
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # near-account-utils
2
+
3
+ Utility functions for interacting with NEAR Protocol accounts and smart contracts.
4
+
5
+ ## Installation
6
+
7
+ npm install near-account-utils
8
+
9
+ ## API & Usage
10
+
11
+ ### `getAccountInfo(accountId)`
12
+
13
+ Fetch on-chain account details.
14
+
15
+ import { getAccountInfo } from 'near-account-utils';
16
+
17
+ const info = await getAccountInfo('alice.near');
18
+ // { amount, code_hash, storage_usage, block_height }
19
+
20
+ ### `accountExists(accountId)`
21
+
22
+ Check whether an account exists on-chain.
23
+
24
+ import { accountExists } from 'near-account-utils';
25
+
26
+ const exists = await accountExists('alice.near'); // true | false
27
+
28
+ ### `generateKeyPair()`
29
+
30
+ Generate a new NEAR key pair and its derived implicit account ID.
31
+
32
+ import { generateKeyPair } from 'near-account-utils';
33
+
34
+ const { publicKey, secretKey, implicitAccountId } = await generateKeyPair();
35
+
36
+ ### `viewFunction(contractId, methodName, args)`
37
+
38
+ Call a read-only contract method.
39
+
40
+ import { viewFunction } from 'near-account-utils';
41
+
42
+ const result = await viewFunction('contract.near', 'get_balance', { account_id: 'alice.near' });
43
+
44
+ ### `callFunction(signerAccountId, contractId, methodName, args, options)`
45
+
46
+ Execute a state-changing contract method.
47
+
48
+ import { callFunction } from 'near-account-utils';
49
+
50
+ const outcome = await callFunction(
51
+ 'alice.near',
52
+ 'contract.near',
53
+ 'transfer',
54
+ { receiver_id: 'bob.near', amount: '100' },
55
+ {
56
+ keyPair: myKeyPair,
57
+ attachedDeposit: '1000000000000000000000000',
58
+ gas: '30000000000000',
59
+ networkId: 'mainnet',
60
+ }
61
+ );
62
+
63
+ ## License
64
+
65
+ MIT
package/index.js ADDED
@@ -0,0 +1,121 @@
1
+ import * as nearApiJs from 'near-api-js';
2
+
3
+ const RPC_URL = 'https://rpc.mainnet.near.org';
4
+
5
+ /** Fetches basic account info (balance, code_hash, storage) for a NEAR account */
6
+ export async function getAccountInfo(accountId: string): Promise<{
7
+ amount: string;
8
+ code_hash: string;
9
+ storage_usage: number;
10
+ block_height: number;
11
+ }> {
12
+ const response = await fetch(RPC_URL, {
13
+ method: 'POST',
14
+ headers: { 'Content-Type': 'application/json' },
15
+ body: JSON.stringify({
16
+ jsonrpc: '2.0',
17
+ id: 'near-account-utils',
18
+ method: 'query',
19
+ params: {
20
+ request_type: 'view_account',
21
+ finality: 'final',
22
+ account_id: accountId,
23
+ },
24
+ }),
25
+ });
26
+ const data = await response.json();
27
+ if (data.error) throw new Error(data.error.data ?? data.error.message);
28
+ return {
29
+ amount: data.result.amount,
30
+ code_hash: data.result.code_hash,
31
+ storage_usage: data.result.storage_usage,
32
+ block_height: data.result.block_height,
33
+ };
34
+ }
35
+
36
+ /** Checks whether a NEAR account exists on-chain */
37
+ export async function accountExists(accountId: string): Promise<boolean> {
38
+ try {
39
+ await getAccountInfo(accountId);
40
+ return true;
41
+ } catch {
42
+ return false;
43
+ }
44
+ }
45
+
46
+ /** Generates a new random ed25519 keypair; returns publicKey, secretKey, and an implicit accountId */
47
+ export async function generateKeyPair(): Promise<{
48
+ publicKey: string;
49
+ secretKey: string;
50
+ implicitAccountId: string;
51
+ }> {
52
+ const keyPair = nearApiJs.KeyPair.fromRandom('ed25519');
53
+ const publicKey = keyPair.getPublicKey().toString();
54
+ const secretKey = (keyPair as nearApiJs.KeyPairEd25519).secretKey ?? keyPair.toString();
55
+ // Implicit account id = hex of the raw public key bytes
56
+ const rawBytes = keyPair.getPublicKey().data;
57
+ const implicitAccountId = Buffer.from(rawBytes).toString('hex');
58
+ return { publicKey, secretKey, implicitAccountId };
59
+ }
60
+
61
+ /** Calls a read-only (view) function on a NEAR smart contract */
62
+ export async function viewFunction(
63
+ contractId: string,
64
+ methodName: string,
65
+ args: Record<string, unknown> = {}
66
+ ): Promise<unknown> {
67
+ const argsBase64 = Buffer.from(JSON.stringify(args)).toString('base64');
68
+ const response = await fetch(RPC_URL, {
69
+ method: 'POST',
70
+ headers: { 'Content-Type': 'application/json' },
71
+ body: JSON.stringify({
72
+ jsonrpc: '2.0',
73
+ id: 'near-account-utils',
74
+ method: 'query',
75
+ params: {
76
+ request_type: 'call_function',
77
+ finality: 'final',
78
+ account_id: contractId,
79
+ method_name: methodName,
80
+ args_base64: argsBase64,
81
+ },
82
+ }),
83
+ });
84
+ const data = await response.json();
85
+ if (data.error) throw new Error(data.error.data ?? data.error.message);
86
+ const resultBytes: number[] = data.result.result;
87
+ return JSON.parse(Buffer.from(resultBytes).toString('utf8'));
88
+ }
89
+
90
+ /** Calls a state-changing function on a NEAR contract using near-api-js Account */
91
+ export async function callFunction(
92
+ signerAccountId: string,
93
+ contractId: string,
94
+ methodName: string,
95
+ args: Record<string, unknown>,
96
+ options: {
97
+ keyPair: nearApiJs.KeyPair;
98
+ attachedDeposit?: string;
99
+ gas?: string;
100
+ networkId?: string;
101
+ }
102
+ ): Promise<nearApiJs.providers.FinalExecutionOutcome> {
103
+ const networkId = options.networkId ?? 'mainnet';
104
+ const keyStore = new nearApiJs.keyStores.InMemoryKeyStore();
105
+ await keyStore.setKey(networkId, signerAccountId, options.keyPair);
106
+
107
+ const near = await nearApiJs.connect({
108
+ networkId,
109
+ keyStore,
110
+ nodeUrl: RPC_URL,
111
+ });
112
+
113
+ const account = await near.account(signerAccountId);
114
+ return account.functionCall({
115
+ contractId,
116
+ methodName,
117
+ args,
118
+ gas: BigInt(options.gas ?? '30000000000000'),
119
+ attachedDeposit: BigInt(options.attachedDeposit ?? '0'),
120
+ });
121
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "near-account-utils-sdk",
3
+ "version": "1.0.7855",
4
+ "description": "npm Package - near-account-utils",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "jest --passWithNoTests"
8
+ },
9
+ "keywords": [
10
+ "near",
11
+ "blockchain",
12
+ "web3"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "near-api-js": "^4.0.0"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.0.0",
20
+ "@types/node": "^20.0.0",
21
+ "jest": "^29.0.0",
22
+ "@types/jest": "^29.0.0",
23
+ "ts-jest": "^29.0.0"
24
+ },
25
+ "files": [
26
+ "dist/**/*",
27
+ "README.md"
28
+ ]
29
+ }