@cubist-labs/cubesigner-sdk-key-import 0.4.68-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.
@@ -0,0 +1,91 @@
1
+ import { encode as mpEncode } from "msgpackr";
2
+ import { wordlist } from "@scure/bip39/wordlists/english";
3
+ import * as bip39 from "@scure/bip39";
4
+
5
+ // The KeyPackage type from CubeSigner
6
+ export type MnemonicKeyPackage = {
7
+ EnglishMnemonic: {
8
+ mnemonic: {
9
+ entropy: Uint8Array;
10
+ };
11
+ der_path: {
12
+ path: number[];
13
+ };
14
+ password: string;
15
+ };
16
+ };
17
+
18
+ /**
19
+ * A BIP39 mnemonic to be imported, plus optional BIP39 password
20
+ * and BIP32 derivation path.
21
+ */
22
+ export type MnemonicToImport = {
23
+ mnemonic: string;
24
+ derivationPath?: string;
25
+ password?: string;
26
+ };
27
+
28
+ /**
29
+ * Create a new MnemonicKeyPackage value
30
+ *
31
+ * @param { MnemonicToImport } mne A BIP39 mnemonic and optional BIP39 password and BIP32 derivation path
32
+ * @return { Uint8Array } A serialized key package for import to CubeSigner
33
+ */
34
+ export function newMnemonicKeyPackage(mne: MnemonicToImport): Uint8Array {
35
+ const entropy = bip39.mnemonicToEntropy(mne.mnemonic, wordlist);
36
+ const path = !mne.derivationPath ? [] : parseDerivationPath(mne.derivationPath);
37
+ const password = mne.password ?? "";
38
+ const mnePkg = {
39
+ EnglishMnemonic: {
40
+ mnemonic: {
41
+ entropy,
42
+ },
43
+ der_path: {
44
+ path,
45
+ },
46
+ password,
47
+ },
48
+ };
49
+ return mpEncode(mnePkg);
50
+ }
51
+
52
+ // constants for derivation path parsing
53
+ const DER_HARDENED = 1n << 31n;
54
+ const DER_MAX = 1n << 32n;
55
+
56
+ /**
57
+ * Parse a derivation path into a sequence of 32-bit integers
58
+ *
59
+ * @param { string } derp The derivation path to parse; must start with 'm/'
60
+ * @return { number[] } The parsed path
61
+ */
62
+ export function parseDerivationPath(derp: string): number[] {
63
+ derp = derp.toLowerCase();
64
+ if (derp === "m") {
65
+ return [];
66
+ }
67
+ if (!derp.startsWith("m/")) {
68
+ throw new Error('Derivation path must start with "m/"');
69
+ }
70
+ const parts = derp.slice(2).split("/");
71
+ const ret = [];
72
+ for (let part of parts) {
73
+ let hardened = false;
74
+ if (part.endsWith("'") || part.endsWith("h")) {
75
+ hardened = true;
76
+ part = part.slice(0, part.length - 1);
77
+ }
78
+ if (part === "") {
79
+ throw new Error("Invalid derivation path: empty element");
80
+ }
81
+ let value = BigInt(part);
82
+ if (value >= DER_MAX) {
83
+ throw new Error("Derivation path element greater than 2^32 is invalid");
84
+ }
85
+ if (hardened) {
86
+ value = value | DER_HARDENED;
87
+ }
88
+ ret.push(Number(value));
89
+ }
90
+ return ret;
91
+ }
package/src/util.ts ADDED
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Converts a bigint to a big-endian Uint8Array of a specified length
3
+ *
4
+ * @param { bigint } n The value to convert
5
+ * @param { number } l The length in bytes
6
+ * @return { Uint8Array } The big-endian bytes
7
+ */
8
+ export function toBigEndian(n: bigint, l: number): Uint8Array {
9
+ if (n >= 1n << (8n * BigInt(l))) {
10
+ throw new Error(`Cannot convert ${n} to ${l} big-endian bytes (overflow)`);
11
+ }
12
+ let nn = n;
13
+ const ret = new Uint8Array(l);
14
+ for (let i = l - 1; i >= 0; --i) {
15
+ ret[i] = Number(nn % 256n);
16
+ nn = nn >> 8n;
17
+ }
18
+ return ret;
19
+ }
20
+
21
+ /**
22
+ * Concatenates an array of Uint8Arrays into a single array
23
+ *
24
+ * @param { Uint8Array[] } parts The parts to be concatenated
25
+ * @return { Uint8Array } The concatenated array
26
+ */
27
+ export function concatArrays(parts: Uint8Array[]): Uint8Array {
28
+ const totalLen = parts.reduce((len, part) => len + part.length, 0);
29
+
30
+ let lenSoFar = 0;
31
+ const ret = new Uint8Array(totalLen);
32
+ parts.forEach((part) => {
33
+ ret.set(part, lenSoFar);
34
+ lenSoFar += part.length;
35
+ });
36
+
37
+ return ret;
38
+ }
39
+
40
+ /**
41
+ * Get the current time in seconds since UNIX epoch
42
+ *
43
+ * @return { BigInt } Seconds since UNIX epoch
44
+ */
45
+ export function nowEpochMillis(): bigint {
46
+ return BigInt(Date.now());
47
+ }