near-api-js 7.1.0 → 7.2.0
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/lib/accounts/account.d.ts +1 -1
- package/lib/accounts/account.js +11 -2
- package/lib/crypto/key_pair_ed25519.js +1 -2
- package/lib/crypto/key_pair_secp256k1.js +13 -10
- package/lib/crypto/public_key.js +24 -8
- package/lib/providers/json-rpc-provider.d.ts +2 -2
- package/lib/providers/json-rpc-provider.js +9 -9
- package/lib/tokens/ft/index.d.ts +17 -1
- package/lib/tokens/ft/index.js +33 -7
- package/lib/transactions/action_creators.d.ts +3 -3
- package/lib/transactions/action_creators.js +7 -5
- package/lib/transactions/schema.d.ts +1 -1
- package/lib/transactions/schema.js +3 -2
- package/lib/utils/format.d.ts +24 -0
- package/lib/utils/format.js +35 -1
- package/lib/utils/provider.js +2 -1
- package/package.json +2 -3
|
@@ -3,7 +3,7 @@ import { type Provider } from '../providers/index.js';
|
|
|
3
3
|
import type { RpcTransactionResponse } from '../rpc/types.gen.js';
|
|
4
4
|
import { type SignedMessage, type Signer } from '../signers/index.js';
|
|
5
5
|
import type { SignDelegateActionReturn } from '../signers/signer.js';
|
|
6
|
-
import
|
|
6
|
+
import { FungibleToken, type NativeToken } from '../tokens/index.js';
|
|
7
7
|
import { type Action, type DelegateAction, type SignedDelegate, type SignedTransaction } from '../transactions/index.js';
|
|
8
8
|
import type { SerializedReturnValue, TxExecutionStatus } from '../types/index.js';
|
|
9
9
|
export declare const DEFAULT_WAIT_STATUS: TxExecutionStatus;
|
package/lib/accounts/account.js
CHANGED
|
@@ -3,7 +3,7 @@ import { parseTransactionExecutionError } from '../providers/errors/parse.js';
|
|
|
3
3
|
import { InvalidNonceError } from '../providers/errors/transaction_execution.js';
|
|
4
4
|
import { JsonRpcProvider } from '../providers/index.js';
|
|
5
5
|
import { KeyPairSigner } from '../signers/index.js';
|
|
6
|
-
import { NEAR } from '../tokens/index.js';
|
|
6
|
+
import { FungibleToken, NEAR } from '../tokens/index.js';
|
|
7
7
|
import { actions, buildDelegateAction, createTransaction, } from '../transactions/index.js';
|
|
8
8
|
import { baseDecode, getTransactionLastResult } from '../utils/index.js';
|
|
9
9
|
import { NonceManager } from './nonce-manager.js';
|
|
@@ -298,7 +298,7 @@ export class Account {
|
|
|
298
298
|
}
|
|
299
299
|
const splitted = newAccountId.split('.');
|
|
300
300
|
if (splitted.length != 2) {
|
|
301
|
-
throw new Error(
|
|
301
|
+
throw new Error(`${this.accountId} can only create sub-accounts of itself, or accounts of the form <account>.<tla>`);
|
|
302
302
|
}
|
|
303
303
|
const TLA = splitted[1];
|
|
304
304
|
return this.signAndSendTransaction({
|
|
@@ -512,6 +512,15 @@ export class Account {
|
|
|
512
512
|
*
|
|
513
513
|
*/
|
|
514
514
|
async transfer({ receiverId, amount, token = NEAR }) {
|
|
515
|
+
if (token instanceof FungibleToken) {
|
|
516
|
+
const isRegistered = await token.isAccountRegistered({ accountId: receiverId, provider: this.provider });
|
|
517
|
+
if (!isRegistered) {
|
|
518
|
+
await token.registerAccount({
|
|
519
|
+
accountIdToRegister: receiverId,
|
|
520
|
+
fundingAccount: this,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
}
|
|
515
524
|
return token.transfer({ from: this, receiverId, amount });
|
|
516
525
|
}
|
|
517
526
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ed25519 } from '@noble/curves/ed25519.js';
|
|
2
|
-
import { randomBytes } from '@noble/hashes/utils.js';
|
|
3
2
|
import { baseDecode, baseEncode } from '../utils/index.js';
|
|
4
3
|
import { KeySize, KeyType } from './constants.js';
|
|
5
4
|
import { KeyPairBase } from './key_pair_base.js';
|
|
@@ -34,7 +33,7 @@ export class KeyPairEd25519 extends KeyPairBase {
|
|
|
34
33
|
* // returns [SECRET_KEY]
|
|
35
34
|
*/
|
|
36
35
|
static fromRandom() {
|
|
37
|
-
const secretKey =
|
|
36
|
+
const secretKey = crypto.getRandomValues(new Uint8Array(KeySize.SECRET_KEY));
|
|
38
37
|
const publicKey = ed25519.getPublicKey(new Uint8Array(secretKey));
|
|
39
38
|
const extendedSecretKey = new Uint8Array([...secretKey, ...publicKey]);
|
|
40
39
|
return new KeyPairEd25519(baseEncode(extendedSecretKey));
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import secp256k1 from 'secp256k1';
|
|
1
|
+
import { secp256k1 } from '@noble/curves/secp256k1.js';
|
|
3
2
|
import { baseDecode, baseEncode } from '../utils/index.js';
|
|
4
3
|
import { KeySize, KeyType } from './constants.js';
|
|
5
4
|
import { KeyPairBase } from './key_pair_base.js';
|
|
@@ -24,8 +23,8 @@ export class KeyPairSecp256k1 extends KeyPairBase {
|
|
|
24
23
|
super();
|
|
25
24
|
const decoded = baseDecode(extendedSecretKey);
|
|
26
25
|
const secretKey = new Uint8Array(decoded.slice(0, KeySize.SECRET_KEY));
|
|
27
|
-
const withHeader = secp256k1.
|
|
28
|
-
const data = withHeader.subarray(1
|
|
26
|
+
const withHeader = secp256k1.getPublicKey(new Uint8Array(secretKey), false);
|
|
27
|
+
const data = withHeader.subarray(1); // remove the 0x04 header byte
|
|
29
28
|
this.publicKey = new PublicKey({
|
|
30
29
|
keyType: KeyType.SECP256K1,
|
|
31
30
|
data,
|
|
@@ -44,17 +43,21 @@ export class KeyPairSecp256k1 extends KeyPairBase {
|
|
|
44
43
|
* // returns [SECRET_KEY]
|
|
45
44
|
*/
|
|
46
45
|
static fromRandom() {
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const publicKey = withHeader.subarray(1, withHeader.length);
|
|
46
|
+
const secretKey = crypto.getRandomValues(new Uint8Array(KeySize.SECRET_KEY));
|
|
47
|
+
const withHeader = secp256k1.getPublicKey(secretKey, false);
|
|
48
|
+
const publicKey = withHeader.subarray(1);
|
|
51
49
|
const extendedSecretKey = new Uint8Array([...secretKey, ...publicKey]);
|
|
52
50
|
return new KeyPairSecp256k1(baseEncode(extendedSecretKey));
|
|
53
51
|
}
|
|
54
52
|
sign(message) {
|
|
55
53
|
// nearcore expects 65 byte signatures formed by appending the recovery id to the 64 byte signature
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
// noble v2 'recovered' format is [recovery, r, s] but NEAR expects [r, s, recovery]
|
|
55
|
+
// prehash: false because NEAR already provides a sha256-hashed message
|
|
56
|
+
const raw = secp256k1.sign(message, baseDecode(this.secretKey), { prehash: false, format: 'recovered' });
|
|
57
|
+
const signature = new Uint8Array(65);
|
|
58
|
+
signature.set(raw.subarray(1), 0); // r, s (64 bytes)
|
|
59
|
+
signature[64] = raw[0]; // recovery byte
|
|
60
|
+
return { signature, publicKey: this.publicKey };
|
|
58
61
|
}
|
|
59
62
|
verify(message, signature) {
|
|
60
63
|
return this.publicKey.verify(message, signature);
|
package/lib/crypto/public_key.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ed25519 } from '@noble/curves/ed25519.js';
|
|
2
|
-
import secp256k1 from 'secp256k1';
|
|
2
|
+
import { secp256k1 } from '@noble/curves/secp256k1.js';
|
|
3
3
|
import { baseDecode, baseEncode } from '../utils/index.js';
|
|
4
4
|
import { KeySize, KeyType } from './constants.js';
|
|
5
5
|
function key_type_to_str(keyType) {
|
|
@@ -70,16 +70,30 @@ export class PublicKey extends Enum {
|
|
|
70
70
|
this[keyName] = publicKey;
|
|
71
71
|
this.enum = keyName;
|
|
72
72
|
}
|
|
73
|
-
/**
|
|
74
|
-
* Creates a PublicKey instance from a string or an existing PublicKey instance.
|
|
75
|
-
* @param value The string or PublicKey instance to create a PublicKey from.
|
|
76
|
-
* @returns {PublicKey} The PublicKey instance.
|
|
77
|
-
*/
|
|
78
73
|
static from(value) {
|
|
79
74
|
if (typeof value === 'string') {
|
|
80
75
|
return PublicKey.fromString(value);
|
|
81
76
|
}
|
|
82
|
-
|
|
77
|
+
if (value instanceof PublicKey) {
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
if (typeof value === 'object' && value !== null) {
|
|
81
|
+
const maybeEd25519 = value['ed25519Key'];
|
|
82
|
+
if (typeof maybeEd25519 === 'object' && maybeEd25519 !== null && 'data' in maybeEd25519) {
|
|
83
|
+
return new PublicKey({
|
|
84
|
+
keyType: KeyType.ED25519,
|
|
85
|
+
data: Uint8Array.from(maybeEd25519.data),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const maybeSecp256k1 = value['secp256k1Key'];
|
|
89
|
+
if (typeof maybeSecp256k1 === 'object' && maybeSecp256k1 !== null && 'data' in maybeSecp256k1) {
|
|
90
|
+
return new PublicKey({
|
|
91
|
+
keyType: KeyType.SECP256K1,
|
|
92
|
+
data: Uint8Array.from(maybeSecp256k1.data),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
throw new Error('Unsupported public key format');
|
|
83
97
|
}
|
|
84
98
|
/**
|
|
85
99
|
* Creates a PublicKey instance from an encoded key string.
|
|
@@ -131,7 +145,9 @@ export class PublicKey extends Enum {
|
|
|
131
145
|
case KeyType.ED25519:
|
|
132
146
|
return ed25519.verify(signature, message, data);
|
|
133
147
|
case KeyType.SECP256K1:
|
|
134
|
-
return secp256k1.
|
|
148
|
+
return secp256k1.verify(signature.subarray(0, 64), message, new Uint8Array([0x04, ...data]), {
|
|
149
|
+
prehash: false,
|
|
150
|
+
});
|
|
135
151
|
default:
|
|
136
152
|
throw new Error(`Unknown key type: ${keyType}`);
|
|
137
153
|
}
|
|
@@ -64,14 +64,14 @@ export declare class JsonRpcProvider implements Provider {
|
|
|
64
64
|
block_height: number;
|
|
65
65
|
}>;
|
|
66
66
|
viewContractCode({ contractId, blockQuery }: ViewContractCodeArgs): Promise<{
|
|
67
|
-
code: Uint8Array<
|
|
67
|
+
code: Uint8Array<ArrayBufferLike>;
|
|
68
68
|
code_base64: string;
|
|
69
69
|
hash: CryptoHash;
|
|
70
70
|
block_hash: CryptoHash;
|
|
71
71
|
block_height: number;
|
|
72
72
|
}>;
|
|
73
73
|
viewGlobalContractCode({ identifier, blockQuery, }: ViewGlobalContractCodeArgs): Promise<{
|
|
74
|
-
code: Uint8Array<
|
|
74
|
+
code: Uint8Array<ArrayBufferLike>;
|
|
75
75
|
code_base64: string;
|
|
76
76
|
hash: CryptoHash;
|
|
77
77
|
block_hash: CryptoHash;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { encodeTransaction } from '../transactions/index.js';
|
|
8
8
|
import { TypedError, } from '../types/index.js';
|
|
9
|
-
import { baseEncode, findSeatPrice } from '../utils/index.js';
|
|
9
|
+
import { base64Decode, base64Encode, baseEncode, bytesToString, findSeatPrice, stringToBytes } from '../utils/index.js';
|
|
10
10
|
import { parseRpcError, parseRpcErrorMessage } from './errors/parse.js';
|
|
11
11
|
import { RpcError } from './errors/rpc.js';
|
|
12
12
|
import { fetchJsonRpc, retryConfig } from './fetch_json.js';
|
|
@@ -122,7 +122,7 @@ export class JsonRpcProvider {
|
|
|
122
122
|
});
|
|
123
123
|
return {
|
|
124
124
|
...data,
|
|
125
|
-
code:
|
|
125
|
+
code: base64Decode(data.code_base64),
|
|
126
126
|
};
|
|
127
127
|
}
|
|
128
128
|
async viewGlobalContractCode({ identifier, blockQuery = { finality: DEFAULT_FINALITY }, }) {
|
|
@@ -143,7 +143,7 @@ export class JsonRpcProvider {
|
|
|
143
143
|
request_type: 'view_global_contract_code',
|
|
144
144
|
code_hash: typeof identifier.codeHash === 'string'
|
|
145
145
|
? identifier.codeHash
|
|
146
|
-
:
|
|
146
|
+
: Array.from(identifier.codeHash, (b) => b.toString(16).padStart(2, '0')).join(''),
|
|
147
147
|
});
|
|
148
148
|
}
|
|
149
149
|
else if ('accountId' in identifier) {
|
|
@@ -158,11 +158,11 @@ export class JsonRpcProvider {
|
|
|
158
158
|
}
|
|
159
159
|
return {
|
|
160
160
|
...data,
|
|
161
|
-
code:
|
|
161
|
+
code: base64Decode(data.code_base64),
|
|
162
162
|
};
|
|
163
163
|
}
|
|
164
164
|
async viewContractState({ contractId, prefix, blockQuery = { finality: DEFAULT_FINALITY }, }) {
|
|
165
|
-
const prefixBase64 =
|
|
165
|
+
const prefixBase64 = base64Encode(stringToBytes(prefix || ''));
|
|
166
166
|
let reference;
|
|
167
167
|
if ('blockId' in blockQuery) {
|
|
168
168
|
reference = { block_id: blockQuery.blockId };
|
|
@@ -184,7 +184,7 @@ export class JsonRpcProvider {
|
|
|
184
184
|
const { result } = await this.callFunctionRaw({ contractId, method, args, blockQuery });
|
|
185
185
|
if (result.length === 0)
|
|
186
186
|
return undefined;
|
|
187
|
-
const serializedResult =
|
|
187
|
+
const serializedResult = bytesToString(new Uint8Array(result));
|
|
188
188
|
try {
|
|
189
189
|
return JSON.parse(serializedResult);
|
|
190
190
|
}
|
|
@@ -193,8 +193,8 @@ export class JsonRpcProvider {
|
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
195
|
async callFunctionRaw({ contractId, method, args, blockQuery = { finality: DEFAULT_FINALITY }, }) {
|
|
196
|
-
const
|
|
197
|
-
const argsBase64 =
|
|
196
|
+
const argsBytes = args instanceof Uint8Array ? args : stringToBytes(JSON.stringify(args));
|
|
197
|
+
const argsBase64 = base64Encode(argsBytes);
|
|
198
198
|
if ('blockId' in blockQuery) {
|
|
199
199
|
return this.query({
|
|
200
200
|
request_type: 'call_function',
|
|
@@ -279,7 +279,7 @@ export class JsonRpcProvider {
|
|
|
279
279
|
async sendTransactionUntil(signedTransaction, waitUntil) {
|
|
280
280
|
const bytes = encodeTransaction(signedTransaction);
|
|
281
281
|
return this.sendJsonRpc('send_tx', {
|
|
282
|
-
signed_tx_base64:
|
|
282
|
+
signed_tx_base64: base64Encode(bytes),
|
|
283
283
|
wait_until: waitUntil,
|
|
284
284
|
});
|
|
285
285
|
}
|
package/lib/tokens/ft/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Account } from '../../accounts/account.js';
|
|
2
|
+
import type { Provider } from '../../providers/provider.js';
|
|
2
3
|
interface FTMetadata {
|
|
3
4
|
spec?: string;
|
|
4
5
|
name: string;
|
|
@@ -61,7 +62,10 @@ export declare class FungibleToken extends BaseFT {
|
|
|
61
62
|
receiverId: string;
|
|
62
63
|
amount: string | number | bigint;
|
|
63
64
|
}): Promise<any>;
|
|
64
|
-
getBalance(
|
|
65
|
+
getBalance({ accountId, provider }: {
|
|
66
|
+
accountId: string;
|
|
67
|
+
provider: Provider;
|
|
68
|
+
}): Promise<bigint>;
|
|
65
69
|
/**
|
|
66
70
|
* Transfer tokens and call a function on the receiver contract,
|
|
67
71
|
* only works if the receiver implements the `ft_on_transfer` method
|
|
@@ -100,6 +104,18 @@ export declare class FungibleToken extends BaseFT {
|
|
|
100
104
|
account: Account;
|
|
101
105
|
force: boolean;
|
|
102
106
|
}): Promise<any>;
|
|
107
|
+
/**
|
|
108
|
+
* Checks if an account is registered to the fungible token contract
|
|
109
|
+
*
|
|
110
|
+
* @param param
|
|
111
|
+
* @param param.accountId The AccountID to check
|
|
112
|
+
* @param param.provider A Provider to use for the query
|
|
113
|
+
* @returns Whether the account is registered
|
|
114
|
+
*/
|
|
115
|
+
isAccountRegistered({ accountId, provider, }: {
|
|
116
|
+
accountId: string;
|
|
117
|
+
provider: Provider;
|
|
118
|
+
}): Promise<boolean>;
|
|
103
119
|
}
|
|
104
120
|
/**
|
|
105
121
|
* The NEAR token is the native token of the NEAR blockchain
|
package/lib/tokens/ft/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { actions } from '../../transactions/action_creators.js';
|
|
2
|
+
import { teraToGas } from '../../units/gas.js';
|
|
2
3
|
import { formatAmount, parseAmount } from './format.js';
|
|
3
4
|
class BaseFT {
|
|
4
5
|
constructor(metadata) {
|
|
@@ -51,15 +52,15 @@ export class FungibleToken extends BaseFT {
|
|
|
51
52
|
amount: amount.toString(),
|
|
52
53
|
receiver_id: receiverId,
|
|
53
54
|
},
|
|
54
|
-
gas:
|
|
55
|
+
gas: teraToGas(30),
|
|
55
56
|
deposit: 1,
|
|
56
57
|
});
|
|
57
58
|
}
|
|
58
|
-
async getBalance(
|
|
59
|
-
const balance = await
|
|
59
|
+
async getBalance({ accountId, provider }) {
|
|
60
|
+
const balance = await provider.callFunction({
|
|
60
61
|
contractId: this.accountId,
|
|
61
62
|
method: 'ft_balance_of',
|
|
62
|
-
args: { account_id:
|
|
63
|
+
args: { account_id: accountId },
|
|
63
64
|
});
|
|
64
65
|
return BigInt(balance);
|
|
65
66
|
}
|
|
@@ -82,7 +83,7 @@ export class FungibleToken extends BaseFT {
|
|
|
82
83
|
amount: amount.toString(),
|
|
83
84
|
msg,
|
|
84
85
|
},
|
|
85
|
-
gas:
|
|
86
|
+
gas: teraToGas(30),
|
|
86
87
|
deposit: 1,
|
|
87
88
|
});
|
|
88
89
|
}
|
|
@@ -107,7 +108,7 @@ export class FungibleToken extends BaseFT {
|
|
|
107
108
|
account_id: accountIdToRegister,
|
|
108
109
|
registration_only: true,
|
|
109
110
|
},
|
|
110
|
-
gas:
|
|
111
|
+
gas: teraToGas(30),
|
|
111
112
|
deposit: requiredDeposit,
|
|
112
113
|
});
|
|
113
114
|
}
|
|
@@ -123,10 +124,35 @@ export class FungibleToken extends BaseFT {
|
|
|
123
124
|
contractId: this.accountId,
|
|
124
125
|
methodName: 'storage_unregister',
|
|
125
126
|
args: { force },
|
|
126
|
-
gas:
|
|
127
|
+
gas: teraToGas(30),
|
|
127
128
|
deposit: 1,
|
|
128
129
|
});
|
|
129
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Checks if an account is registered to the fungible token contract
|
|
133
|
+
*
|
|
134
|
+
* @param param
|
|
135
|
+
* @param param.accountId The AccountID to check
|
|
136
|
+
* @param param.provider A Provider to use for the query
|
|
137
|
+
* @returns Whether the account is registered
|
|
138
|
+
*/
|
|
139
|
+
async isAccountRegistered({ accountId, provider, }) {
|
|
140
|
+
const [storage, required] = await Promise.all([
|
|
141
|
+
provider.callFunction({
|
|
142
|
+
contractId: this.accountId,
|
|
143
|
+
method: 'storage_balance_of',
|
|
144
|
+
args: { account_id: accountId },
|
|
145
|
+
}),
|
|
146
|
+
provider.callFunction({
|
|
147
|
+
contractId: this.accountId,
|
|
148
|
+
method: 'storage_balance_bounds',
|
|
149
|
+
args: {},
|
|
150
|
+
}),
|
|
151
|
+
]);
|
|
152
|
+
if (!storage)
|
|
153
|
+
return false;
|
|
154
|
+
return BigInt(storage.total) >= BigInt(required.min);
|
|
155
|
+
}
|
|
130
156
|
}
|
|
131
157
|
/**
|
|
132
158
|
* The NEAR token is the native token of the NEAR blockchain
|
|
@@ -27,11 +27,11 @@ declare function createAccount(): Action;
|
|
|
27
27
|
*/
|
|
28
28
|
declare function deployContract(code: Uint8Array): Action;
|
|
29
29
|
/**
|
|
30
|
-
* Converts an input argument to a
|
|
30
|
+
* Converts an input argument to a Uint8Array, handling cases for both JSON and Uint8Array.
|
|
31
31
|
* @param args The input argument, either JSON object or Uint8Array.
|
|
32
|
-
* @returns A
|
|
32
|
+
* @returns A Uint8Array representation of the input argument.
|
|
33
33
|
*/
|
|
34
|
-
export declare function stringifyJsonOrBytes(args: any):
|
|
34
|
+
export declare function stringifyJsonOrBytes(args: any): Uint8Array;
|
|
35
35
|
/**
|
|
36
36
|
* Constructs {@link Action} instance representing contract method call.
|
|
37
37
|
*
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { stringToBytes } from '../utils/index.js';
|
|
1
2
|
import { AccessKey, AccessKeyPermission, Action, AddKey, CreateAccount, DeleteAccount, DeleteKey, DeployContract, DeployGlobalContract, FullAccessPermission, FunctionCall, FunctionCallPermission, GlobalContractDeployMode, GlobalContractIdentifier, SignedDelegate, Stake, Transfer, UseGlobalContract, } from './actions.js';
|
|
2
3
|
/**
|
|
3
4
|
* Creates a full access key with full access permissions.
|
|
@@ -46,13 +47,14 @@ function deployContract(code) {
|
|
|
46
47
|
return new Action({ deployContract: new DeployContract({ code }) });
|
|
47
48
|
}
|
|
48
49
|
/**
|
|
49
|
-
* Converts an input argument to a
|
|
50
|
+
* Converts an input argument to a Uint8Array, handling cases for both JSON and Uint8Array.
|
|
50
51
|
* @param args The input argument, either JSON object or Uint8Array.
|
|
51
|
-
* @returns A
|
|
52
|
+
* @returns A Uint8Array representation of the input argument.
|
|
52
53
|
*/
|
|
53
54
|
export function stringifyJsonOrBytes(args) {
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
if (args instanceof Uint8Array)
|
|
56
|
+
return args;
|
|
57
|
+
return stringToBytes(JSON.stringify(args));
|
|
56
58
|
}
|
|
57
59
|
/**
|
|
58
60
|
* Constructs {@link Action} instance representing contract method call.
|
|
@@ -170,7 +172,7 @@ function useGlobalContract(contractIdentifier) {
|
|
|
170
172
|
})
|
|
171
173
|
: new GlobalContractIdentifier({
|
|
172
174
|
CodeHash: typeof contractIdentifier.codeHash === 'string'
|
|
173
|
-
?
|
|
175
|
+
? new Uint8Array(contractIdentifier.codeHash.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)))
|
|
174
176
|
: contractIdentifier.codeHash,
|
|
175
177
|
});
|
|
176
178
|
return new Action({ useGlobalContract: new UseGlobalContract({ contractIdentifier: identifier }) });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Schema } from 'borsh';
|
|
2
|
-
import
|
|
2
|
+
import { PublicKey } from '../crypto/index.js';
|
|
3
3
|
import type { Action, SignedDelegate } from './actions.js';
|
|
4
4
|
import type { DelegateAction } from './delegate.js';
|
|
5
5
|
import type { Signature } from './signature.js';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { deserialize, serialize } from 'borsh';
|
|
2
|
+
import { PublicKey } from '../crypto/index.js';
|
|
2
3
|
import { DelegateActionPrefix } from './prefix.js';
|
|
3
4
|
/**
|
|
4
5
|
* Borsh-encode a delegate action for inclusion as an action within a meta transaction
|
|
@@ -45,7 +46,7 @@ export function decodeSignedTransaction(bytes) {
|
|
|
45
46
|
export class Transaction {
|
|
46
47
|
constructor({ signerId, publicKey, nonce, receiverId, actions, blockHash, }) {
|
|
47
48
|
this.signerId = signerId;
|
|
48
|
-
this.publicKey = publicKey;
|
|
49
|
+
this.publicKey = PublicKey.from(publicKey);
|
|
49
50
|
this.nonce = nonce;
|
|
50
51
|
this.receiverId = receiverId;
|
|
51
52
|
this.actions = actions;
|
|
@@ -60,7 +61,7 @@ export class Transaction {
|
|
|
60
61
|
}
|
|
61
62
|
export class SignedTransaction {
|
|
62
63
|
constructor({ transaction, signature }) {
|
|
63
|
-
this.transaction = transaction;
|
|
64
|
+
this.transaction = transaction instanceof Transaction ? transaction : new Transaction(transaction);
|
|
64
65
|
this.signature = signature;
|
|
65
66
|
}
|
|
66
67
|
encode() {
|
package/lib/utils/format.d.ts
CHANGED
|
@@ -41,4 +41,28 @@ export declare function baseEncode(value: Uint8Array | string): string;
|
|
|
41
41
|
* @returns Uint8Array representing the decoded value
|
|
42
42
|
*/
|
|
43
43
|
export declare function baseDecode(value: string): Uint8Array;
|
|
44
|
+
/**
|
|
45
|
+
* Encodes a Uint8Array into a base64 string
|
|
46
|
+
* @param bytes Uint8Array to encode
|
|
47
|
+
* @returns base64 encoded string
|
|
48
|
+
*/
|
|
49
|
+
export declare function base64Encode(bytes: Uint8Array): string;
|
|
50
|
+
/**
|
|
51
|
+
* Decodes a base64 string into a Uint8Array
|
|
52
|
+
* @param str base64 encoded string
|
|
53
|
+
* @returns Uint8Array representing the decoded value
|
|
54
|
+
*/
|
|
55
|
+
export declare function base64Decode(str: string): Uint8Array;
|
|
56
|
+
/**
|
|
57
|
+
* Encodes a string into a Uint8Array using UTF-8
|
|
58
|
+
* @param str string to encode
|
|
59
|
+
* @returns Uint8Array representing the UTF-8 bytes
|
|
60
|
+
*/
|
|
61
|
+
export declare function stringToBytes(str: string): Uint8Array;
|
|
62
|
+
/**
|
|
63
|
+
* Decodes a Uint8Array into a string using UTF-8
|
|
64
|
+
* @param bytes Uint8Array to decode
|
|
65
|
+
* @returns decoded string
|
|
66
|
+
*/
|
|
67
|
+
export declare function bytesToString(bytes: Uint8Array): string;
|
|
44
68
|
export {};
|
package/lib/utils/format.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { base58 } from '@scure/base';
|
|
1
|
+
import { base58, base64 } from '@scure/base';
|
|
2
2
|
/**
|
|
3
3
|
* Exponent for calculating how many indivisible units are there in one NEAR. See {@link NEAR_NOMINATION}.
|
|
4
4
|
*/
|
|
@@ -126,3 +126,37 @@ export function baseEncode(value) {
|
|
|
126
126
|
export function baseDecode(value) {
|
|
127
127
|
return base58.decode(value);
|
|
128
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Encodes a Uint8Array into a base64 string
|
|
131
|
+
* @param bytes Uint8Array to encode
|
|
132
|
+
* @returns base64 encoded string
|
|
133
|
+
*/
|
|
134
|
+
export function base64Encode(bytes) {
|
|
135
|
+
return base64.encode(bytes);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Decodes a base64 string into a Uint8Array
|
|
139
|
+
* @param str base64 encoded string
|
|
140
|
+
* @returns Uint8Array representing the decoded value
|
|
141
|
+
*/
|
|
142
|
+
export function base64Decode(str) {
|
|
143
|
+
return base64.decode(str);
|
|
144
|
+
}
|
|
145
|
+
const encoder = new TextEncoder();
|
|
146
|
+
const decoder = new TextDecoder();
|
|
147
|
+
/**
|
|
148
|
+
* Encodes a string into a Uint8Array using UTF-8
|
|
149
|
+
* @param str string to encode
|
|
150
|
+
* @returns Uint8Array representing the UTF-8 bytes
|
|
151
|
+
*/
|
|
152
|
+
export function stringToBytes(str) {
|
|
153
|
+
return encoder.encode(str);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Decodes a Uint8Array into a string using UTF-8
|
|
157
|
+
* @param bytes Uint8Array to decode
|
|
158
|
+
* @returns decoded string
|
|
159
|
+
*/
|
|
160
|
+
export function bytesToString(bytes) {
|
|
161
|
+
return decoder.decode(bytes);
|
|
162
|
+
}
|
package/lib/utils/provider.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { base64Decode, bytesToString } from './format.js';
|
|
1
2
|
/** @hidden */
|
|
2
3
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
3
4
|
export function getTransactionLastResult(txResult) {
|
|
@@ -5,7 +6,7 @@ export function getTransactionLastResult(txResult) {
|
|
|
5
6
|
txResult.status !== null &&
|
|
6
7
|
'SuccessValue' in txResult.status &&
|
|
7
8
|
typeof txResult.status.SuccessValue === 'string') {
|
|
8
|
-
const value =
|
|
9
|
+
const value = bytesToString(base64Decode(txResult.status.SuccessValue));
|
|
9
10
|
try {
|
|
10
11
|
return JSON.parse(value);
|
|
11
12
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "near-api-js",
|
|
3
3
|
"description": "JavaScript library to interact with NEAR Protocol via RPC API",
|
|
4
|
-
"version": "7.
|
|
4
|
+
"version": "7.2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/near/near-api-js.git"
|
|
@@ -48,8 +48,7 @@
|
|
|
48
48
|
"@scure/base": "2.0.0",
|
|
49
49
|
"borsh": "2.0.0",
|
|
50
50
|
"is-my-json-valid": "2.20.6",
|
|
51
|
-
"near-seed-phrase": "0.2.1"
|
|
52
|
-
"secp256k1": "5.0.1"
|
|
51
|
+
"near-seed-phrase": "0.2.1"
|
|
53
52
|
},
|
|
54
53
|
"devDependencies": {
|
|
55
54
|
"@arethetypeswrong/cli": "0.18.2",
|