@cityofzion/blockchain-service 1.14.0 → 1.15.1

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,4 +1,4 @@
1
- import { Account, BlockchainService } from './interfaces';
1
+ import { Account, BlockchainService, UntilIndexRecord } from './interfaces';
2
2
  export declare class BSAggregator<BSName extends string = string> {
3
3
  #private;
4
4
  readonly blockchainServicesByName: Record<BSName, BlockchainService<BSName>>;
@@ -10,5 +10,5 @@ export declare class BSAggregator<BSName extends string = string> {
10
10
  getBlockchainNameByAddress(address: string): BSName[];
11
11
  getBlockchainNameByKey(wif: string): BSName[];
12
12
  getBlockchainNameByEncrypted(keyOrJson: string): BSName[];
13
- generateAccountsFromMnemonic(mnemonic: string): Promise<Map<BSName, Account<BSName>[]>>;
13
+ generateAccountsFromMnemonic(mnemonic: string, untilIndexByBlockchainService?: UntilIndexRecord<BSName>): Promise<Map<BSName, Account<BSName>[]>>;
14
14
  }
@@ -56,11 +56,11 @@ class BSAggregator {
56
56
  getBlockchainNameByEncrypted(keyOrJson) {
57
57
  return __classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f").filter(bs => bs.validateEncrypted(keyOrJson)).map(bs => bs.name);
58
58
  }
59
- generateAccountsFromMnemonic(mnemonic) {
59
+ generateAccountsFromMnemonic(mnemonic, untilIndexByBlockchainService) {
60
60
  return __awaiter(this, void 0, void 0, function* () {
61
- return (0, functions_1.fetchAccountsForBlockchainServices)(__classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f"), (service, index) => __awaiter(this, void 0, void 0, function* () {
61
+ return (0, functions_1.generateAccountForBlockchainService)(__classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f"), (service, index) => __awaiter(this, void 0, void 0, function* () {
62
62
  return service.generateAccountFromMnemonic(mnemonic, index);
63
- }));
63
+ }), untilIndexByBlockchainService);
64
64
  });
65
65
  }
66
66
  }
@@ -1,4 +1,4 @@
1
- import { Account, BlockchainService, BSCalculableFee, BSClaimable, BSWithExplorerService, BSWithLedger, BSWithNameService, BSWithNft } from './interfaces';
1
+ import { Account, BlockchainService, BSCalculableFee, BSClaimable, BSWithExplorerService, BSWithLedger, BSWithNameService, BSWithNft, UntilIndexRecord } from './interfaces';
2
2
  export declare function hasNameService<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithNameService;
3
3
  export declare function isClaimable<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSClaimable<BSName>;
4
4
  export declare function isCalculableFee<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSCalculableFee<BSName>;
@@ -11,7 +11,9 @@ export declare function wait(ms: number): Promise<unknown>;
11
11
  */
12
12
  export declare function waitForTransaction<BSName extends string = string>(service: BlockchainService<BSName>, txId: string): Promise<boolean>;
13
13
  export declare function waitForAccountTransaction<BSName extends string = string>(service: BlockchainService<BSName>, txId: string, account: Account<BSName>, maxAttempts?: number): Promise<boolean>;
14
- export declare function fetchAccountsForBlockchainServices<BSName extends string = string>(blockchainServices: BlockchainService<BSName>[], getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>): Promise<Map<BSName, Account<BSName>[]>>;
14
+ export declare function fetchAccounts<BSName extends string = string>(blockchainServices: BlockchainService<BSName>, initialIndex: number, getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>): Promise<Account<BSName>[]>;
15
+ export declare function generateAccount<BSName extends string = string>(blockchainServices: BlockchainService<BSName>, initialIndex: number, untilIndex: number, getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>): Promise<Account<BSName>[]>;
16
+ export declare function generateAccountForBlockchainService<BSName extends string = string>(blockchainServices: BlockchainService<BSName>[], getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>, untilIndexByBlockchainService?: UntilIndexRecord<BSName>): Promise<Map<BSName, Account<BSName>[]>>;
15
17
  export declare function normalizeHash(hash: string): string;
16
18
  export declare function denormalizeHash(hash: string): string;
17
19
  export declare function countDecimals(value: string | number): number;
package/dist/functions.js CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.formatNumber = exports.countDecimals = exports.denormalizeHash = exports.normalizeHash = exports.fetchAccountsForBlockchainServices = exports.waitForAccountTransaction = exports.waitForTransaction = exports.wait = exports.hasLedger = exports.hasExplorerService = exports.hasNft = exports.isCalculableFee = exports.isClaimable = exports.hasNameService = void 0;
12
+ exports.formatNumber = exports.countDecimals = exports.denormalizeHash = exports.normalizeHash = exports.generateAccountForBlockchainService = exports.generateAccount = exports.fetchAccounts = exports.waitForAccountTransaction = exports.waitForTransaction = exports.wait = exports.hasLedger = exports.hasExplorerService = exports.hasNft = exports.isCalculableFee = exports.isClaimable = exports.hasNameService = void 0;
13
13
  function hasNameService(service) {
14
14
  return 'resolveNameServiceDomain' in service && 'validateNameServiceDomainFormat' in service;
15
15
  }
@@ -81,37 +81,64 @@ function waitForAccountTransaction(service, txId, account, maxAttempts = 10) {
81
81
  });
82
82
  }
83
83
  exports.waitForAccountTransaction = waitForAccountTransaction;
84
- function fetchAccountsForBlockchainServices(blockchainServices, getAccountCallback) {
84
+ function fetchAccounts(blockchainServices, initialIndex, getAccountCallback) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ const accounts = [];
87
+ let index = initialIndex;
88
+ let shouldBreak = false;
89
+ while (!shouldBreak) {
90
+ const generatedAccount = yield getAccountCallback(blockchainServices, index);
91
+ try {
92
+ const { transactions } = yield blockchainServices.blockchainDataService.getTransactionsByAddress({
93
+ address: generatedAccount.address,
94
+ });
95
+ if (!transactions || transactions.length <= 0)
96
+ shouldBreak = true;
97
+ }
98
+ catch (_b) {
99
+ shouldBreak = true;
100
+ }
101
+ accounts.push(generatedAccount);
102
+ index++;
103
+ }
104
+ return accounts;
105
+ });
106
+ }
107
+ exports.fetchAccounts = fetchAccounts;
108
+ function generateAccount(blockchainServices, initialIndex, untilIndex, getAccountCallback) {
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ const accounts = [];
111
+ let index = initialIndex;
112
+ while (index <= untilIndex) {
113
+ const generatedAccount = yield getAccountCallback(blockchainServices, index);
114
+ accounts.push(generatedAccount);
115
+ index++;
116
+ }
117
+ return accounts;
118
+ });
119
+ }
120
+ exports.generateAccount = generateAccount;
121
+ function generateAccountForBlockchainService(blockchainServices, getAccountCallback, untilIndexByBlockchainService) {
85
122
  return __awaiter(this, void 0, void 0, function* () {
86
123
  const accountsByBlockchainService = new Map();
87
124
  const promises = blockchainServices.map((service) => __awaiter(this, void 0, void 0, function* () {
88
- let index = 0;
89
- const accounts = [];
90
- let shouldBreak = false;
91
- while (!shouldBreak) {
92
- const generatedAccount = yield getAccountCallback(service, index);
93
- if (index !== 0) {
94
- try {
95
- const { transactions } = yield service.blockchainDataService.getTransactionsByAddress({
96
- address: generatedAccount.address,
97
- });
98
- if (!transactions || transactions.length <= 0)
99
- shouldBreak = true;
100
- }
101
- catch (_b) {
102
- shouldBreak = true;
103
- }
104
- }
105
- accounts.push(generatedAccount);
106
- index++;
125
+ var _b;
126
+ const firstAccount = yield getAccountCallback(service, 0);
127
+ const untilIndex = (_b = untilIndexByBlockchainService === null || untilIndexByBlockchainService === void 0 ? void 0 : untilIndexByBlockchainService[service.name]) === null || _b === void 0 ? void 0 : _b[firstAccount.address];
128
+ if (untilIndex === undefined) {
129
+ const accounts = yield fetchAccounts(service, 1, getAccountCallback);
130
+ accountsByBlockchainService.set(service.name, [firstAccount, ...accounts]);
131
+ }
132
+ else {
133
+ const accounts = yield generateAccount(service, 1, untilIndex, getAccountCallback);
134
+ accountsByBlockchainService.set(service.name, [firstAccount, ...accounts]);
107
135
  }
108
- accountsByBlockchainService.set(service.name, accounts);
109
136
  }));
110
137
  yield Promise.all(promises);
111
138
  return accountsByBlockchainService;
112
139
  });
113
140
  }
114
- exports.fetchAccountsForBlockchainServices = fetchAccountsForBlockchainServices;
141
+ exports.generateAccountForBlockchainService = generateAccountForBlockchainService;
115
142
  function normalizeHash(hash) {
116
143
  return hash.replace('0x', '').toLowerCase();
117
144
  }
@@ -1,5 +1,6 @@
1
1
  import Transport from '@ledgerhq/hw-transport';
2
2
  import TypedEmitter from 'typed-emitter';
3
+ export type UntilIndexRecord<BSName extends string = string> = Partial<Record<BSName, Record<string, number>>>;
3
4
  export type Account<BSName extends string = string> = {
4
5
  key: string;
5
6
  type: 'wif' | 'privateKey' | 'publicKey';
@@ -226,7 +227,7 @@ export type GetLedgerTransport<BSName extends string = string> = (account: Accou
226
227
  export interface LedgerService<BSName extends string = string> {
227
228
  emitter: LedgerServiceEmitter;
228
229
  getLedgerTransport?: GetLedgerTransport<BSName>;
229
- getAccounts(transport: Transport): Promise<Account<BSName>[]>;
230
+ getAccounts(transport: Transport, getUntilIndex?: UntilIndexRecord<BSName>): Promise<Account<BSName>[]>;
230
231
  getAccount(transport: Transport, index: number): Promise<Account<BSName>>;
231
232
  }
232
233
  export type SwapServiceToken<BSName extends string = string> = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cityofzion/blockchain-service",
3
- "version": "1.14.0",
3
+ "version": "1.15.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": "https://github.com/CityOfZion/blockchain-services",