@aptos-labs/ts-sdk 0.0.3 → 0.0.4
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 +1 -0
- package/dist/browser/index.global.js +26 -25
- package/dist/browser/index.global.js.map +1 -1
- package/dist/cjs/index.d.ts +413 -227
- package/dist/cjs/index.js +451 -181
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +413 -227
- package/dist/esm/index.mjs +440 -178
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/index.d.ts +11 -17
- package/dist/types/index.js.map +1 -1
- package/package.json +3 -2
- package/src/api/account.ts +17 -18
- package/src/api/ans.ts +58 -0
- package/src/api/aptos.ts +7 -1
- package/src/api/coin.ts +3 -3
- package/src/api/digitalAsset.ts +9 -8
- package/src/api/event.ts +4 -3
- package/src/api/faucet.ts +3 -2
- package/src/api/general.ts +2 -2
- package/src/api/staking.ts +5 -4
- package/src/api/transactionSubmission.ts +27 -18
- package/src/bcs/deserializer.ts +4 -4
- package/src/bcs/serializable/moveStructs.ts +5 -9
- package/src/bcs/serializer.ts +4 -4
- package/src/client/core.ts +14 -6
- package/src/core/account.ts +83 -29
- package/src/core/accountAddress.ts +34 -30
- package/src/core/authenticationKey.ts +11 -9
- package/src/core/crypto/ed25519.ts +48 -1
- package/src/core/crypto/hdKey.ts +105 -0
- package/src/core/crypto/index.ts +1 -0
- package/src/core/crypto/secp256k1.ts +36 -0
- package/src/index.ts +0 -1
- package/src/internal/account.ts +80 -58
- package/src/internal/ans.ts +177 -0
- package/src/internal/coin.ts +5 -5
- package/src/internal/digitalAsset.ts +16 -17
- package/src/internal/event.ts +7 -7
- package/src/internal/faucet.ts +4 -4
- package/src/internal/general.ts +3 -3
- package/src/internal/staking.ts +8 -8
- package/src/internal/transactionSubmission.ts +71 -18
- package/src/transactions/instances/index.ts +1 -0
- package/src/transactions/instances/moduleId.ts +1 -1
- package/src/transactions/instances/rotationProofChallenge.ts +58 -0
- package/src/transactions/transactionBuilder/helpers.ts +5 -1
- package/src/transactions/transactionBuilder/remoteAbi.ts +3 -3
- package/src/transactions/transactionBuilder/transactionBuilder.ts +31 -45
- package/src/transactions/typeTag/index.ts +10 -10
- package/src/transactions/typeTag/parser.ts +1 -1
- package/src/transactions/types.ts +28 -17
- package/src/types/index.ts +11 -16
- package/src/utils/apiEndpoints.ts +8 -0
- package/src/version.ts +1 -1
- package/src/utils/hdKey.ts +0 -113
package/src/utils/hdKey.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// Copyright © Aptos Foundation
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { hmac } from "@noble/hashes/hmac";
|
|
5
|
-
import { sha512 } from "@noble/hashes/sha512";
|
|
6
|
-
import * as bip39 from "@scure/bip39";
|
|
7
|
-
|
|
8
|
-
export type DerivedKeys = {
|
|
9
|
-
key: Uint8Array;
|
|
10
|
-
chainCode: Uint8Array;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Aptos derive path is 637
|
|
15
|
-
*
|
|
16
|
-
* See https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
17
|
-
*/
|
|
18
|
-
export const APTOS_PATH_REGEX = /^m\/44'\/637'\/[0-9]+'\/[0-9]+'\/[0-9]+'?$/;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A list of supported key types and associated seeds
|
|
22
|
-
*/
|
|
23
|
-
export enum KeyType {
|
|
24
|
-
ED25519 = "ed25519 seed",
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const HARDENED_OFFSET = 0x80000000;
|
|
28
|
-
|
|
29
|
-
const deriveKey = (hashSeed: Uint8Array | string, data: Uint8Array | string): DerivedKeys => {
|
|
30
|
-
const digest = hmac.create(sha512, hashSeed).update(data).digest();
|
|
31
|
-
return {
|
|
32
|
-
key: digest.slice(0, 32),
|
|
33
|
-
chainCode: digest.slice(32),
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Derive a child key from the private key
|
|
39
|
-
* @param key
|
|
40
|
-
* @param chainCode
|
|
41
|
-
* @param index
|
|
42
|
-
* @constructor
|
|
43
|
-
*/
|
|
44
|
-
const CKDPriv = ({ key, chainCode }: DerivedKeys, index: number): DerivedKeys => {
|
|
45
|
-
const buffer = new ArrayBuffer(4);
|
|
46
|
-
new DataView(buffer).setUint32(0, index);
|
|
47
|
-
const indexBytes = new Uint8Array(buffer);
|
|
48
|
-
const zero = new Uint8Array([0]);
|
|
49
|
-
const data = new Uint8Array([...zero, ...key, ...indexBytes]);
|
|
50
|
-
|
|
51
|
-
return deriveKey(chainCode, data);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const removeApostrophes = (val: string): string => val.replace("'", "");
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Splits derive path into segments
|
|
58
|
-
* @param path
|
|
59
|
-
*/
|
|
60
|
-
const splitPath = (path: string): Array<string> => path.split("/").slice(1).map(removeApostrophes);
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Checks if the BIP44 path is valid for Aptos
|
|
64
|
-
* @param path the BIP44 path
|
|
65
|
-
*
|
|
66
|
-
* @returns true if the path is a valid Aptos path
|
|
67
|
-
*/
|
|
68
|
-
export const isValidPath = (path: string): boolean => {
|
|
69
|
-
if (!APTOS_PATH_REGEX.test(path)) {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
return !splitPath(path).some(Number.isNaN as any);
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Normalizes the mnemonic by removing extra whitespace and making it lowercase
|
|
77
|
-
* @param mnemonic the mnemonic seed phrase
|
|
78
|
-
*/
|
|
79
|
-
const mnemonicToSeed = (mnemonic: string): Uint8Array => {
|
|
80
|
-
const normalizedMnemonic = mnemonic
|
|
81
|
-
.trim()
|
|
82
|
-
.split(/\s+/)
|
|
83
|
-
.map((part) => part.toLowerCase())
|
|
84
|
-
.join(" ");
|
|
85
|
-
return bip39.mnemonicToSeedSync(normalizedMnemonic);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Derives a private key from a mnemonic seed phrase.
|
|
90
|
-
*
|
|
91
|
-
* To derive multiple keys from the same phrase, change the path
|
|
92
|
-
* @param keyType the key type seed used to derive keys
|
|
93
|
-
* @param path the BIP44 path
|
|
94
|
-
* @param seedPhrase the mnemonic seed phrase
|
|
95
|
-
* @param offset the offset used for key derivation, defaults to [HARDENED_OFFSET]
|
|
96
|
-
*/
|
|
97
|
-
export const derivePrivateKeyFromMnemonic = (
|
|
98
|
-
keyType: KeyType,
|
|
99
|
-
path: string,
|
|
100
|
-
seedPhrase: string,
|
|
101
|
-
offset = HARDENED_OFFSET,
|
|
102
|
-
): DerivedKeys => {
|
|
103
|
-
if (!isValidPath(path)) {
|
|
104
|
-
throw new Error("Invalid derivation path");
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Derive the master key from the mnemonic
|
|
108
|
-
const { key, chainCode } = deriveKey(keyType, mnemonicToSeed(seedPhrase));
|
|
109
|
-
const segments = splitPath(path).map((el) => parseInt(el, 10));
|
|
110
|
-
|
|
111
|
-
// Derive the child key based on the path
|
|
112
|
-
return segments.reduce((parentKeys, segment) => CKDPriv(parentKeys, segment + offset), { key, chainCode });
|
|
113
|
-
};
|