@octaflowlabs/onchain-sdk 1.0.0-test14 → 1.0.0-test16

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.
@@ -109,7 +109,7 @@ export const buildMaxNativeTransferTx = async (options) => {
109
109
  throw new Error('Max native transfer requires a native token address');
110
110
  const provisionalTx = await buildUnsignedTransferTx({
111
111
  ...options,
112
- value: options.balance,
112
+ value: '0',
113
113
  tokenAddress: options.tokenAddress,
114
114
  });
115
115
  if (!provisionalTx.gasReserve)
@@ -113,7 +113,7 @@ const buildMaxNativeTransferTx = async (options) => {
113
113
  throw new Error('Max native transfer requires a native token address');
114
114
  const provisionalTx = await (0, exports.buildUnsignedTransferTx)({
115
115
  ...options,
116
- value: options.balance,
116
+ value: '0',
117
117
  tokenAddress: options.tokenAddress,
118
118
  });
119
119
  if (!provisionalTx.gasReserve)
@@ -4,11 +4,14 @@ export { ERC20_TOKEN_CONTRACT_ABI };
4
4
  /** constants exports */
5
5
  export { GAS_LIMIT_PER_TX_TYPE } from './constants/constants';
6
6
  /** basic blockchain exports */
7
- export { buildMaxNativeTransferTx, buildUnsignedTransferTx } from './blockchain/buildUnsignedTransferTx';
7
+ export { buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
8
8
  export { broadcastTransaction } from './blockchain/broadcastTransaction';
9
9
  export { estimateGasLimitFromProvider } from './blockchain/estimateGasLimitFromProvider';
10
10
  export { getProvider } from './blockchain/getProvider';
11
11
  export { txStatus } from './blockchain/txStatus';
12
+ /** services exports */
13
+ export { EvmWalletService, EvmGeneratedWallet, EvmDerivedWallet, } from './services/evm-wallet-core/evmWalletService';
14
+ export { EntropySource } from './services/evm-wallet-core/entropy';
12
15
  /** utils exports */
13
16
  export { getShortenTransactionHash } from './utils/getShortenTxHash';
14
17
  export { transformBigInt } from './utils/transformBigInt';
package/dist/cjs/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.NATIVE_TOKENS = exports.transformBigInt = exports.getShortenTransactionHash = exports.txStatus = exports.getProvider = exports.estimateGasLimitFromProvider = exports.broadcastTransaction = exports.buildUnsignedTransferTx = exports.buildMaxNativeTransferTx = exports.GAS_LIMIT_PER_TX_TYPE = exports.ERC20_TOKEN_CONTRACT_ABI = void 0;
6
+ exports.NATIVE_TOKENS = exports.transformBigInt = exports.getShortenTransactionHash = exports.EvmWalletService = exports.txStatus = exports.getProvider = exports.estimateGasLimitFromProvider = exports.broadcastTransaction = exports.buildUnsignedTransferTx = exports.buildMaxNativeTransferTx = exports.GAS_LIMIT_PER_TX_TYPE = exports.ERC20_TOKEN_CONTRACT_ABI = void 0;
7
7
  /** ABIs exports */
8
8
  const ERC20_TOKEN_CONTRACT_ABI_1 = __importDefault(require("./ABIs/ERC20_TOKEN_CONTRACT_ABI"));
9
9
  exports.ERC20_TOKEN_CONTRACT_ABI = ERC20_TOKEN_CONTRACT_ABI_1.default;
@@ -22,6 +22,9 @@ var getProvider_1 = require("./blockchain/getProvider");
22
22
  Object.defineProperty(exports, "getProvider", { enumerable: true, get: function () { return getProvider_1.getProvider; } });
23
23
  var txStatus_1 = require("./blockchain/txStatus");
24
24
  Object.defineProperty(exports, "txStatus", { enumerable: true, get: function () { return txStatus_1.txStatus; } });
25
+ /** services exports */
26
+ var evmWalletService_1 = require("./services/evm-wallet-core/evmWalletService");
27
+ Object.defineProperty(exports, "EvmWalletService", { enumerable: true, get: function () { return evmWalletService_1.EvmWalletService; } });
25
28
  /** utils exports */
26
29
  var getShortenTxHash_1 = require("./utils/getShortenTxHash");
27
30
  Object.defineProperty(exports, "getShortenTransactionHash", { enumerable: true, get: function () { return getShortenTxHash_1.getShortenTransactionHash; } });
@@ -0,0 +1,3 @@
1
+ export interface EntropySource {
2
+ randomBytes(length: number): Uint8Array;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,27 @@
1
+ /** local imports */
2
+ import { EntropySource } from './entropy';
3
+ export interface EvmGeneratedWallet {
4
+ address: string;
5
+ mnemonic: string;
6
+ privateKey: string;
7
+ path: string;
8
+ }
9
+ export interface EvmDerivedWallet {
10
+ address: string;
11
+ privateKey: string;
12
+ path: string;
13
+ publicKey: string;
14
+ }
15
+ export declare class EvmWalletService {
16
+ private entropy;
17
+ private readonly DEFAULT_DERIVATION_PATH;
18
+ constructor(entropy: EntropySource);
19
+ generateNewWallet(accountIndex?: number, wordCount?: 12 | 15 | 18 | 21 | 24): EvmGeneratedWallet;
20
+ generateMultipleWallets(mnemonic: string, count: number, startIndex?: number): EvmDerivedWallet[];
21
+ deriveWalletFromMnemonic(mnemonic: string, accountIndex?: number, customPath?: string): EvmDerivedWallet;
22
+ importFromPrivateKey(privateKey: string): Omit<EvmDerivedWallet, 'path'>;
23
+ signMessage(privateKey: string, message: string): Promise<string>;
24
+ isValidMnemonic(mnemonic: string): boolean;
25
+ private validateMnemonic;
26
+ private getEntropyForWordCount;
27
+ }
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EvmWalletService = void 0;
4
+ /** npm imports */
5
+ const ethers_1 = require("ethers");
6
+ class EvmWalletService {
7
+ constructor(entropy) {
8
+ this.entropy = entropy;
9
+ this.DEFAULT_DERIVATION_PATH = "m/44'/60'/0'/0";
10
+ }
11
+ generateNewWallet(accountIndex = 0, wordCount = 12) {
12
+ const entropy = this.getEntropyForWordCount(wordCount);
13
+ const randomBytesArray = this.entropy.randomBytes(entropy);
14
+ const customMnemonic = ethers_1.Mnemonic.fromEntropy(randomBytesArray);
15
+ const path = `${this.DEFAULT_DERIVATION_PATH}/${accountIndex}`;
16
+ const hdNode = ethers_1.HDNodeWallet.fromMnemonic(customMnemonic, path);
17
+ return {
18
+ address: hdNode.address,
19
+ mnemonic: customMnemonic.phrase,
20
+ privateKey: hdNode.privateKey,
21
+ path,
22
+ };
23
+ }
24
+ generateMultipleWallets(mnemonic, count, startIndex = 0) {
25
+ this.validateMnemonic(mnemonic);
26
+ const wallets = [];
27
+ const mnemonicObject = ethers_1.Mnemonic.fromPhrase(mnemonic);
28
+ for (let i = 0; i < count; i++) {
29
+ const accountIndex = startIndex + i;
30
+ const path = `${this.DEFAULT_DERIVATION_PATH}/${accountIndex}`;
31
+ const hdNode = ethers_1.HDNodeWallet.fromMnemonic(mnemonicObject, path);
32
+ wallets.push({
33
+ address: hdNode.address,
34
+ privateKey: hdNode.privateKey,
35
+ path,
36
+ publicKey: hdNode.publicKey,
37
+ });
38
+ }
39
+ return wallets;
40
+ }
41
+ deriveWalletFromMnemonic(mnemonic, accountIndex = 0, customPath) {
42
+ this.validateMnemonic(mnemonic);
43
+ const path = customPath || `${this.DEFAULT_DERIVATION_PATH}/${accountIndex}`;
44
+ const mnemonicObject = ethers_1.Mnemonic.fromPhrase(mnemonic);
45
+ const hdNode = ethers_1.HDNodeWallet.fromMnemonic(mnemonicObject, path);
46
+ return {
47
+ address: hdNode.address,
48
+ privateKey: hdNode.privateKey,
49
+ path,
50
+ publicKey: hdNode.publicKey,
51
+ };
52
+ }
53
+ importFromPrivateKey(privateKey) {
54
+ const wallet = new ethers_1.Wallet(privateKey);
55
+ return {
56
+ address: wallet.address,
57
+ privateKey: wallet.privateKey,
58
+ publicKey: wallet.signingKey.publicKey,
59
+ };
60
+ }
61
+ async signMessage(privateKey, message) {
62
+ const wallet = new ethers_1.Wallet(privateKey);
63
+ return await wallet.signMessage(message);
64
+ }
65
+ isValidMnemonic(mnemonic) {
66
+ try {
67
+ ethers_1.Mnemonic.fromPhrase(mnemonic);
68
+ return true;
69
+ }
70
+ catch {
71
+ return false;
72
+ }
73
+ }
74
+ validateMnemonic(mnemonic) {
75
+ if (!this.isValidMnemonic(mnemonic)) {
76
+ throw new Error('Invalid mnemonic phrase');
77
+ }
78
+ }
79
+ getEntropyForWordCount(wordCount) {
80
+ const entropyMap = {
81
+ 12: 16, // 128 bits
82
+ 15: 20, // 160 bits
83
+ 18: 24, // 192 bits
84
+ 21: 28, // 224 bits
85
+ 24: 32, // 256 bits
86
+ };
87
+ return entropyMap[wordCount];
88
+ }
89
+ }
90
+ exports.EvmWalletService = EvmWalletService;
package/dist/index.d.ts CHANGED
@@ -4,11 +4,14 @@ export { ERC20_TOKEN_CONTRACT_ABI };
4
4
  /** constants exports */
5
5
  export { GAS_LIMIT_PER_TX_TYPE } from './constants/constants';
6
6
  /** basic blockchain exports */
7
- export { buildMaxNativeTransferTx, buildUnsignedTransferTx } from './blockchain/buildUnsignedTransferTx';
7
+ export { buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
8
8
  export { broadcastTransaction } from './blockchain/broadcastTransaction';
9
9
  export { estimateGasLimitFromProvider } from './blockchain/estimateGasLimitFromProvider';
10
10
  export { getProvider } from './blockchain/getProvider';
11
11
  export { txStatus } from './blockchain/txStatus';
12
+ /** services exports */
13
+ export { EvmWalletService, EvmGeneratedWallet, EvmDerivedWallet, } from './services/evm-wallet-core/evmWalletService';
14
+ export { EntropySource } from './services/evm-wallet-core/entropy';
12
15
  /** utils exports */
13
16
  export { getShortenTransactionHash } from './utils/getShortenTxHash';
14
17
  export { transformBigInt } from './utils/transformBigInt';
package/dist/index.js CHANGED
@@ -4,11 +4,13 @@ export { ERC20_TOKEN_CONTRACT_ABI };
4
4
  /** constants exports */
5
5
  export { GAS_LIMIT_PER_TX_TYPE } from './constants/constants';
6
6
  /** basic blockchain exports */
7
- export { buildMaxNativeTransferTx, buildUnsignedTransferTx } from './blockchain/buildUnsignedTransferTx';
7
+ export { buildMaxNativeTransferTx, buildUnsignedTransferTx, } from './blockchain/buildUnsignedTransferTx';
8
8
  export { broadcastTransaction } from './blockchain/broadcastTransaction';
9
9
  export { estimateGasLimitFromProvider } from './blockchain/estimateGasLimitFromProvider';
10
10
  export { getProvider } from './blockchain/getProvider';
11
11
  export { txStatus } from './blockchain/txStatus';
12
+ /** services exports */
13
+ export { EvmWalletService, } from './services/evm-wallet-core/evmWalletService';
12
14
  /** utils exports */
13
15
  export { getShortenTransactionHash } from './utils/getShortenTxHash';
14
16
  export { transformBigInt } from './utils/transformBigInt';
@@ -0,0 +1,3 @@
1
+ export interface EntropySource {
2
+ randomBytes(length: number): Uint8Array;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ /** local imports */
2
+ import { EntropySource } from './entropy';
3
+ export interface EvmGeneratedWallet {
4
+ address: string;
5
+ mnemonic: string;
6
+ privateKey: string;
7
+ path: string;
8
+ }
9
+ export interface EvmDerivedWallet {
10
+ address: string;
11
+ privateKey: string;
12
+ path: string;
13
+ publicKey: string;
14
+ }
15
+ export declare class EvmWalletService {
16
+ private entropy;
17
+ private readonly DEFAULT_DERIVATION_PATH;
18
+ constructor(entropy: EntropySource);
19
+ generateNewWallet(accountIndex?: number, wordCount?: 12 | 15 | 18 | 21 | 24): EvmGeneratedWallet;
20
+ generateMultipleWallets(mnemonic: string, count: number, startIndex?: number): EvmDerivedWallet[];
21
+ deriveWalletFromMnemonic(mnemonic: string, accountIndex?: number, customPath?: string): EvmDerivedWallet;
22
+ importFromPrivateKey(privateKey: string): Omit<EvmDerivedWallet, 'path'>;
23
+ signMessage(privateKey: string, message: string): Promise<string>;
24
+ isValidMnemonic(mnemonic: string): boolean;
25
+ private validateMnemonic;
26
+ private getEntropyForWordCount;
27
+ }
@@ -0,0 +1,86 @@
1
+ /** npm imports */
2
+ import { Wallet as EthersWallet, HDNodeWallet, Mnemonic } from 'ethers';
3
+ export class EvmWalletService {
4
+ constructor(entropy) {
5
+ this.entropy = entropy;
6
+ this.DEFAULT_DERIVATION_PATH = "m/44'/60'/0'/0";
7
+ }
8
+ generateNewWallet(accountIndex = 0, wordCount = 12) {
9
+ const entropy = this.getEntropyForWordCount(wordCount);
10
+ const randomBytesArray = this.entropy.randomBytes(entropy);
11
+ const customMnemonic = Mnemonic.fromEntropy(randomBytesArray);
12
+ const path = `${this.DEFAULT_DERIVATION_PATH}/${accountIndex}`;
13
+ const hdNode = HDNodeWallet.fromMnemonic(customMnemonic, path);
14
+ return {
15
+ address: hdNode.address,
16
+ mnemonic: customMnemonic.phrase,
17
+ privateKey: hdNode.privateKey,
18
+ path,
19
+ };
20
+ }
21
+ generateMultipleWallets(mnemonic, count, startIndex = 0) {
22
+ this.validateMnemonic(mnemonic);
23
+ const wallets = [];
24
+ const mnemonicObject = Mnemonic.fromPhrase(mnemonic);
25
+ for (let i = 0; i < count; i++) {
26
+ const accountIndex = startIndex + i;
27
+ const path = `${this.DEFAULT_DERIVATION_PATH}/${accountIndex}`;
28
+ const hdNode = HDNodeWallet.fromMnemonic(mnemonicObject, path);
29
+ wallets.push({
30
+ address: hdNode.address,
31
+ privateKey: hdNode.privateKey,
32
+ path,
33
+ publicKey: hdNode.publicKey,
34
+ });
35
+ }
36
+ return wallets;
37
+ }
38
+ deriveWalletFromMnemonic(mnemonic, accountIndex = 0, customPath) {
39
+ this.validateMnemonic(mnemonic);
40
+ const path = customPath || `${this.DEFAULT_DERIVATION_PATH}/${accountIndex}`;
41
+ const mnemonicObject = Mnemonic.fromPhrase(mnemonic);
42
+ const hdNode = HDNodeWallet.fromMnemonic(mnemonicObject, path);
43
+ return {
44
+ address: hdNode.address,
45
+ privateKey: hdNode.privateKey,
46
+ path,
47
+ publicKey: hdNode.publicKey,
48
+ };
49
+ }
50
+ importFromPrivateKey(privateKey) {
51
+ const wallet = new EthersWallet(privateKey);
52
+ return {
53
+ address: wallet.address,
54
+ privateKey: wallet.privateKey,
55
+ publicKey: wallet.signingKey.publicKey,
56
+ };
57
+ }
58
+ async signMessage(privateKey, message) {
59
+ const wallet = new EthersWallet(privateKey);
60
+ return await wallet.signMessage(message);
61
+ }
62
+ isValidMnemonic(mnemonic) {
63
+ try {
64
+ Mnemonic.fromPhrase(mnemonic);
65
+ return true;
66
+ }
67
+ catch {
68
+ return false;
69
+ }
70
+ }
71
+ validateMnemonic(mnemonic) {
72
+ if (!this.isValidMnemonic(mnemonic)) {
73
+ throw new Error('Invalid mnemonic phrase');
74
+ }
75
+ }
76
+ getEntropyForWordCount(wordCount) {
77
+ const entropyMap = {
78
+ 12: 16, // 128 bits
79
+ 15: 20, // 160 bits
80
+ 18: 24, // 192 bits
81
+ 21: 28, // 224 bits
82
+ 24: 32, // 256 bits
83
+ };
84
+ return entropyMap[wordCount];
85
+ }
86
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octaflowlabs/onchain-sdk",
3
- "version": "1.0.0-test14",
3
+ "version": "1.0.0-test16",
4
4
  "description": "onchain methods for web3",
5
5
  "repository": "https://github.com/crisramb665/onchain-sdk.git",
6
6
  "license": "MIT",