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