@scallop-io/sui-kit 2.0.1 → 2.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.
@@ -1,93 +1,93 @@
1
- import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
2
- import { getKeyPair } from './keypair.js';
3
- import { hexOrBase64ToUint8Array, normalizePrivateKey } from './util.js';
4
- import { generateMnemonic } from './crypto.js';
5
- import type {
6
- AccountManagerParams,
7
- DerivePathParams,
8
- } from '../../types/index.js';
9
1
  import {
10
- SUI_PRIVATE_KEY_PREFIX,
11
- decodeSuiPrivateKey,
12
- } from '@mysten/sui/cryptography';
2
+ decodeSuiPrivateKey,
3
+ SUI_PRIVATE_KEY_PREFIX,
4
+ } from "@mysten/sui/cryptography";
5
+ import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
6
+ import type {
7
+ AccountManagerParams,
8
+ DerivePathParams,
9
+ } from "../../types/index.js";
10
+ import { generateMnemonic } from "./crypto.js";
11
+ import { getKeyPair } from "./keypair.js";
12
+ import { hexOrBase64ToUint8Array, normalizePrivateKey } from "./util.js";
13
13
 
14
14
  export class SuiAccountManager {
15
- private mnemonics: string;
16
- private secretKey: string;
17
- public currentKeyPair: Ed25519Keypair;
18
- public currentAddress: string;
15
+ private mnemonics: string;
16
+ private secretKey: string;
17
+ public currentKeyPair: Ed25519Keypair;
18
+ public currentAddress: string;
19
19
 
20
- /**
21
- * Support the following ways to init the SuiToolkit:
22
- * 1. mnemonics
23
- * 2. secretKey (base64 or hex)
24
- * If none of them is provided, will generate a random mnemonics with 24 words.
25
- *
26
- * @param mnemonics, 12 or 24 mnemonics words, separated by space
27
- * @param secretKey, base64 or hex string or Bech32 string, when mnemonics is provided, secretKey will be ignored
28
- */
29
- constructor({ mnemonics, secretKey }: AccountManagerParams = {}) {
30
- // If the mnemonics or secretKey is provided, use it
31
- // Otherwise, generate a random mnemonics with 24 words
32
- this.mnemonics = mnemonics || '';
33
- this.secretKey = secretKey || '';
34
- if (!this.mnemonics && !this.secretKey) {
35
- this.mnemonics = generateMnemonic(24);
36
- }
20
+ /**
21
+ * Support the following ways to init the SuiToolkit:
22
+ * 1. mnemonics
23
+ * 2. secretKey (base64 or hex)
24
+ * If none of them is provided, will generate a random mnemonics with 24 words.
25
+ *
26
+ * @param mnemonics, 12 or 24 mnemonics words, separated by space
27
+ * @param secretKey, base64 or hex string or Bech32 string, when mnemonics is provided, secretKey will be ignored
28
+ */
29
+ constructor({ mnemonics, secretKey }: AccountManagerParams = {}) {
30
+ // If the mnemonics or secretKey is provided, use it
31
+ // Otherwise, generate a random mnemonics with 24 words
32
+ this.mnemonics = mnemonics || "";
33
+ this.secretKey = secretKey || "";
34
+ if (!this.mnemonics && !this.secretKey) {
35
+ this.mnemonics = generateMnemonic(24);
36
+ }
37
37
 
38
- // Init the current account
39
- this.currentKeyPair = this.secretKey
40
- ? this.parseSecretKey(this.secretKey)
41
- : getKeyPair(this.mnemonics);
42
- this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
43
- }
38
+ // Init the current account
39
+ this.currentKeyPair = this.secretKey
40
+ ? this.parseSecretKey(this.secretKey)
41
+ : getKeyPair(this.mnemonics);
42
+ this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
43
+ }
44
44
 
45
- /**
46
- * Check if the secretKey starts with bench32 format
47
- */
48
- parseSecretKey(secretKey: string) {
49
- if (secretKey.startsWith(SUI_PRIVATE_KEY_PREFIX)) {
50
- const { secretKey: uint8ArraySecretKey } = decodeSuiPrivateKey(secretKey);
51
- return Ed25519Keypair.fromSecretKey(
52
- normalizePrivateKey(uint8ArraySecretKey)
53
- );
54
- }
45
+ /**
46
+ * Check if the secretKey starts with bench32 format
47
+ */
48
+ parseSecretKey(secretKey: string) {
49
+ if (secretKey.startsWith(SUI_PRIVATE_KEY_PREFIX)) {
50
+ const { secretKey: uint8ArraySecretKey } = decodeSuiPrivateKey(secretKey);
51
+ return Ed25519Keypair.fromSecretKey(
52
+ normalizePrivateKey(uint8ArraySecretKey),
53
+ );
54
+ }
55
55
 
56
- return Ed25519Keypair.fromSecretKey(
57
- normalizePrivateKey(hexOrBase64ToUint8Array(secretKey))
58
- );
59
- }
56
+ return Ed25519Keypair.fromSecretKey(
57
+ normalizePrivateKey(hexOrBase64ToUint8Array(secretKey)),
58
+ );
59
+ }
60
60
 
61
- /**
62
- * if derivePathParams is not provided or mnemonics is empty, it will return the currentKeyPair.
63
- * else:
64
- * it will generate keyPair from the mnemonic with the given derivePathParams.
65
- */
66
- getKeyPair(derivePathParams?: DerivePathParams) {
67
- if (!derivePathParams || !this.mnemonics) return this.currentKeyPair;
68
- return getKeyPair(this.mnemonics, derivePathParams);
69
- }
61
+ /**
62
+ * if derivePathParams is not provided or mnemonics is empty, it will return the currentKeyPair.
63
+ * else:
64
+ * it will generate keyPair from the mnemonic with the given derivePathParams.
65
+ */
66
+ getKeyPair(derivePathParams?: DerivePathParams) {
67
+ if (!derivePathParams || !this.mnemonics) return this.currentKeyPair;
68
+ return getKeyPair(this.mnemonics, derivePathParams);
69
+ }
70
70
 
71
- /**
72
- * if derivePathParams is not provided or mnemonics is empty, it will return the currentAddress.
73
- * else:
74
- * it will generate address from the mnemonic with the given derivePathParams.
75
- */
76
- getAddress(derivePathParams?: DerivePathParams) {
77
- if (!derivePathParams || !this.mnemonics) return this.currentAddress;
78
- return getKeyPair(this.mnemonics, derivePathParams)
79
- .getPublicKey()
80
- .toSuiAddress();
81
- }
71
+ /**
72
+ * if derivePathParams is not provided or mnemonics is empty, it will return the currentAddress.
73
+ * else:
74
+ * it will generate address from the mnemonic with the given derivePathParams.
75
+ */
76
+ getAddress(derivePathParams?: DerivePathParams) {
77
+ if (!derivePathParams || !this.mnemonics) return this.currentAddress;
78
+ return getKeyPair(this.mnemonics, derivePathParams)
79
+ .getPublicKey()
80
+ .toSuiAddress();
81
+ }
82
82
 
83
- /**
84
- * Switch the current account with the given derivePathParams.
85
- * This is only useful when the mnemonics is provided. For secretKey mode, it will always use the same account.
86
- */
87
- switchAccount(derivePathParams: DerivePathParams) {
88
- if (this.mnemonics) {
89
- this.currentKeyPair = getKeyPair(this.mnemonics, derivePathParams);
90
- this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
91
- }
92
- }
83
+ /**
84
+ * Switch the current account with the given derivePathParams.
85
+ * This is only useful when the mnemonics is provided. For secretKey mode, it will always use the same account.
86
+ */
87
+ switchAccount(derivePathParams: DerivePathParams) {
88
+ if (this.mnemonics) {
89
+ this.currentKeyPair = getKeyPair(this.mnemonics, derivePathParams);
90
+ this.currentAddress = this.currentKeyPair.getPublicKey().toSuiAddress();
91
+ }
92
+ }
93
93
  }
@@ -1,19 +1,19 @@
1
- import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
2
- import type { DerivePathParams } from '../../types/index.js';
1
+ import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
2
+ import type { DerivePathParams } from "../../types/index.js";
3
3
 
4
4
  /**
5
5
  * @description Get ed25519 derive path for SUI
6
6
  * @param derivePathParams
7
7
  */
8
8
  export const getDerivePathForSUI = (
9
- derivePathParams: DerivePathParams = {}
9
+ derivePathParams: DerivePathParams = {},
10
10
  ) => {
11
- const {
12
- accountIndex = 0,
13
- isExternal = false,
14
- addressIndex = 0,
15
- } = derivePathParams;
16
- return `m/44'/784'/${accountIndex}'/${isExternal ? 1 : 0}'/${addressIndex}'`;
11
+ const {
12
+ accountIndex = 0,
13
+ isExternal = false,
14
+ addressIndex = 0,
15
+ } = derivePathParams;
16
+ return `m/44'/784'/${accountIndex}'/${isExternal ? 1 : 0}'/${addressIndex}'`;
17
17
  };
18
18
 
19
19
  /**
@@ -30,9 +30,9 @@ export const getDerivePathForSUI = (
30
30
  * @param derivePathParams
31
31
  */
32
32
  export const getKeyPair = (
33
- mnemonics: string,
34
- derivePathParams: DerivePathParams = {}
33
+ mnemonics: string,
34
+ derivePathParams: DerivePathParams = {},
35
35
  ) => {
36
- const derivePath = getDerivePathForSUI(derivePathParams);
37
- return Ed25519Keypair.deriveKeypair(mnemonics, derivePath);
36
+ const derivePath = getDerivePathForSUI(derivePathParams);
37
+ return Ed25519Keypair.deriveKeypair(mnemonics, derivePath);
38
38
  };
@@ -1,11 +1,11 @@
1
- import { fromBase64, fromHex } from '@mysten/bcs';
1
+ import { fromBase64, fromHex } from "@mysten/bcs";
2
2
 
3
3
  /**
4
4
  * @description This regular expression matches any string that contains only hexadecimal digits (0-9, A-F, a-f).
5
5
  * @param str
6
6
  */
7
7
  export const isHex = (str: string) =>
8
- /^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(str);
8
+ /^0x[0-9a-fA-F]+$|^[0-9a-fA-F]+$/.test(str);
9
9
 
10
10
  /**
11
11
  * @description This regular expression matches any string that contains only base64 digits (0-9, A-Z, a-z, +, /, =).
@@ -19,13 +19,13 @@ export const isBase64 = (str: string) => /^[a-zA-Z0-9+/]+={0,2}$/g.test(str);
19
19
  * @description Convert a hex or base64 string to Uint8Array
20
20
  */
21
21
  export const hexOrBase64ToUint8Array = (str: string): Uint8Array => {
22
- if (isHex(str)) {
23
- return fromHex(str);
24
- }
25
- if (isBase64(str)) {
26
- return fromBase64(str);
27
- }
28
- throw new Error('The string is not a valid hex or base64 string.');
22
+ if (isHex(str)) {
23
+ return fromHex(str);
24
+ }
25
+ if (isBase64(str)) {
26
+ return fromBase64(str);
27
+ }
28
+ throw new Error("The string is not a valid hex or base64 string.");
29
29
  };
30
30
 
31
31
  const PRIVATE_KEY_SIZE = 32;
@@ -39,19 +39,19 @@ const LEGACY_PRIVATE_KEY_SIZE = 64;
39
39
  * 3. A 33-byte array with the first byte being 0x00 (sui.keystore key is a Base64 string with scheme flag 0x00 at the beginning)
40
40
  */
41
41
  export const normalizePrivateKey = (key: Uint8Array): Uint8Array => {
42
- if (key.length === LEGACY_PRIVATE_KEY_SIZE) {
43
- return key.slice(0, PRIVATE_KEY_SIZE);
44
- }
45
- if (key.length === PRIVATE_KEY_SIZE + 1 && key[0] === 0) {
46
- return key.slice(1);
47
- }
48
- if (key.length === PRIVATE_KEY_SIZE) {
49
- return key;
50
- }
51
- throw new Error('invalid secret key');
42
+ if (key.length === LEGACY_PRIVATE_KEY_SIZE) {
43
+ return key.slice(0, PRIVATE_KEY_SIZE);
44
+ }
45
+ if (key.length === PRIVATE_KEY_SIZE + 1 && key[0] === 0) {
46
+ return key.slice(1);
47
+ }
48
+ if (key.length === PRIVATE_KEY_SIZE) {
49
+ return key;
50
+ }
51
+ throw new Error("invalid secret key");
52
52
  };
53
53
 
54
54
  /**
55
55
  * @deprecated Please use fromHex and fromBase64 from '@mysten/bcs' directly.
56
56
  */
57
- export { fromHex, fromBase64 } from '@mysten/bcs';
57
+ export { fromBase64, fromHex } from "@mysten/bcs";
@@ -1,7 +1,7 @@
1
1
  export {
2
- SuiInteractor,
3
- getFullnodeUrl,
4
- type SuiObjectData,
5
- type SuiObjectDataOptions,
6
- type SimulateTransactionResponse,
7
- } from './suiInteractor.js';
2
+ getFullnodeUrl,
3
+ type SimulateTransactionResponse,
4
+ SuiInteractor,
5
+ type SuiObjectData,
6
+ type SuiObjectDataOptions,
7
+ } from "./suiInteractor.js";