@cityofzion/blockchain-service 0.7.0 → 0.7.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.
@@ -0,0 +1,16 @@
1
+ import { AccountWithDerivationPath, BlockchainService } from './interfaces';
2
+ export declare class BSAggregator<BSCustomName extends string = string, BSCustom extends BlockchainService<BSCustomName> = BlockchainService<BSCustomName>> {
3
+ readonly blockchainServicesByName: Record<BSCustomName, BSCustom>;
4
+ readonly blockchainServices: BlockchainService<BSCustomName>[];
5
+ constructor(blockchainServices: Record<BSCustomName, BSCustom>);
6
+ addBlockchain(name: BSCustomName, blockchain: BSCustom): void;
7
+ validateAddressAllBlockchains(address: string): boolean;
8
+ validateTextAllBlockchains(text: string): boolean;
9
+ validateKeyAllBlockchains(wif: string): boolean;
10
+ validateEncryptedAllBlockchains(keyOrJson: string): boolean;
11
+ getBlockchainByName(name: BSCustomName): BlockchainService<BSCustomName>;
12
+ getBlockchainByAddress(address: string): BlockchainService<BSCustomName> | undefined;
13
+ getBlockchainByKey(wif: string): BlockchainService<BSCustomName> | undefined;
14
+ getBlockchainByEncrypted(keyOrJson: string): BlockchainService<BSCustomName> | undefined;
15
+ generateAccountFromMnemonicAllBlockchains(mnemonic: string, skippedAddresses?: string[]): Promise<Map<BSCustomName, AccountWithDerivationPath[]>>;
16
+ }
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BSAggregator = void 0;
13
+ const exceptions_1 = require("./exceptions");
14
+ class BSAggregator {
15
+ constructor(blockchainServices) {
16
+ this.blockchainServicesByName = blockchainServices;
17
+ this.blockchainServices = Object.values(blockchainServices);
18
+ }
19
+ addBlockchain(name, blockchain) {
20
+ if (this.blockchainServicesByName[name])
21
+ throw new exceptions_1.BlockchainAlreadyExistError(name);
22
+ this.blockchainServicesByName[name] = blockchain;
23
+ this.blockchainServices.push(blockchain);
24
+ }
25
+ validateAddressAllBlockchains(address) {
26
+ return this.blockchainServices.some(bs => bs.validateAddress(address));
27
+ }
28
+ validateTextAllBlockchains(text) {
29
+ return this.blockchainServices.some(bs => [bs.validateAddress(text), bs.validateEncrypted(text), bs.validateKey(text)].some(it => it === true));
30
+ }
31
+ validateKeyAllBlockchains(wif) {
32
+ return this.blockchainServices.some(bs => bs.validateKey(wif));
33
+ }
34
+ validateEncryptedAllBlockchains(keyOrJson) {
35
+ return this.blockchainServices.some(bs => bs.validateEncrypted(keyOrJson));
36
+ }
37
+ getBlockchainByName(name) {
38
+ const service = this.blockchainServicesByName[name];
39
+ if (!service)
40
+ throw new exceptions_1.BlockchainNotFoundError(name);
41
+ return this.blockchainServicesByName[name];
42
+ }
43
+ getBlockchainByAddress(address) {
44
+ return this.blockchainServices.find(bs => bs.validateAddress(address));
45
+ }
46
+ getBlockchainByKey(wif) {
47
+ return this.blockchainServices.find(bs => bs.validateKey(wif));
48
+ }
49
+ getBlockchainByEncrypted(keyOrJson) {
50
+ return this.blockchainServices.find(bs => bs.validateEncrypted(keyOrJson));
51
+ }
52
+ generateAccountFromMnemonicAllBlockchains(mnemonic, skippedAddresses) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const mnemonicAccounts = new Map();
55
+ const promises = this.blockchainServices.map((service) => __awaiter(this, void 0, void 0, function* () {
56
+ let index = 0;
57
+ const accounts = [];
58
+ while (true) {
59
+ const generatedAccount = service.generateAccountFromMnemonic(mnemonic, index);
60
+ if (skippedAddresses && skippedAddresses.find(address => address === generatedAccount.address)) {
61
+ index++;
62
+ continue;
63
+ }
64
+ if (index !== 0) {
65
+ try {
66
+ const { totalCount } = yield service.blockchainDataService.getTransactionsByAddress({
67
+ address: generatedAccount.address,
68
+ });
69
+ if (!totalCount || totalCount <= 0)
70
+ break;
71
+ }
72
+ catch (_a) {
73
+ break;
74
+ }
75
+ }
76
+ accounts.push(generatedAccount);
77
+ index++;
78
+ }
79
+ mnemonicAccounts.set(service.blockchainName, accounts);
80
+ }));
81
+ yield Promise.all(promises);
82
+ return mnemonicAccounts;
83
+ });
84
+ }
85
+ }
86
+ exports.BSAggregator = BSAggregator;
@@ -1,4 +1,9 @@
1
- export declare const exceptions: {
2
- invalidBlockchainService: (message?: string) => never;
3
- blockchainAlreadyExist: (blockchainName: string) => never;
4
- };
1
+ export declare class BlockchainAlreadyExistError extends Error {
2
+ constructor(blockchainName: string);
3
+ }
4
+ export declare class InvalidBlockchainServiceError extends Error {
5
+ constructor(message?: string);
6
+ }
7
+ export declare class BlockchainNotFoundError extends Error {
8
+ constructor(blockchainName: string);
9
+ }
@@ -1,11 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.exceptions = void 0;
4
- exports.exceptions = {
5
- invalidBlockchainService: (message) => {
6
- throw new Error(`Invalid blockchainServices => ${message}`);
7
- },
8
- blockchainAlreadyExist: (blockchainName) => {
9
- throw new Error(`The blockchain ${blockchainName} already exist`);
3
+ exports.BlockchainNotFoundError = exports.InvalidBlockchainServiceError = exports.BlockchainAlreadyExistError = void 0;
4
+ class BlockchainAlreadyExistError extends Error {
5
+ constructor(blockchainName) {
6
+ super(`The blockchain ${blockchainName} already exist`);
10
7
  }
11
- };
8
+ }
9
+ exports.BlockchainAlreadyExistError = BlockchainAlreadyExistError;
10
+ class InvalidBlockchainServiceError extends Error {
11
+ constructor(message) {
12
+ super(`Invalid blockchainServices => ${message}`);
13
+ }
14
+ }
15
+ exports.InvalidBlockchainServiceError = InvalidBlockchainServiceError;
16
+ class BlockchainNotFoundError extends Error {
17
+ constructor(blockchainName) {
18
+ super(`The blockchain ${blockchainName} not found`);
19
+ }
20
+ }
21
+ exports.BlockchainNotFoundError = BlockchainNotFoundError;
@@ -1,3 +1,5 @@
1
- import { BlockchainService, Claimable, NeoNameService } from "./interfaces";
2
- export declare function hasNNS(service: BlockchainService): service is NeoNameService & BlockchainService;
3
- export declare function isClaimable(service: BlockchainService): service is Claimable & BlockchainService;
1
+ import { BlockchainService, BSCalculableFee, BSClaimable, BSWithNameService, BSWithNft } from './interfaces';
2
+ export declare function hasNameService(service: BlockchainService): service is BlockchainService & BSWithNameService;
3
+ export declare function isClaimable(service: BlockchainService): service is BlockchainService & BSClaimable;
4
+ export declare function isCalculableFee(service: BlockchainService): service is BlockchainService & BSCalculableFee;
5
+ export declare function hasNft(service: BlockchainService): service is BlockchainService & BSWithNft;
package/dist/functions.js CHANGED
@@ -1,11 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isClaimable = exports.hasNNS = void 0;
4
- function hasNNS(service) {
5
- return "getNeoNsRecord" in service && "getOwnerOfNeoNsRecord" in service && "validateNNSDomain" in service;
3
+ exports.hasNft = exports.isCalculableFee = exports.isClaimable = exports.hasNameService = void 0;
4
+ function hasNameService(service) {
5
+ return 'resolveNameServiceDomain' in service && 'validateNameServiceDomainFormat' in service;
6
6
  }
7
- exports.hasNNS = hasNNS;
7
+ exports.hasNameService = hasNameService;
8
8
  function isClaimable(service) {
9
- return "claim" in this && "tokenClaim" in this && "getUnclaimed" in this.dataService;
9
+ return 'claim' in service && 'claimToken' in service && 'getUnclaimed' in service.blockchainDataService;
10
10
  }
11
11
  exports.isClaimable = isClaimable;
12
+ function isCalculableFee(service) {
13
+ return 'calculateTransferFee' in service;
14
+ }
15
+ exports.isCalculableFee = isCalculableFee;
16
+ function hasNft(service) {
17
+ return 'nftDataService' in service;
18
+ }
19
+ exports.hasNft = hasNft;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './interfaces';
2
- export * from './BSAgreggator';
3
- export * from './exchanges';
4
- export * from "./functions";
2
+ export * from './BSAggregator';
3
+ export * from './functions';
package/dist/index.js CHANGED
@@ -15,6 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./interfaces"), exports);
18
- __exportStar(require("./BSAgreggator"), exports);
19
- __exportStar(require("./exchanges"), exports);
18
+ __exportStar(require("./BSAggregator"), exports);
20
19
  __exportStar(require("./functions"), exports);
@@ -1,116 +1,119 @@
1
- export type SendTransactionParam = {
2
- senderAccount: Account;
3
- transactionIntents: IntentTransactionParam[];
4
- priorityFee?: number;
5
- };
6
- export type IntentTransactionParam = {
7
- receiverAddress: string;
8
- tokenHash: string;
9
- amount: number;
10
- };
11
- export type CalculateTransferFeeDetails = {
12
- systemFee?: string;
13
- networkFee?: string;
1
+ export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
2
+ export type Account = {
3
+ key: string;
4
+ type: 'wif' | 'privateKey';
5
+ address: string;
14
6
  };
15
- export type Currency = "USD" | "BRL" | "EUR";
16
- export type ExchangeInfo = {
17
- symbol: string;
18
- amount: number;
7
+ export type AccountWithDerivationPath = Account & {
8
+ derivationPath: string;
19
9
  };
20
- export type ClaimResponse = {
21
- txid: string;
10
+ export interface Token {
22
11
  symbol: string;
23
- hash: string;
24
- };
25
- export interface Claimable {
26
- claim(account: Account): Promise<ClaimResponse>;
27
- dataService: BlockchainDataService & BDSClaimable;
28
- tokenClaim: {
29
- hash: string;
30
- symbol: string;
31
- decimals: number;
32
- };
33
- }
34
- export declare enum NNSRecordTypes {
35
- IPV4 = "1",
36
- CANONICAL_NAME = "5",
37
- TEXT = "16",
38
- IPV6 = "28"
39
- }
40
- export interface NeoNameService {
41
- getNNSRecord(domainName: string, type: NNSRecordTypes): Promise<string>;
42
- getOwnerOfNNS(domainName: string): Promise<string>;
43
- validateNNSFormat(domainName: string): boolean;
44
- }
45
- export type Token = {
46
12
  name: string;
47
- symbol: string;
48
13
  hash: string;
49
14
  decimals: number;
15
+ }
16
+ export type NetworkType = 'mainnet' | 'testnet' | 'custom';
17
+ export type Network = {
18
+ type: NetworkType;
19
+ url: string;
50
20
  };
51
- export type Account = {
52
- wif: string;
53
- address: string;
21
+ export type IntentTransferParam = {
22
+ receiverAddress: string;
23
+ tokenHash: string;
24
+ amount: string;
25
+ tokenDecimals?: number;
26
+ };
27
+ export type TransferParam = {
28
+ senderAccount: Account;
29
+ intent: IntentTransferParam;
30
+ tipIntent?: IntentTransferParam;
31
+ priorityFee?: string;
54
32
  };
55
- export type CalculateTransferFeeResponse = {
56
- result: number;
57
- details?: CalculateTransferFeeDetails;
33
+ export type TokenPricesResponse = {
34
+ price: number;
35
+ symbol: string;
58
36
  };
37
+ export type Currency = 'USD' | 'BRL' | 'EUR';
38
+ export interface ExchangeDataService {
39
+ getTokenPrices(currency: Currency): Promise<TokenPricesResponse[]>;
40
+ }
59
41
  export interface BlockchainService<BSCustomName extends string = string> {
60
- readonly dataService: BlockchainDataService;
61
42
  readonly blockchainName: BSCustomName;
62
43
  readonly derivationPath: string;
63
- readonly feeToken: {
64
- hash: string;
65
- symbol: string;
66
- decimals: number;
67
- };
68
- readonly exchange: Exchange;
69
- readonly tokens: Token[];
70
- sendTransaction(param: SendTransactionParam): Promise<string>;
71
- generateMnemonic(): string;
72
- generateWif(mnemonic: string, index: number): string;
73
- generateAccount(mnemonic: string, index: number): Account;
74
- generateAccountFromWif(wif: string): string;
75
- decryptKey(encryptedKey: string, password: string): Promise<Account>;
44
+ readonly feeToken: Token;
45
+ exchangeDataService: ExchangeDataService;
46
+ blockchainDataService: BlockchainDataService;
47
+ tokens: Token[];
48
+ network: Network;
49
+ setNetwork: (network: PartialBy<Network, 'url'>) => void;
50
+ generateAccountFromMnemonic(mnemonic: string | string, index: number): AccountWithDerivationPath;
51
+ generateAccountFromKey(key: string): Account;
52
+ decrypt(keyOrJson: string, password: string): Promise<Account>;
76
53
  validateAddress(address: string): boolean;
77
- validateEncryptedKey(encryptedKey: string): boolean;
78
- validateWif(wif: string): boolean;
79
- calculateTransferFee(param: SendTransactionParam, details?: boolean): Promise<CalculateTransferFeeResponse>;
54
+ validateEncrypted(keyOrJson: string): boolean;
55
+ validateKey(key: string): boolean;
56
+ transfer(param: TransferParam): Promise<string>;
57
+ }
58
+ export interface BSCalculableFee {
59
+ calculateTransferFee(param: TransferParam, details?: boolean): Promise<string>;
60
+ }
61
+ export interface BSClaimable {
62
+ readonly claimToken: Token;
63
+ readonly burnToken: Token;
64
+ blockchainDataService: BlockchainDataService & BDSClaimable;
65
+ claim(account: Account): Promise<string>;
66
+ }
67
+ export interface BSWithNameService {
68
+ resolveNameServiceDomain(domainName: string): Promise<string>;
69
+ validateNameServiceDomainFormat(domainName: string): boolean;
70
+ }
71
+ export interface BSWithNft {
72
+ nftDataService: NftDataService;
80
73
  }
81
74
  export type TransactionNotifications = {
82
- contract: string;
83
- event_name: string;
75
+ eventName: string;
84
76
  state: {
85
77
  type: string;
86
78
  value: string;
87
79
  }[];
88
80
  };
89
- export type TransactionResponse = {
90
- txid: string;
91
- block: number;
92
- time: string;
93
- transfers: Omit<TransactionTransfer, 'txid'>[];
94
- sysfee: string;
95
- netfee: string;
96
- totfee: string;
97
- notifications: TransactionNotifications[];
98
- };
99
- export type TransactionTransfer = {
81
+ export type TransactionTransferAsset = {
100
82
  amount: string;
101
83
  to: string;
102
84
  from: string;
85
+ type: 'token';
86
+ contractHash: string;
87
+ token?: Token;
88
+ };
89
+ export type TransactionTransferNft = {
90
+ tokenId: string;
91
+ to: string;
92
+ from: string;
93
+ type: 'nft';
94
+ contractHash: string;
95
+ };
96
+ export type TransactionResponse = {
103
97
  hash: string;
104
- txid: string;
98
+ block: number;
99
+ time: number;
100
+ transfers: (TransactionTransferAsset | TransactionTransferNft)[];
101
+ fee?: string;
102
+ notifications: TransactionNotifications[];
105
103
  };
106
104
  export type ContractParameter = {
107
105
  name: string;
108
106
  type: string;
109
107
  };
110
- export type TransactionHistoryResponse = {
108
+ export type TransactionsByAddressResponse = {
111
109
  totalCount: number;
110
+ limit: number;
112
111
  transactions: TransactionResponse[];
113
112
  };
113
+ export type TransactionsByAddressParams = {
114
+ address: string;
115
+ page?: number;
116
+ };
114
117
  export type ContractMethod = {
115
118
  name: string;
116
119
  parameters: ContractParameter[];
@@ -120,44 +123,47 @@ export type ContractResponse = {
120
123
  name: string;
121
124
  methods: ContractMethod[];
122
125
  };
123
- export type ConsensusNodeResponse = {
124
- url: string;
125
- height: number;
126
- };
127
- export type TokenInfoResponse = {
128
- symbol: string;
129
- decimals: number;
130
- };
131
- export type BlockchainNetwork = "mainnet" | "testnet" | "privatenet";
132
126
  export type BalanceResponse = {
133
- amount: number;
134
- hash: string;
135
- symbol: string;
136
- name: string;
137
- };
138
- export type UnclaimedResponse = {
139
- address: string;
140
- unclaimed: number;
127
+ amount: string;
128
+ token: Token;
141
129
  };
142
- export interface BDSClaimable {
143
- getUnclaimed(address: string): Promise<UnclaimedResponse>;
144
- }
145
130
  export interface BlockchainDataService {
146
- readonly explorer: string;
147
- network: BlockchainNetwork;
148
- setNetwork(network: BlockchainNetwork): void;
149
131
  getTransaction(txid: string): Promise<TransactionResponse>;
150
- getHistoryTransactions(address: string, page: number): Promise<TransactionHistoryResponse>;
132
+ getTransactionsByAddress(params: TransactionsByAddressParams): Promise<TransactionsByAddressResponse>;
151
133
  getContract(contractHash: string): Promise<ContractResponse>;
152
- getTokenInfo(tokenHash: string): Promise<TokenInfoResponse>;
134
+ getTokenInfo(tokenHash: string): Promise<Token>;
153
135
  getBalance(address: string): Promise<BalanceResponse[]>;
154
- getAllNodes(): Promise<ConsensusNodeResponse[]>;
155
- getHigherNode(): Promise<ConsensusNodeResponse>;
136
+ getBlockHeight(): Promise<number>;
156
137
  }
157
- export type ToeknPricesResponse = {
158
- amount: number;
159
- Symbol: string;
138
+ export interface BDSClaimable {
139
+ getUnclaimed(address: string): Promise<string>;
140
+ }
141
+ export interface NftResponse {
142
+ id: string;
143
+ contractHash: string;
144
+ collectionName?: string;
145
+ collectionImage?: string;
146
+ symbol: string;
147
+ image?: string;
148
+ name?: string;
149
+ isSVG?: boolean;
150
+ }
151
+ export interface NftsResponse {
152
+ items: NftResponse[];
153
+ nextCursor?: string;
154
+ total?: number;
155
+ }
156
+ export type GetNftsByAddressParams = {
157
+ address: string;
158
+ page?: number;
159
+ cursor?: string;
160
+ size?: number;
161
+ };
162
+ export type GetNftParam = {
163
+ tokenId: string;
164
+ contractHash: string;
160
165
  };
161
- export interface Exchange {
162
- getTokenPrices(currency: Currency): Promise<ToeknPricesResponse[]>;
166
+ export interface NftDataService {
167
+ getNftsByAddress(params: GetNftsByAddressParams): Promise<NftsResponse>;
168
+ getNft(params: GetNftParam): Promise<NftResponse>;
163
169
  }
@@ -1,11 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NNSRecordTypes = void 0;
4
- var NNSRecordTypes;
5
- (function (NNSRecordTypes) {
6
- NNSRecordTypes["IPV4"] = "1";
7
- NNSRecordTypes["CANONICAL_NAME"] = "5";
8
- NNSRecordTypes["TEXT"] = "16";
9
- NNSRecordTypes["IPV6"] = "28";
10
- })(NNSRecordTypes = exports.NNSRecordTypes || (exports.NNSRecordTypes = {}));
11
- //*****************************************************************************
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cityofzion/blockchain-service",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": "https://github.com/CityOfZion/blockchain-services",