near-api-ts 0.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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/index.cjs +3222 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +838 -0
- package/dist/index.d.ts +838 -0
- package/dist/index.js +3169 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -0
- package/src/_common/configs/constants.ts +23 -0
- package/src/_common/schemas/borsh/actions/addKey.ts +44 -0
- package/src/_common/schemas/borsh/actions/createAccount.ts +7 -0
- package/src/_common/schemas/borsh/actions/deleteAccount.ts +9 -0
- package/src/_common/schemas/borsh/actions/deleteKey.ts +11 -0
- package/src/_common/schemas/borsh/actions/deployContract.ts +9 -0
- package/src/_common/schemas/borsh/actions/deployGlobalContract.ts +24 -0
- package/src/_common/schemas/borsh/actions/functionCall.ts +12 -0
- package/src/_common/schemas/borsh/actions/signedDelegate.ts +13 -0
- package/src/_common/schemas/borsh/actions/stake.ts +12 -0
- package/src/_common/schemas/borsh/actions/transfer.ts +9 -0
- package/src/_common/schemas/borsh/actions/useGlobalContract.ts +23 -0
- package/src/_common/schemas/borsh/delegateAction.ts +41 -0
- package/src/_common/schemas/borsh/index.ts +3 -0
- package/src/_common/schemas/borsh/publicKey.ts +25 -0
- package/src/_common/schemas/borsh/signature.ts +25 -0
- package/src/_common/schemas/borsh/signedTransaction.ts +10 -0
- package/src/_common/schemas/borsh/transaction.ts +42 -0
- package/src/_common/schemas/valibot/common.ts +12 -0
- package/src/_common/schemas/valibot/cryptoHash.ts +12 -0
- package/src/_common/schemas/valibot/curveString.ts +23 -0
- package/src/_common/schemas/valibot/privateKey.ts +38 -0
- package/src/_common/schemas/valibot/publicKey.ts +33 -0
- package/src/_common/schemas/valibot/transaction.ts +42 -0
- package/src/_common/schemas/zod/common.ts +29 -0
- package/src/_common/transformers/contract.ts +11 -0
- package/src/_common/transformers/curveString.ts +27 -0
- package/src/_common/transformers/toBorshBytes/signedTransaction.ts +20 -0
- package/src/_common/transformers/toBorshBytes/transaction.ts +12 -0
- package/src/_common/transformers/toNative/actions/addKey.ts +34 -0
- package/src/_common/transformers/toNative/actions/createAccount.ts +5 -0
- package/src/_common/transformers/toNative/actions/deleteAccount.ts +12 -0
- package/src/_common/transformers/toNative/actions/deleteKey.ts +13 -0
- package/src/_common/transformers/toNative/actions/deployContract.ts +17 -0
- package/src/_common/transformers/toNative/actions/functionCall.ts +22 -0
- package/src/_common/transformers/toNative/actions/transfer.ts +13 -0
- package/src/_common/transformers/toNative/blockReference.ts +30 -0
- package/src/_common/transformers/toNative/publicKey.ts +8 -0
- package/src/_common/transformers/toNative/signature.ts +8 -0
- package/src/_common/transformers/toNative/signedTransaction.ts +13 -0
- package/src/_common/transformers/toNative/transaction.ts +67 -0
- package/src/_common/utils/common.ts +22 -0
- package/src/_common/utils/createCircularQueue.ts +46 -0
- package/src/_common/utils/createLinkedList.ts +30 -0
- package/src/_common/utils/snakeToCamelCase.ts +26 -0
- package/src/_common/utils/tokenConverter/convertTokensToUnits.ts +65 -0
- package/src/_common/utils/tokenConverter/convertUnitsToTokens.ts +71 -0
- package/src/_common/utils/tokenConverter/helpers.ts +26 -0
- package/src/client/createClient/account/getAccountKey.ts +51 -0
- package/src/client/createClient/account/getAccountKeys.ts +47 -0
- package/src/client/createClient/account/getAccountState.ts +65 -0
- package/src/client/createClient/account/helpers/transformKey.ts +36 -0
- package/src/client/createClient/block/getBlock.ts +24 -0
- package/src/client/createClient/contract/callContractReadFunction.ts +66 -0
- package/src/client/createClient/contract/getContractState.ts +60 -0
- package/src/client/createClient/createClient.ts +34 -0
- package/src/client/createClient/createSendRequest.ts +29 -0
- package/src/client/createClient/protocol/getGasPrice.ts +51 -0
- package/src/client/createClient/protocol/getProtocolConfig.ts +39 -0
- package/src/client/createClient/transaction/sendSignedTransaction.ts +31 -0
- package/src/client/presets/networks.ts +21 -0
- package/src/helpers/actionCreators/addFullAccessKey.ts +14 -0
- package/src/helpers/actionCreators/addFunctionCallKey.ts +20 -0
- package/src/helpers/actionCreators/createAccount.ts +5 -0
- package/src/helpers/actionCreators/deleteAccount.ts +11 -0
- package/src/helpers/actionCreators/deleteKey.ts +9 -0
- package/src/helpers/actionCreators/deployContract.ts +13 -0
- package/src/helpers/actionCreators/functionCall.ts +12 -0
- package/src/helpers/actionCreators/transfer.ts +9 -0
- package/src/helpers/crypto/getPublicKey.ts +59 -0
- package/src/helpers/crypto/getRandomPrivateKey.ts +19 -0
- package/src/helpers/crypto/getTransactionHash.ts +21 -0
- package/src/helpers/crypto/sign.ts +69 -0
- package/src/helpers/gas.ts +25 -0
- package/src/helpers/near.ts +118 -0
- package/src/index.ts +19 -0
- package/src/keyServices/memoryKeyService/createFindPrivateKey.ts +11 -0
- package/src/keyServices/memoryKeyService/createMemoryKeyService.ts +23 -0
- package/src/keyServices/memoryKeyService/createSignTransaction.ts +24 -0
- package/src/keyServices/memoryKeyService/parseKeySources.ts +49 -0
- package/src/signers/memorySigner/createMemorySigner.ts +35 -0
- package/src/signers/memorySigner/executors/executeTransaction.ts +30 -0
- package/src/signers/memorySigner/executors/helpers/getSignedTransaction.ts +32 -0
- package/src/signers/memorySigner/executors/signTransaction.ts +26 -0
- package/src/signers/memorySigner/keyPool/createFindKeyForTask.ts +27 -0
- package/src/signers/memorySigner/keyPool/createIsKeyForTaskExist.ts +22 -0
- package/src/signers/memorySigner/keyPool/createKeyPool.ts +37 -0
- package/src/signers/memorySigner/keyPool/getFullAccessKeyList.ts +39 -0
- package/src/signers/memorySigner/keyPool/getFunctionCallKeyList.ts +43 -0
- package/src/signers/memorySigner/keyPool/helpers/keyUtils.ts +13 -0
- package/src/signers/memorySigner/matcher/createMatcher.ts +42 -0
- package/src/signers/memorySigner/resolver/createResolver.ts +21 -0
- package/src/signers/memorySigner/state/createState.ts +27 -0
- package/src/signers/memorySigner/taskQueue/addTask/executeMultipleTransactions.ts +29 -0
- package/src/signers/memorySigner/taskQueue/addTask/executeTransaction.ts +28 -0
- package/src/signers/memorySigner/taskQueue/addTask/helpers/getSigningKeyPriority.ts +39 -0
- package/src/signers/memorySigner/taskQueue/addTask/signMultipleTransactions.ts +26 -0
- package/src/signers/memorySigner/taskQueue/addTask/signTransaction.ts +23 -0
- package/src/signers/memorySigner/taskQueue/createFindTaskForKey.ts +22 -0
- package/src/signers/memorySigner/taskQueue/createTaskQueue.ts +45 -0
- package/types/accountKey.ts +24 -0
- package/types/actions/addKey.ts +48 -0
- package/types/actions/createAccount.ts +9 -0
- package/types/actions/deleteAccount.ts +18 -0
- package/types/actions/deleteKey.ts +18 -0
- package/types/actions/deployContract.ts +19 -0
- package/types/actions/functionCall.ts +32 -0
- package/types/actions/transfer.ts +18 -0
- package/types/client/account/getAccountKey.ts +30 -0
- package/types/client/account/getAccountKeys.ts +28 -0
- package/types/client/account/getAccountState.ts +40 -0
- package/types/client/block/getBlock.ts +13 -0
- package/types/client/client.ts +52 -0
- package/types/client/contract/callContractReadFunction.ts +98 -0
- package/types/client/contract/getContractState.ts +33 -0
- package/types/client/protocol/getGasPrice.ts +16 -0
- package/types/client/protocol/getProtocolConfig.ts +17 -0
- package/types/client/transaction/sendSignedTransaction.ts +19 -0
- package/types/common.ts +79 -0
- package/types/contract.ts +13 -0
- package/types/crypto.ts +25 -0
- package/types/delegateAction.ts +0 -0
- package/types/keyServices/memoryKeyService.ts +26 -0
- package/types/signedTransaction.ts +14 -0
- package/types/signers/memorySigner.ts +59 -0
- package/types/transaction.ts +107 -0
- package/types/utils.ts +44 -0
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "near-api-ts",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Typescript library for interactions with Near Protocol in the browser or Node.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": {
|
|
9
|
+
"import": "./dist/index.d.ts",
|
|
10
|
+
"require": "./dist/index.d.cts"
|
|
11
|
+
},
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"types",
|
|
20
|
+
"package.json",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup",
|
|
29
|
+
"dev": "tsup --watch",
|
|
30
|
+
"run": "tsx src/index.ts",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@noble/curves": "^1.9.4",
|
|
35
|
+
"@noble/hashes": "^1.8.0",
|
|
36
|
+
"@scure/base": "^1.2.6",
|
|
37
|
+
"borsh": "^2.0.0",
|
|
38
|
+
"valibot": "^1.1.0",
|
|
39
|
+
"zod": "^4.0.11"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@near-js/jsonrpc-types": "^1.3.4"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const BinaryCryptoKeyLengths = {
|
|
2
|
+
Ed25519: {
|
|
3
|
+
// SecretKey + PublicKey
|
|
4
|
+
PrivateKey: 64,
|
|
5
|
+
SecretKey: 32,
|
|
6
|
+
PublicKey: 32,
|
|
7
|
+
},
|
|
8
|
+
Secp256k1: {
|
|
9
|
+
// SecretKey + PublicKey
|
|
10
|
+
PrivateKey: 96,
|
|
11
|
+
SecretKey: 32,
|
|
12
|
+
PublicKey: 64,
|
|
13
|
+
},
|
|
14
|
+
} as const;
|
|
15
|
+
|
|
16
|
+
export const BinaryCryptoHashLength = 32;
|
|
17
|
+
export const NearDecimals = 24;
|
|
18
|
+
|
|
19
|
+
// Fetch block every 1000 blocks * 600ms best block time = >10 minutes
|
|
20
|
+
export const RefetchBlockHashInterval = 600_000;
|
|
21
|
+
|
|
22
|
+
// 1 minute
|
|
23
|
+
export const SignerTaskTtlMs = 60_000 as const
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { publicKeyBorshSchema } from '../publicKey';
|
|
2
|
+
|
|
3
|
+
const fullAccessPermissionBorshSchema = {
|
|
4
|
+
struct: {
|
|
5
|
+
fullAccess: {
|
|
6
|
+
struct: {},
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const functionCallPermissionBorshSchema = {
|
|
12
|
+
struct: {
|
|
13
|
+
functionCall: {
|
|
14
|
+
struct: {
|
|
15
|
+
allowance: { option: 'u128' },
|
|
16
|
+
receiverId: 'string',
|
|
17
|
+
methodNames: { array: { type: 'string' } },
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const accessKeyBorshSchema = {
|
|
24
|
+
struct: {
|
|
25
|
+
nonce: 'u64',
|
|
26
|
+
permission: {
|
|
27
|
+
enum: [
|
|
28
|
+
functionCallPermissionBorshSchema,
|
|
29
|
+
fullAccessPermissionBorshSchema,
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const addKeyActionBorshSchema = {
|
|
36
|
+
struct: {
|
|
37
|
+
addKey: {
|
|
38
|
+
struct: {
|
|
39
|
+
publicKey: publicKeyBorshSchema,
|
|
40
|
+
accessKey: accessKeyBorshSchema,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const codeHashModeBorshSchema = {
|
|
2
|
+
struct: {
|
|
3
|
+
CodeHash: { struct: {} },
|
|
4
|
+
},
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const accountIdModeBorshSchema = {
|
|
8
|
+
struct: {
|
|
9
|
+
AccountId: { struct: {} },
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const deployGlobalContractActionBorshSchema = {
|
|
14
|
+
struct: {
|
|
15
|
+
deployGlobalContract: {
|
|
16
|
+
struct: {
|
|
17
|
+
code: { array: { type: 'u8' } },
|
|
18
|
+
deployMode: {
|
|
19
|
+
enum: [codeHashModeBorshSchema, accountIdModeBorshSchema],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { delegateActionBorshSchema } from '../delegateAction';
|
|
2
|
+
import { signatureBorshSchema } from '../signature';
|
|
3
|
+
|
|
4
|
+
export const signedDelegateActionBorshSchema = {
|
|
5
|
+
struct: {
|
|
6
|
+
signedDelegate: {
|
|
7
|
+
struct: {
|
|
8
|
+
delegateAction: delegateActionBorshSchema,
|
|
9
|
+
signature: signatureBorshSchema,
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const codeHashIdentifierBorshSchema = {
|
|
2
|
+
struct: {
|
|
3
|
+
CodeHash: { array: { type: 'u8', len: 32 } },
|
|
4
|
+
},
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const accountIdIdentifierBorshSchema = {
|
|
8
|
+
struct: {
|
|
9
|
+
AccountId: 'string',
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const useGlobalContractActionBorshSchema = {
|
|
14
|
+
struct: {
|
|
15
|
+
useGlobalContract: {
|
|
16
|
+
struct: {
|
|
17
|
+
contractIdentifier: {
|
|
18
|
+
enum: [codeHashIdentifierBorshSchema, accountIdIdentifierBorshSchema],
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Schema } from 'borsh';
|
|
2
|
+
import { publicKeyBorshSchema } from './publicKey';
|
|
3
|
+
import { createAccountActionBorshSchema } from './actions/createAccount';
|
|
4
|
+
import { transferActionBorshSchema } from './actions/transfer';
|
|
5
|
+
import { addKeyActionBorshSchema } from './actions/addKey';
|
|
6
|
+
import { deployContractActionBorshSchema } from './actions/deployContract';
|
|
7
|
+
import { functionCallActionBorshSchema } from './actions/functionCall';
|
|
8
|
+
import { stakeActionBorshSchema } from './actions/stake';
|
|
9
|
+
import { deleteKeyActionBorshSchema } from './actions/deleteKey';
|
|
10
|
+
import { deleteAccountActionBorshSchema } from './actions/deleteAccount';
|
|
11
|
+
import { deployGlobalContractActionBorshSchema } from './actions/deployGlobalContract';
|
|
12
|
+
import { useGlobalContractActionBorshSchema } from './actions/useGlobalContract';
|
|
13
|
+
|
|
14
|
+
const allowedActionBorshSchema: Schema = {
|
|
15
|
+
enum: [
|
|
16
|
+
createAccountActionBorshSchema,
|
|
17
|
+
deployContractActionBorshSchema,
|
|
18
|
+
functionCallActionBorshSchema,
|
|
19
|
+
transferActionBorshSchema,
|
|
20
|
+
stakeActionBorshSchema,
|
|
21
|
+
addKeyActionBorshSchema,
|
|
22
|
+
deleteKeyActionBorshSchema,
|
|
23
|
+
deleteAccountActionBorshSchema,
|
|
24
|
+
{ struct: { signedDelegate: 'string' } }, // TODO is it possible to rid this placeholder and keep enum order?
|
|
25
|
+
deployGlobalContractActionBorshSchema,
|
|
26
|
+
useGlobalContractActionBorshSchema,
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const delegateActionBorshSchema: Schema = {
|
|
31
|
+
struct: {
|
|
32
|
+
senderId: 'string',
|
|
33
|
+
publicKey: publicKeyBorshSchema,
|
|
34
|
+
actions: { array: { type: allowedActionBorshSchema } },
|
|
35
|
+
receiverId: 'string',
|
|
36
|
+
nonce: 'u64',
|
|
37
|
+
maxBlockHeight: 'u64',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// TODO add DelegateActionPrefix
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Schema } from 'borsh';
|
|
2
|
+
|
|
3
|
+
const Ed25519PublicKeyBorshSchema = {
|
|
4
|
+
struct: {
|
|
5
|
+
ed25519Key: {
|
|
6
|
+
struct: {
|
|
7
|
+
data: { array: { type: 'u8', len: 32 } },
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const Secp256k1PublicKeyBorshSchema = {
|
|
14
|
+
struct: {
|
|
15
|
+
secp256k1Key: {
|
|
16
|
+
struct: {
|
|
17
|
+
data: { array: { type: 'u8', len: 64 } },
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const publicKeyBorshSchema: Schema = {
|
|
24
|
+
enum: [Ed25519PublicKeyBorshSchema, Secp256k1PublicKeyBorshSchema],
|
|
25
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Schema } from 'borsh';
|
|
2
|
+
|
|
3
|
+
const Ed25519SignatureBorshSchema = {
|
|
4
|
+
struct: {
|
|
5
|
+
ed25519Signature: {
|
|
6
|
+
struct: {
|
|
7
|
+
data: { array: { type: 'u8', len: 64 } },
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const Secp256k1SignatureBorshSchema = {
|
|
14
|
+
struct: {
|
|
15
|
+
secp256k1Signature: {
|
|
16
|
+
struct: {
|
|
17
|
+
data: { array: { type: 'u8', len: 65 } },
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const signatureBorshSchema: Schema = {
|
|
24
|
+
enum: [Ed25519SignatureBorshSchema, Secp256k1SignatureBorshSchema],
|
|
25
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Schema } from 'borsh';
|
|
2
|
+
import { transactionBorshSchema } from './transaction';
|
|
3
|
+
import { signatureBorshSchema } from './signature';
|
|
4
|
+
|
|
5
|
+
export const signedTransactionBorshSchema: Schema = {
|
|
6
|
+
struct: {
|
|
7
|
+
transaction: transactionBorshSchema,
|
|
8
|
+
signature: signatureBorshSchema,
|
|
9
|
+
},
|
|
10
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Schema } from 'borsh';
|
|
2
|
+
import { publicKeyBorshSchema } from './publicKey';
|
|
3
|
+
import { createAccountActionBorshSchema } from './actions/createAccount';
|
|
4
|
+
import { transferActionBorshSchema } from './actions/transfer';
|
|
5
|
+
import { addKeyActionBorshSchema } from './actions/addKey';
|
|
6
|
+
import { deployContractActionBorshSchema } from './actions/deployContract';
|
|
7
|
+
import { deployGlobalContractActionBorshSchema } from './actions/deployGlobalContract';
|
|
8
|
+
import { useGlobalContractActionBorshSchema } from './actions/useGlobalContract';
|
|
9
|
+
import { functionCallActionBorshSchema } from './actions/functionCall';
|
|
10
|
+
import { stakeActionBorshSchema } from './actions/stake';
|
|
11
|
+
import { deleteKeyActionBorshSchema } from './actions/deleteKey';
|
|
12
|
+
import { deleteAccountActionBorshSchema } from './actions/deleteAccount';
|
|
13
|
+
import { signedDelegateActionBorshSchema } from './actions/signedDelegate';
|
|
14
|
+
|
|
15
|
+
// Actions order in this enum is important and must match nearcore
|
|
16
|
+
const actionBorshSchema: Schema = {
|
|
17
|
+
enum: [
|
|
18
|
+
createAccountActionBorshSchema,
|
|
19
|
+
deployContractActionBorshSchema,
|
|
20
|
+
functionCallActionBorshSchema,
|
|
21
|
+
transferActionBorshSchema,
|
|
22
|
+
stakeActionBorshSchema,
|
|
23
|
+
addKeyActionBorshSchema,
|
|
24
|
+
deleteKeyActionBorshSchema,
|
|
25
|
+
deleteAccountActionBorshSchema,
|
|
26
|
+
signedDelegateActionBorshSchema,
|
|
27
|
+
deployGlobalContractActionBorshSchema,
|
|
28
|
+
useGlobalContractActionBorshSchema,
|
|
29
|
+
],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// Fields order is important and must follow the nearcore
|
|
33
|
+
export const transactionBorshSchema: Schema = {
|
|
34
|
+
struct: {
|
|
35
|
+
signerId: 'string',
|
|
36
|
+
publicKey: publicKeyBorshSchema,
|
|
37
|
+
nonce: 'u64',
|
|
38
|
+
receiverId: 'string',
|
|
39
|
+
blockHash: { array: { type: 'u8', len: 32 } },
|
|
40
|
+
actions: { array: { type: actionBorshSchema } },
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
|
|
3
|
+
export const Base58StringSchema = v.pipe(
|
|
4
|
+
v.string(),
|
|
5
|
+
v.regex(
|
|
6
|
+
/^[1-9A-HJ-NP-Za-km-z]+$/,
|
|
7
|
+
`Base58 string contains invalid characters. Allowed characters:\
|
|
8
|
+
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`,
|
|
9
|
+
),
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { base58 } from '@scure/base';
|
|
3
|
+
import { Base58StringSchema } from './common';
|
|
4
|
+
import { BinaryCryptoHashLength } from '../../configs/constants';
|
|
5
|
+
|
|
6
|
+
export const Base58CryptoHashSchema = v.pipe(
|
|
7
|
+
Base58StringSchema,
|
|
8
|
+
v.check((value) => {
|
|
9
|
+
const u8Value = base58.decode(value);
|
|
10
|
+
return u8Value.length === BinaryCryptoHashLength;
|
|
11
|
+
}, `Binary Crypto Hash length should be exactly ${BinaryCryptoHashLength} bytes`),
|
|
12
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import type { Curve, CurveString } from 'nat-types/crypto';
|
|
3
|
+
import { base58 } from '@scure/base';
|
|
4
|
+
|
|
5
|
+
export const CurveStringSchema = v.pipe(
|
|
6
|
+
v.string(),
|
|
7
|
+
v.regex(
|
|
8
|
+
/^(ed25519|secp256k1):[1-9A-HJ-NP-Za-km-z]+$/,
|
|
9
|
+
'Invalid elliptic curve string format, expected <curve>:<base58>',
|
|
10
|
+
),
|
|
11
|
+
v.transform((value) => value as CurveString),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export const CurveStringTransformSchema = v.pipe(
|
|
15
|
+
CurveStringSchema,
|
|
16
|
+
v.transform((value) => {
|
|
17
|
+
const [curve, base58String] = value.split(':');
|
|
18
|
+
return {
|
|
19
|
+
curve: curve as Curve,
|
|
20
|
+
u8Data: base58.decode(base58String),
|
|
21
|
+
};
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BinaryCryptoKeyLengths } from '../../configs/constants';
|
|
2
|
+
import * as v from 'valibot';
|
|
3
|
+
|
|
4
|
+
const { Ed25519, Secp256k1 } = BinaryCryptoKeyLengths;
|
|
5
|
+
|
|
6
|
+
export const BinarySecp256k1PrivateKeySchema = v.pipe(
|
|
7
|
+
v.instance(Uint8Array),
|
|
8
|
+
v.length(
|
|
9
|
+
Secp256k1.PrivateKey,
|
|
10
|
+
`Length of binary secp256k1 private key should be ${Secp256k1.PrivateKey}`,
|
|
11
|
+
),
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export const BinaryEd25519PrivateKeySchema = v.pipe(
|
|
15
|
+
v.instance(Uint8Array),
|
|
16
|
+
v.length(
|
|
17
|
+
Ed25519.PrivateKey,
|
|
18
|
+
`Length of binary ed25519 private key should be ${Ed25519.PrivateKey}`,
|
|
19
|
+
),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
// import { getRandomSecp256k1PrivateKey } from '../../crypto/getRandomPrivateKey';
|
|
23
|
+
|
|
24
|
+
// new Array(100).fill(0).forEach((_, i) => {
|
|
25
|
+
// const { publicKey, privateKey } = getRandomSecp256k1PrivateKey();
|
|
26
|
+
// console.log(
|
|
27
|
+
// `Public key: ${publicKey}, privateKey: ${privateKey}`,
|
|
28
|
+
// `Public key: ${publicKey.length}, privateKey: ${privateKey.length}`,
|
|
29
|
+
// );
|
|
30
|
+
// });
|
|
31
|
+
|
|
32
|
+
// console.log(
|
|
33
|
+
// v.parse(
|
|
34
|
+
// PublicKeySchema,
|
|
35
|
+
// 'secp256k1:2k5acvmedoDyDm28qDknzd7R7gY7ndYwtTAbmn8P2bVuiXDCdFEi7KbbYxuXQArpTp9SveYYRgBP1LG9Jxwf1NGn2ad',
|
|
36
|
+
// { abortEarly: true}
|
|
37
|
+
// ),
|
|
38
|
+
// );
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import type { Ed25519PublicKey, Secp256k1PublicKey } from 'nat-types/crypto';
|
|
3
|
+
|
|
4
|
+
const Ed25519PublicKeySchema = v.pipe(
|
|
5
|
+
v.string(),
|
|
6
|
+
v.regex(
|
|
7
|
+
/^ed25519:[1-9A-HJ-NP-Za-km-z]+$/,
|
|
8
|
+
'Invalid public key format, expected ed25519:<base58>',
|
|
9
|
+
),
|
|
10
|
+
v.check(
|
|
11
|
+
(value) => value.length === 51 || value.length === 52,
|
|
12
|
+
'Ed25519 public key string must be 51 or 52 characters long',
|
|
13
|
+
),
|
|
14
|
+
v.transform((value) => value as Ed25519PublicKey),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const Secp256k1PublicKeySchema = v.pipe(
|
|
18
|
+
v.string(),
|
|
19
|
+
v.regex(
|
|
20
|
+
/^secp256k1:[1-9A-HJ-NP-Za-km-z]+$/,
|
|
21
|
+
'Invalid public key format, expected secp256k1:<base58>',
|
|
22
|
+
),
|
|
23
|
+
v.check(
|
|
24
|
+
(value) => value.length === 97 || value.length === 98,
|
|
25
|
+
'Secp256k1 public key string must be 97 or 98 characters long',
|
|
26
|
+
),
|
|
27
|
+
v.transform((value) => value as Secp256k1PublicKey),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export const PublicKeySchema = v.union(
|
|
31
|
+
[Ed25519PublicKeySchema, Secp256k1PublicKeySchema],
|
|
32
|
+
'Invalid public key',
|
|
33
|
+
);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as v from 'valibot';
|
|
2
|
+
import { PublicKeySchema } from './publicKey';
|
|
3
|
+
import { Base58CryptoHashSchema } from './cryptoHash';
|
|
4
|
+
|
|
5
|
+
// Near Account
|
|
6
|
+
const AccountIdSchema = v.pipe(v.string(), v.brand('AccountId'));
|
|
7
|
+
|
|
8
|
+
type AccountId = v.InferOutput<typeof AccountIdSchema>;
|
|
9
|
+
|
|
10
|
+
const NonceSchema = v.number('uint64');
|
|
11
|
+
|
|
12
|
+
const TransferActionSchema = v.object({
|
|
13
|
+
type: v.literal('Transfer'),
|
|
14
|
+
params: v.object({
|
|
15
|
+
amount: v.object({
|
|
16
|
+
yoctoNear: v.bigint(),
|
|
17
|
+
}),
|
|
18
|
+
}),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const CreateAccountActionSchema = v.object({
|
|
22
|
+
type: v.literal('CreateAccount'),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const ActionSchema = v.variant('type', [
|
|
26
|
+
TransferActionSchema,
|
|
27
|
+
CreateAccountActionSchema,
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
export const TransactionSchema = v.object({
|
|
31
|
+
signerAccountId: AccountIdSchema,
|
|
32
|
+
signerPublicKey: PublicKeySchema,
|
|
33
|
+
receiverAccountId: AccountIdSchema,
|
|
34
|
+
// action: v.optional(ActionSchema),
|
|
35
|
+
nonce: NonceSchema,
|
|
36
|
+
blockHash: Base58CryptoHashSchema,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
type Transaction = v.InferOutput<typeof TransactionSchema>;
|
|
41
|
+
|
|
42
|
+
// const a: Transaction = {}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as z from 'zod/mini';
|
|
2
|
+
import { base58 } from '@scure/base';
|
|
3
|
+
import { oneLine } from '@common/utils/common';
|
|
4
|
+
|
|
5
|
+
const Base58StringSchema = z.string().check(
|
|
6
|
+
z.regex(
|
|
7
|
+
/^[1-9A-HJ-NP-Za-km-z]+$/,
|
|
8
|
+
oneLine(`Base58 string contains invalid characters. Allowed characters:
|
|
9
|
+
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`),
|
|
10
|
+
),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const CryptoHashSchema = Base58StringSchema.check(
|
|
14
|
+
z.refine(
|
|
15
|
+
(val) => {
|
|
16
|
+
try {
|
|
17
|
+
const bytes = base58.decode(val);
|
|
18
|
+
return bytes.length === 32;
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
{ error: 'CryptoHash string must decode to 32 bytes' },
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export const BlockHashSchema = CryptoHashSchema;
|
|
28
|
+
export const BlockHeightSchema = z.uint64();
|
|
29
|
+
export const BlockIdSchema = z.union([BlockHeightSchema, BlockHashSchema]);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FnArgs } from 'nat-types/contract';
|
|
2
|
+
import type { MaybeJsonLikeValue } from 'nat-types/common';
|
|
3
|
+
import { toJsonBytes } from '@common/utils/common';
|
|
4
|
+
|
|
5
|
+
export const toContractFnArgsBytes = <AJ extends MaybeJsonLikeValue>(
|
|
6
|
+
args: FnArgs<AJ>,
|
|
7
|
+
): Uint8Array => {
|
|
8
|
+
if (args.fnArgsBytes) return args.fnArgsBytes;
|
|
9
|
+
if (args.fnArgsJson) return toJsonBytes(args.fnArgsJson);
|
|
10
|
+
return new Uint8Array();
|
|
11
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { base58 } from '@scure/base';
|
|
2
|
+
import type {
|
|
3
|
+
Curve,
|
|
4
|
+
CurveString,
|
|
5
|
+
Ed25519CurveString,
|
|
6
|
+
Secp256k1CurveString,
|
|
7
|
+
} from 'nat-types/crypto';
|
|
8
|
+
|
|
9
|
+
export const toEd25519CurveString = (u8Data: Uint8Array): Ed25519CurveString =>
|
|
10
|
+
`ed25519:${base58.encode(u8Data)}`;
|
|
11
|
+
|
|
12
|
+
export const toSecp256k1CurveString = (
|
|
13
|
+
u8Data: Uint8Array,
|
|
14
|
+
): Secp256k1CurveString => `secp256k1:${base58.encode(u8Data)}`;
|
|
15
|
+
|
|
16
|
+
export const fromCurveString = (
|
|
17
|
+
value: CurveString,
|
|
18
|
+
): {
|
|
19
|
+
curve: Curve;
|
|
20
|
+
u8Data: Uint8Array;
|
|
21
|
+
} => {
|
|
22
|
+
const [curve, base58String] = value.split(':');
|
|
23
|
+
return {
|
|
24
|
+
curve: curve as Curve,
|
|
25
|
+
u8Data: base58.decode(base58String),
|
|
26
|
+
};
|
|
27
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NativeSignedTransaction,
|
|
3
|
+
SignedTransaction,
|
|
4
|
+
} from 'nat-types/signedTransaction';
|
|
5
|
+
import type { BorshBytes } from 'nat-types/common';
|
|
6
|
+
import { serialize } from 'borsh';
|
|
7
|
+
import { signedTransactionBorshSchema } from '@common/schemas/borsh';
|
|
8
|
+
import { toNativeSignedTransaction } from '@common/transformers/toNative/signedTransaction';
|
|
9
|
+
|
|
10
|
+
export const serializeNativeSignedTransaction = (
|
|
11
|
+
nativeSignedTransaction: NativeSignedTransaction,
|
|
12
|
+
): BorshBytes =>
|
|
13
|
+
serialize(signedTransactionBorshSchema, nativeSignedTransaction);
|
|
14
|
+
|
|
15
|
+
export const serializeSignedTransaction = (
|
|
16
|
+
signedTransaction: SignedTransaction,
|
|
17
|
+
): BorshBytes =>
|
|
18
|
+
serializeNativeSignedTransaction(
|
|
19
|
+
toNativeSignedTransaction(signedTransaction),
|
|
20
|
+
);
|