near-encoding 1.1.7619 → 1.1.9071
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.
- package/README.md +28 -35
- package/index.js +75 -63
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,60 +6,53 @@ Utility functions for encoding, decoding, and interacting with the NEAR blockcha
|
|
|
6
6
|
|
|
7
7
|
npm install near-encoding
|
|
8
8
|
|
|
9
|
-
##
|
|
10
|
-
|
|
11
|
-
### `encodeBase64(input: string): string`
|
|
9
|
+
## Usage
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
### Base64 Encoding / Decoding
|
|
14
12
|
|
|
15
|
-
import { encodeBase64 } from 'near-encoding';
|
|
13
|
+
import { encodeBase64, decodeBase64 } from 'near-encoding';
|
|
16
14
|
|
|
17
15
|
const encoded = encodeBase64('hello world');
|
|
16
|
+
// "aGVsbG8gd29ybGQ="
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
### `decodeBase64(input: string): string`
|
|
22
|
-
|
|
23
|
-
Decodes a Base64 string back to UTF-8.
|
|
24
|
-
|
|
25
|
-
import { decodeBase64 } from 'near-encoding';
|
|
26
|
-
|
|
27
|
-
const decoded = decodeBase64('aGVsbG8gd29ybGQ=');
|
|
18
|
+
const decoded = decodeBase64(encoded);
|
|
19
|
+
// "hello world"
|
|
28
20
|
|
|
29
|
-
|
|
21
|
+
### Encoding / Decoding Contract Arguments
|
|
30
22
|
|
|
31
|
-
|
|
23
|
+
import { encodeArgs, decodeArgs } from 'near-encoding';
|
|
32
24
|
|
|
33
|
-
|
|
25
|
+
const encoded = encodeArgs({ token_id: '1', receiver_id: 'alice.near' });
|
|
26
|
+
// Base64-encoded JSON string
|
|
34
27
|
|
|
35
|
-
|
|
28
|
+
const decoded = decodeArgs(encoded);
|
|
29
|
+
// { token_id: '1', receiver_id: 'alice.near' }
|
|
36
30
|
|
|
37
|
-
|
|
31
|
+
### Generating a Key Pair
|
|
38
32
|
|
|
39
|
-
|
|
33
|
+
import { generateKeyPair } from 'near-encoding';
|
|
40
34
|
|
|
41
|
-
|
|
35
|
+
const keyPair = generateKeyPair();
|
|
36
|
+
// { publicKey: 'ed25519:...', secretKey: 'ed25519:...' }
|
|
42
37
|
|
|
43
|
-
|
|
38
|
+
### Calling a Contract Function
|
|
44
39
|
|
|
45
40
|
import { functionCall } from 'near-encoding';
|
|
46
41
|
|
|
47
|
-
const
|
|
48
|
-
accountId: 'alice.near',
|
|
49
|
-
contractId: 'token.near',
|
|
50
|
-
methodName: 'transfer',
|
|
51
|
-
args: { receiver_id: 'bob.near', amount: '100' },
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
### `generateKeyPair(accountId: string): GeneratedKeyPair`
|
|
42
|
+
const result = await functionCall(options);
|
|
57
43
|
|
|
58
|
-
|
|
44
|
+
`options` accepts a `FunctionCallOptions` object. The function returns a `Promise<FunctionCallResult>`.
|
|
59
45
|
|
|
60
|
-
|
|
46
|
+
## API
|
|
61
47
|
|
|
62
|
-
|
|
48
|
+
| Function | Signature |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `encodeBase64` | `(input: string): string` |
|
|
51
|
+
| `decodeBase64` | `(input: string): string` |
|
|
52
|
+
| `encodeArgs` | `(args: Record<string, unknown>): string` |
|
|
53
|
+
| `decodeArgs` | `(encoded: string): Record<string, unknown>` |
|
|
54
|
+
| `generateKeyPair` | `(): KeyPairResult` |
|
|
55
|
+
| `functionCall` | `(options: FunctionCallOptions): Promise<FunctionCallResult>` |
|
|
63
56
|
|
|
64
57
|
## License
|
|
65
58
|
|
package/index.js
CHANGED
|
@@ -4,102 +4,125 @@ import { KeyPairEd25519 } from 'near-api-js/lib/utils/key_pair';
|
|
|
4
4
|
|
|
5
5
|
const RPC_URL = 'https://rpc.mainnet.near.org';
|
|
6
6
|
|
|
7
|
-
/** Encode a UTF-8 string to a
|
|
7
|
+
/** Encode a UTF-8 string to a Base64 string (NEAR-compatible). */
|
|
8
8
|
export function encodeBase64(input: string): string {
|
|
9
9
|
return Buffer.from(input, 'utf8').toString('base64');
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
/** Decode a
|
|
12
|
+
/** Decode a Base64 string back to a UTF-8 string. */
|
|
13
13
|
export function decodeBase64(input: string): string {
|
|
14
14
|
return Buffer.from(input, 'base64').toString('utf8');
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
/** Encode
|
|
18
|
-
export function encodeArgs(args: Record<string, unknown>):
|
|
19
|
-
return
|
|
17
|
+
/** Encode a plain object to a JSON-then-Base64 string suitable for NEAR function-call args. */
|
|
18
|
+
export function encodeArgs(args: Record<string, unknown>): string {
|
|
19
|
+
return encodeBase64(JSON.stringify(args));
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
/** Decode a
|
|
23
|
-
export function
|
|
24
|
-
|
|
25
|
-
return JSON.parse(json) as T;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface ViewCallOptions {
|
|
29
|
-
contractId: string;
|
|
30
|
-
methodName: string;
|
|
31
|
-
args?: Record<string, unknown>;
|
|
22
|
+
/** Decode a Base64 function-call args string back to a plain object. */
|
|
23
|
+
export function decodeArgs(encoded: string): Record<string, unknown> {
|
|
24
|
+
return JSON.parse(decodeBase64(encoded)) as Record<string, unknown>;
|
|
32
25
|
}
|
|
33
26
|
|
|
34
27
|
export interface ViewCallResult<T = unknown> {
|
|
35
28
|
result: T;
|
|
36
|
-
|
|
29
|
+
blockHash: string;
|
|
37
30
|
}
|
|
38
31
|
|
|
39
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* Call a NEAR view function and return the decoded result.
|
|
34
|
+
* @param contractId The NEAR account ID of the contract.
|
|
35
|
+
* @param methodName The view method to call.
|
|
36
|
+
* @param args Arguments to pass (will be Base64-encoded automatically).
|
|
37
|
+
*/
|
|
40
38
|
export async function viewCall<T = unknown>(
|
|
41
|
-
|
|
39
|
+
contractId: string,
|
|
40
|
+
methodName: string,
|
|
41
|
+
args: Record<string, unknown> = {}
|
|
42
42
|
): Promise<ViewCallResult<T>> {
|
|
43
|
-
const
|
|
44
|
-
|
|
43
|
+
const encodedArgs = encodeArgs(args);
|
|
44
|
+
|
|
45
|
+
const body = {
|
|
46
|
+
jsonrpc: '2.0',
|
|
47
|
+
id: 'near-encoding',
|
|
48
|
+
method: 'query',
|
|
49
|
+
params: {
|
|
50
|
+
request_type: 'call_function',
|
|
51
|
+
finality: 'final',
|
|
52
|
+
account_id: contractId,
|
|
53
|
+
method_name: methodName,
|
|
54
|
+
args_base64: encodedArgs,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
45
57
|
|
|
46
58
|
const response = await fetch(RPC_URL, {
|
|
47
59
|
method: 'POST',
|
|
48
60
|
headers: { 'Content-Type': 'application/json' },
|
|
49
|
-
body: JSON.stringify(
|
|
50
|
-
jsonrpc: '2.0',
|
|
51
|
-
id: 'near-encoding',
|
|
52
|
-
method: 'query',
|
|
53
|
-
params: {
|
|
54
|
-
request_type: 'call_function',
|
|
55
|
-
finality: 'final',
|
|
56
|
-
account_id: contractId,
|
|
57
|
-
method_name: methodName,
|
|
58
|
-
args_base64: argsBase64,
|
|
59
|
-
},
|
|
60
|
-
}),
|
|
61
|
+
body: JSON.stringify(body),
|
|
61
62
|
});
|
|
62
63
|
|
|
63
64
|
if (!response.ok) {
|
|
64
65
|
throw new Error(`RPC HTTP error: ${response.status}`);
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
const json = await response.json()
|
|
68
|
+
const json = (await response.json()) as {
|
|
69
|
+
result?: { result: number[]; block_hash: string };
|
|
70
|
+
error?: { message: string };
|
|
71
|
+
};
|
|
68
72
|
|
|
69
|
-
if (json.error) {
|
|
70
|
-
|
|
71
|
-
}
|
|
73
|
+
if (json.error) throw new Error(`RPC error: ${json.error.message}`);
|
|
74
|
+
if (!json.result) throw new Error('Empty RPC result');
|
|
72
75
|
|
|
73
|
-
const raw
|
|
74
|
-
|
|
75
|
-
'binary' as BufferEncoding
|
|
76
|
-
)
|
|
77
|
-
.toString('base64');
|
|
76
|
+
const raw = Buffer.from(json.result.result).toString('utf8');
|
|
77
|
+
const decoded = JSON.parse(raw) as T;
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
return { result: decoded, blockHash: json.result.block_hash };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface KeyPairResult {
|
|
83
|
+
publicKey: string;
|
|
84
|
+
secretKey: string;
|
|
85
|
+
keyPair: KeyPair;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Generate a new random Ed25519 key pair for use with NEAR. */
|
|
89
|
+
export function generateKeyPair(): KeyPairResult {
|
|
90
|
+
const keyPair = KeyPairEd25519.fromRandom();
|
|
91
|
+
return {
|
|
92
|
+
publicKey: keyPair.getPublicKey().toString(),
|
|
93
|
+
secretKey: keyPair.toString(),
|
|
94
|
+
keyPair,
|
|
95
|
+
};
|
|
81
96
|
}
|
|
82
97
|
|
|
83
98
|
export interface FunctionCallOptions {
|
|
84
99
|
accountId: string;
|
|
85
100
|
contractId: string;
|
|
86
101
|
methodName: string;
|
|
87
|
-
args
|
|
102
|
+
args: Record<string, unknown>;
|
|
88
103
|
gas?: string;
|
|
89
104
|
attachedDeposit?: string;
|
|
90
|
-
keyPair?:
|
|
105
|
+
keyPair?: KeyPair;
|
|
91
106
|
networkId?: string;
|
|
92
107
|
}
|
|
93
108
|
|
|
94
|
-
|
|
109
|
+
export interface FunctionCallResult {
|
|
110
|
+
transactionHash: string;
|
|
111
|
+
status: unknown;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Sign and send a NEAR function call using near-api-js.
|
|
116
|
+
* Uses the provided keyPair (from generateKeyPair) for signing.
|
|
117
|
+
*/
|
|
95
118
|
export async function functionCall(
|
|
96
119
|
options: FunctionCallOptions
|
|
97
|
-
): Promise<
|
|
120
|
+
): Promise<FunctionCallResult> {
|
|
98
121
|
const {
|
|
99
122
|
accountId,
|
|
100
123
|
contractId,
|
|
101
124
|
methodName,
|
|
102
|
-
args
|
|
125
|
+
args,
|
|
103
126
|
gas = '30000000000000',
|
|
104
127
|
attachedDeposit = '0',
|
|
105
128
|
keyPair,
|
|
@@ -120,27 +143,16 @@ export async function functionCall(
|
|
|
120
143
|
|
|
121
144
|
const account = await near.account(accountId);
|
|
122
145
|
|
|
123
|
-
|
|
146
|
+
const outcome = await account.functionCall({
|
|
124
147
|
contractId,
|
|
125
148
|
methodName,
|
|
126
|
-
args
|
|
149
|
+
args,
|
|
127
150
|
gas: BigInt(gas),
|
|
128
151
|
attachedDeposit: BigInt(attachedDeposit),
|
|
129
152
|
});
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export interface GeneratedKeyPair {
|
|
133
|
-
accountId: string;
|
|
134
|
-
publicKey: string;
|
|
135
|
-
keyPair: nearApiJs.KeyPair;
|
|
136
|
-
}
|
|
137
153
|
|
|
138
|
-
/** Generate a new ed25519 KeyPair for a given accountId (no private key param accepted) */
|
|
139
|
-
export function generateKeyPair(accountId: string): GeneratedKeyPair {
|
|
140
|
-
const keyPair = KeyPairEd25519.fromRandom();
|
|
141
154
|
return {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
keyPair,
|
|
155
|
+
transactionHash: outcome.transaction.hash,
|
|
156
|
+
status: outcome.status,
|
|
145
157
|
};
|
|
146
158
|
}
|