@cityofzion/blockchain-service 1.15.0 → 1.15.2

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, untilIndex?: number): Promise<Map<BSName, Account<BSName>[]>>;
13
+ generateAccountsFromMnemonic(mnemonic: string, untilIndexByBlockchainService?: UntilIndexRecord<BSName>): Promise<Map<BSName, Account<BSName>[]>>;
14
14
  }
@@ -56,16 +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, untilIndex) {
59
+ generateAccountsFromMnemonic(mnemonic, untilIndexByBlockchainService) {
60
60
  return __awaiter(this, void 0, void 0, function* () {
61
- if (untilIndex === undefined) {
62
- return (0, functions_1.fetchAccountsForBlockchainServices)(__classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f"), (service, index) => __awaiter(this, void 0, void 0, function* () {
63
- return service.generateAccountFromMnemonic(mnemonic, index);
64
- }));
65
- }
66
- return (0, functions_1.generateAccountUntilIndexForBlockchainService)(__classPrivateFieldGet(this, _BSAggregator_blockchainServices, "f"), untilIndex, (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* () {
67
62
  return service.generateAccountFromMnemonic(mnemonic, index);
68
- }));
63
+ }), untilIndexByBlockchainService);
69
64
  });
70
65
  }
71
66
  }
@@ -1,6 +1,7 @@
1
- import { Account, BlockchainService, BSCalculableFee, BSClaimable, BSWithExplorerService, BSWithLedger, BSWithNameService, BSWithNft } from './interfaces';
1
+ import { Account, BlockchainService, BSCalculableFee, BSClaimable, BSMigrationNeo3, 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
+ export declare function hasMigrationNeo3<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSMigrationNeo3<BSName>;
4
5
  export declare function isCalculableFee<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSCalculableFee<BSName>;
5
6
  export declare function hasNft<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithNft;
6
7
  export declare function hasExplorerService<BSName extends string = string>(service: BlockchainService<BSName>): service is BlockchainService<BSName> & BSWithExplorerService;
@@ -11,8 +12,9 @@ export declare function wait(ms: number): Promise<unknown>;
11
12
  */
12
13
  export declare function waitForTransaction<BSName extends string = string>(service: BlockchainService<BSName>, txId: string): Promise<boolean>;
13
14
  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>[]>>;
15
- export declare function generateAccountUntilIndexForBlockchainService<BSName extends string = string>(blockchainServices: BlockchainService<BSName>[], untilIndex: number, getAccountCallback: (service: BlockchainService<BSName>, index: number) => Promise<Account<BSName>>): Promise<Map<BSName, Account<BSName>[]>>;
15
+ 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>[]>;
16
+ 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>[]>;
17
+ 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>[]>>;
16
18
  export declare function normalizeHash(hash: string): string;
17
19
  export declare function denormalizeHash(hash: string): string;
18
20
  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.generateAccountUntilIndexForBlockchainService = 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.hasMigrationNeo3 = exports.isClaimable = exports.hasNameService = void 0;
13
13
  function hasNameService(service) {
14
14
  return 'resolveNameServiceDomain' in service && 'validateNameServiceDomainFormat' in service;
15
15
  }
@@ -18,6 +18,10 @@ function isClaimable(service) {
18
18
  return 'claim' in service && 'claimToken' in service && 'getUnclaimed' in service.blockchainDataService;
19
19
  }
20
20
  exports.isClaimable = isClaimable;
21
+ function hasMigrationNeo3(service) {
22
+ return 'migrateToNeo3' in service && 'calculateToMigrateToNeo3Values' in service;
23
+ }
24
+ exports.hasMigrationNeo3 = hasMigrationNeo3;
21
25
  function isCalculableFee(service) {
22
26
  return 'calculateTransferFee' in service;
23
27
  }
@@ -81,58 +85,64 @@ function waitForAccountTransaction(service, txId, account, maxAttempts = 10) {
81
85
  });
82
86
  }
83
87
  exports.waitForAccountTransaction = waitForAccountTransaction;
84
- function fetchAccountsForBlockchainServices(blockchainServices, getAccountCallback) {
88
+ function fetchAccounts(blockchainServices, initialIndex, getAccountCallback) {
85
89
  return __awaiter(this, void 0, void 0, function* () {
86
- const accountsByBlockchainService = new Map();
87
- 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++;
90
+ const accounts = [];
91
+ let index = initialIndex;
92
+ let shouldBreak = false;
93
+ while (!shouldBreak) {
94
+ const generatedAccount = yield getAccountCallback(blockchainServices, index);
95
+ try {
96
+ const { transactions } = yield blockchainServices.blockchainDataService.getTransactionsByAddress({
97
+ address: generatedAccount.address,
98
+ });
99
+ if (!transactions || transactions.length <= 0)
100
+ shouldBreak = true;
107
101
  }
108
- accountsByBlockchainService.set(service.name, accounts);
109
- }));
110
- yield Promise.allSettled(promises);
111
- return accountsByBlockchainService;
102
+ catch (_b) {
103
+ shouldBreak = true;
104
+ }
105
+ accounts.push(generatedAccount);
106
+ index++;
107
+ }
108
+ return accounts;
112
109
  });
113
110
  }
114
- exports.fetchAccountsForBlockchainServices = fetchAccountsForBlockchainServices;
115
- function generateAccountUntilIndexForBlockchainService(blockchainServices, untilIndex, getAccountCallback) {
111
+ exports.fetchAccounts = fetchAccounts;
112
+ function generateAccount(blockchainServices, initialIndex, untilIndex, getAccountCallback) {
116
113
  return __awaiter(this, void 0, void 0, function* () {
117
- if (untilIndex < 0) {
118
- throw new Error('Invalid index');
114
+ const accounts = [];
115
+ let index = initialIndex;
116
+ while (index <= untilIndex) {
117
+ const generatedAccount = yield getAccountCallback(blockchainServices, index);
118
+ accounts.push(generatedAccount);
119
+ index++;
119
120
  }
121
+ return accounts;
122
+ });
123
+ }
124
+ exports.generateAccount = generateAccount;
125
+ function generateAccountForBlockchainService(blockchainServices, getAccountCallback, untilIndexByBlockchainService) {
126
+ return __awaiter(this, void 0, void 0, function* () {
120
127
  const accountsByBlockchainService = new Map();
121
128
  const promises = blockchainServices.map((service) => __awaiter(this, void 0, void 0, function* () {
122
- let index = 0;
123
- const accounts = [];
124
- while (index <= untilIndex) {
125
- const generatedAccount = yield getAccountCallback(service, index);
126
- accounts.push(generatedAccount);
127
- index++;
129
+ var _b;
130
+ const firstAccount = yield getAccountCallback(service, 0);
131
+ const untilIndex = (_b = untilIndexByBlockchainService === null || untilIndexByBlockchainService === void 0 ? void 0 : untilIndexByBlockchainService[service.name]) === null || _b === void 0 ? void 0 : _b[firstAccount.address];
132
+ if (untilIndex === undefined) {
133
+ const accounts = yield fetchAccounts(service, 1, getAccountCallback);
134
+ accountsByBlockchainService.set(service.name, [firstAccount, ...accounts]);
135
+ }
136
+ else {
137
+ const accounts = yield generateAccount(service, 1, untilIndex, getAccountCallback);
138
+ accountsByBlockchainService.set(service.name, [firstAccount, ...accounts]);
128
139
  }
129
- accountsByBlockchainService.set(service.name, accounts);
130
140
  }));
131
- yield Promise.allSettled(promises);
141
+ yield Promise.all(promises);
132
142
  return accountsByBlockchainService;
133
143
  });
134
144
  }
135
- exports.generateAccountUntilIndexForBlockchainService = generateAccountUntilIndexForBlockchainService;
145
+ exports.generateAccountForBlockchainService = generateAccountForBlockchainService;
136
146
  function normalizeHash(hash) {
137
147
  return hash.replace('0x', '').toLowerCase();
138
148
  }
@@ -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';
@@ -60,6 +61,23 @@ export interface BSClaimable<BSName extends string = string> {
60
61
  blockchainDataService: BlockchainDataService & BDSClaimable;
61
62
  claim(account: Account<BSName>): Promise<string>;
62
63
  }
64
+ export type MigrateToNeo3Params<BSName extends string = string> = {
65
+ account: Account<BSName>;
66
+ address: string;
67
+ };
68
+ export type CalculateToMigrateToNeo3ValuesParams<BSName extends string = string> = {
69
+ account: Account<BSName>;
70
+ };
71
+ export type CalculateToMigrateToNeo3ValuesResponse = {
72
+ gasMigrationTotalFees?: string;
73
+ neoMigrationTotalFees?: string;
74
+ gasMigrationAmount?: string;
75
+ neoMigrationAmount?: string;
76
+ };
77
+ export interface BSMigrationNeo3<BSName extends string = string> {
78
+ migrateToNeo3(params: MigrateToNeo3Params<BSName>): Promise<string[]>;
79
+ calculateToMigrateToNeo3Values(params: CalculateToMigrateToNeo3ValuesParams<BSName>): Promise<CalculateToMigrateToNeo3ValuesResponse>;
80
+ }
63
81
  export interface BSWithNameService {
64
82
  resolveNameServiceDomain(domainName: string): Promise<string>;
65
83
  validateNameServiceDomainFormat(domainName: string): boolean;
@@ -226,7 +244,7 @@ export type GetLedgerTransport<BSName extends string = string> = (account: Accou
226
244
  export interface LedgerService<BSName extends string = string> {
227
245
  emitter: LedgerServiceEmitter;
228
246
  getLedgerTransport?: GetLedgerTransport<BSName>;
229
- getAccounts(transport: Transport, untilIndex?: number): Promise<Account<BSName>[]>;
247
+ getAccounts(transport: Transport, getUntilIndex?: UntilIndexRecord<BSName>): Promise<Account<BSName>[]>;
230
248
  getAccount(transport: Transport, index: number): Promise<Account<BSName>>;
231
249
  }
232
250
  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.15.0",
3
+ "version": "1.15.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": "https://github.com/CityOfZion/blockchain-services",