@cityofzion/bs-neo-legacy 1.13.3 → 1.13.5

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,62 +1,46 @@
1
- import { Account, BDSClaimable, BlockchainDataService, BlockchainService, BSClaimable, ExchangeDataService, Token, Network, TransferParam, BSWithExplorerService, ExplorerService, BSWithLedger, GetLedgerTransport, BalanceResponse, BSWithEncryption, ITokenService } from '@cityofzion/blockchain-service';
2
- import { BSNeoLegacyNetworkId } from './constants/BSNeoLegacyConstants';
1
+ import { TBSAccount, TBSToken, TTransferParam, TGetLedgerTransport, ITokenService, TBSNetwork, IClaimDataService, IBlockchainDataService, IExchangeDataService, IExplorerService, TPingNetworkResponse } from '@cityofzion/blockchain-service';
3
2
  import { NeonJsLedgerServiceNeoLegacy } from './services/ledger/NeonJsLedgerServiceNeoLegacy';
4
- export type MigrateParams<BSName extends string = string> = {
5
- account: Account<BSName>;
6
- neo3Address: string;
7
- neoLegacyMigrationAmounts: CalculateNeoLegacyMigrationAmountsResponse;
8
- };
9
- export type CalculateNeo3MigrationAmountsResponse = {
10
- gasMigrationTotalFees?: string;
11
- neoMigrationTotalFees?: string;
12
- gasMigrationReceiveAmount?: string;
13
- neoMigrationReceiveAmount?: string;
14
- };
15
- export type CalculateNeoLegacyMigrationAmountsResponse = {
16
- hasEnoughGasBalance: boolean;
17
- hasEnoughNeoBalance: boolean;
18
- gasBalance?: BalanceResponse;
19
- neoBalance?: BalanceResponse;
20
- };
21
- export declare class BSNeoLegacy<BSName extends string = string> implements BlockchainService<BSName, BSNeoLegacyNetworkId>, BSClaimable<BSName>, BSWithExplorerService, BSWithLedger<BSName>, BSWithEncryption<BSName> {
3
+ import { IBSNeoLegacy, TBSNeoLegacyNetworkId, TSigningCallback } from './types';
4
+ import { Neo3NeoLegacyMigrationService } from './services/migration/Neo3NeoLegacyMigrationService';
5
+ export declare class BSNeoLegacy<N extends string = string> implements IBSNeoLegacy<N> {
22
6
  #private;
23
- NATIVE_ASSETS: {
24
- symbol: string;
25
- name: string;
26
- hash: string;
27
- decimals: number;
28
- }[];
29
- readonly name: BSName;
7
+ readonly name: N;
30
8
  readonly bip44DerivationPath: string;
31
- nativeTokens: Token[];
32
- feeToken: Token;
33
- claimToken: Token;
34
- burnToken: Token;
35
- blockchainDataService: BlockchainDataService & BDSClaimable;
36
- exchangeDataService: ExchangeDataService;
37
- ledgerService: NeonJsLedgerServiceNeoLegacy<BSName>;
38
- explorerService: ExplorerService;
39
- tokenService: ITokenService;
40
- tokens: Token[];
41
- network: Network<BSNeoLegacyNetworkId>;
9
+ readonly isMultiTransferSupported = true;
10
+ readonly isCustomNetworkSupported = false;
11
+ readonly tokens: TBSToken[];
12
+ readonly nativeTokens: TBSToken[];
13
+ readonly feeToken: TBSToken;
14
+ readonly claimToken: TBSToken;
15
+ readonly burnToken: TBSToken;
16
+ network: TBSNetwork<TBSNeoLegacyNetworkId>;
17
+ availableNetworkURLs: string[];
42
18
  legacyNetwork: string;
43
- constructor(name: BSName, network?: Network<BSNeoLegacyNetworkId>, getLedgerTransport?: GetLedgerTransport<BSName>);
44
- testNetwork(network: Network<BSNeoLegacyNetworkId>): Promise<void>;
45
- setNetwork(network: Network<BSNeoLegacyNetworkId>): void;
19
+ readonly defaultNetwork: TBSNetwork<TBSNeoLegacyNetworkId>;
20
+ readonly availableNetworks: TBSNetwork<TBSNeoLegacyNetworkId>[];
21
+ blockchainDataService: IBlockchainDataService;
22
+ exchangeDataService: IExchangeDataService;
23
+ ledgerService: NeonJsLedgerServiceNeoLegacy<N>;
24
+ explorerService: IExplorerService;
25
+ tokenService: ITokenService;
26
+ claimDataService: IClaimDataService;
27
+ neo3NeoLegacyMigrationService: Neo3NeoLegacyMigrationService<N>;
28
+ constructor(name: N, network?: TBSNetwork<TBSNeoLegacyNetworkId>, getLedgerTransport?: TGetLedgerTransport<N>);
29
+ sendTransfer(config: any, nep5ScriptBuilder?: any): Promise<any>;
30
+ generateSigningCallback(account: TBSAccount<N>): Promise<{
31
+ neonJsAccount: any;
32
+ signingCallback: TSigningCallback;
33
+ }>;
34
+ setNetwork(network: TBSNetwork<TBSNeoLegacyNetworkId>): void;
35
+ pingNetwork(network: TBSNetwork<TBSNeoLegacyNetworkId>): Promise<TPingNetworkResponse>;
46
36
  validateAddress(address: string): boolean;
47
37
  validateEncrypted(key: string): boolean;
48
38
  validateKey(key: string): boolean;
49
- generateAccountFromMnemonic(mnemonic: string[] | string, index: number): Account<BSName>;
50
- generateAccountFromKey(key: string): Account<BSName>;
51
- generateAccountFromPublicKey(publicKey: string): Account<BSName>;
52
- decrypt(encryptedKey: string, password: string): Promise<Account<BSName>>;
39
+ generateAccountFromMnemonic(mnemonic: string[] | string, index: number): TBSAccount<N>;
40
+ generateAccountFromKey(key: string): TBSAccount<N>;
41
+ generateAccountFromPublicKey(publicKey: string): TBSAccount<N>;
42
+ decrypt(encryptedKey: string, password: string): Promise<TBSAccount<N>>;
53
43
  encrypt(key: string, password: string): Promise<string>;
54
- transfer({ intents, senderAccount, tipIntent, ...params }: TransferParam<BSName>): Promise<string[]>;
55
- claim(account: Account<BSName>): Promise<string>;
56
- migrate({ account, neo3Address, neoLegacyMigrationAmounts }: MigrateParams<BSName>): Promise<string>;
57
- /**
58
- * Reference: https://github.com/CityOfZion/legacy-n3-swap-service/blob/master/policy/policy.go
59
- */
60
- calculateNeo3MigrationAmounts(neoLegacyMigrationAmounts: CalculateNeoLegacyMigrationAmountsResponse): CalculateNeo3MigrationAmountsResponse;
61
- calculateNeoLegacyMigrationAmounts(balance: BalanceResponse[]): CalculateNeoLegacyMigrationAmountsResponse;
44
+ transfer({ intents, senderAccount, tipIntent, ...params }: TTransferParam<N>): Promise<string[]>;
45
+ claim(account: TBSAccount<N>): Promise<string>;
62
46
  }
@@ -24,74 +24,145 @@ var __rest = (this && this.__rest) || function (s, e) {
24
24
  }
25
25
  return t;
26
26
  };
27
- var _BSNeoLegacy_instances, _BSNeoLegacy_generateSigningCallback, _BSNeoLegacy_setTokens, _BSNeoLegacy_hasTransactionMoreThanMaxSize, _BSNeoLegacy_getRequiredTransactionFeeConfig, _BSNeoLegacy_signTransfer, _BSNeoLegacy_sendTransfer, _BSNeoLegacy_signClaim, _BSNeoLegacy_sendClaim;
27
+ var __importDefault = (this && this.__importDefault) || function (mod) {
28
+ return (mod && mod.__esModule) ? mod : { "default": mod };
29
+ };
30
+ var _BSNeoLegacy_instances, _BSNeoLegacy_hasTransactionMoreThanMaxSize, _BSNeoLegacy_getRequiredTransactionFeeConfig, _BSNeoLegacy_signClaim, _BSNeoLegacy_sendClaim, _BSNeoLegacy_signTransfer;
28
31
  Object.defineProperty(exports, "__esModule", { value: true });
29
32
  exports.BSNeoLegacy = void 0;
30
33
  const blockchain_service_1 = require("@cityofzion/blockchain-service");
31
- const neon_js_1 = require("@cityofzion/neon-js");
32
- const bs_asteroid_sdk_1 = require("@cityofzion/bs-asteroid-sdk");
33
34
  const BSNeoLegacyConstants_1 = require("./constants/BSNeoLegacyConstants");
34
- const BSNeoLegacyHelper_1 = require("./helpers/BSNeoLegacyHelper");
35
35
  const CryptoCompareEDSNeoLegacy_1 = require("./services/exchange-data/CryptoCompareEDSNeoLegacy");
36
36
  const DoraBDSNeoLegacy_1 = require("./services/blockchain-data/DoraBDSNeoLegacy");
37
37
  const NeoTubeESNeoLegacy_1 = require("./services/explorer/NeoTubeESNeoLegacy");
38
38
  const NeonJsLedgerServiceNeoLegacy_1 = require("./services/ledger/NeonJsLedgerServiceNeoLegacy");
39
39
  const TokenServiceNeoLegacy_1 = require("./services/token/TokenServiceNeoLegacy");
40
+ const DoraCDSNeoLegacy_1 = require("./services/claim-data/DoraCDSNeoLegacy");
41
+ const Neo3NeoLegacyMigrationService_1 = require("./services/migration/Neo3NeoLegacyMigrationService");
42
+ const BSNeoLegacyNeonJsSingletonHelper_1 = require("./helpers/BSNeoLegacyNeonJsSingletonHelper");
43
+ const axios_1 = __importDefault(require("axios"));
40
44
  class BSNeoLegacy {
41
45
  constructor(name, network, getLedgerTransport) {
42
46
  _BSNeoLegacy_instances.add(this);
43
- this.NATIVE_ASSETS = BSNeoLegacyConstants_1.BSNeoLegacyConstants.NATIVE_ASSETS;
44
- network = network !== null && network !== void 0 ? network : BSNeoLegacyConstants_1.BSNeoLegacyConstants.DEFAULT_NETWORK;
47
+ this.isMultiTransferSupported = true;
48
+ this.isCustomNetworkSupported = false;
45
49
  this.name = name;
46
- this.legacyNetwork = BSNeoLegacyConstants_1.BSNeoLegacyConstants.LEGACY_NETWORK_BY_NETWORK_ID[network.id];
47
50
  this.ledgerService = new NeonJsLedgerServiceNeoLegacy_1.NeonJsLedgerServiceNeoLegacy(this, getLedgerTransport);
48
51
  this.bip44DerivationPath = BSNeoLegacyConstants_1.BSNeoLegacyConstants.DEFAULT_BIP44_DERIVATION_PATH;
49
- this.setNetwork(network);
52
+ this.nativeTokens = BSNeoLegacyConstants_1.BSNeoLegacyConstants.NATIVE_ASSETS;
53
+ this.feeToken = BSNeoLegacyConstants_1.BSNeoLegacyConstants.GAS_ASSET;
54
+ this.burnToken = BSNeoLegacyConstants_1.BSNeoLegacyConstants.NEO_ASSET;
55
+ this.claimToken = BSNeoLegacyConstants_1.BSNeoLegacyConstants.GAS_ASSET;
56
+ this.tokens = this.nativeTokens;
57
+ this.availableNetworks = BSNeoLegacyConstants_1.BSNeoLegacyConstants.ALL_NETWORKS;
58
+ this.defaultNetwork = BSNeoLegacyConstants_1.BSNeoLegacyConstants.MAINNET_NETWORK;
59
+ this.setNetwork(network !== null && network !== void 0 ? network : this.defaultNetwork);
50
60
  }
51
- testNetwork(network) {
61
+ sendTransfer(config, nep5ScriptBuilder) {
52
62
  return __awaiter(this, void 0, void 0, function* () {
53
- const blockchainDataServiceClone = new DoraBDSNeoLegacy_1.DoraBDSNeoLegacy(network, this.feeToken, this.claimToken, this.tokens, this.explorerService, this.tokenService);
54
- yield blockchainDataServiceClone.getBlockHeight();
63
+ const { api } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
64
+ const sharedConfig = yield api.fillBalance(config);
65
+ let signedConfig = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_signTransfer).call(this, Object.assign({}, sharedConfig), nep5ScriptBuilder);
66
+ if (__classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_hasTransactionMoreThanMaxSize).call(this, signedConfig)) {
67
+ signedConfig = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_signTransfer).call(this, __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_getRequiredTransactionFeeConfig).call(this, sharedConfig), nep5ScriptBuilder);
68
+ }
69
+ signedConfig = yield api.sendTx(signedConfig);
70
+ return signedConfig.response.txid;
71
+ });
72
+ }
73
+ generateSigningCallback(account) {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ const { wallet, api } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
76
+ const neonJsAccount = new wallet.Account(account.key);
77
+ if (account.isHardware) {
78
+ if (!this.ledgerService.getLedgerTransport)
79
+ throw new Error('You must provide a getLedgerTransport function to use Ledger');
80
+ if (typeof account.bip44Path !== 'string')
81
+ throw new Error('Your account must have bip44 path to use Ledger');
82
+ const ledgerTransport = yield this.ledgerService.getLedgerTransport(account);
83
+ return {
84
+ neonJsAccount,
85
+ signingCallback: this.ledgerService.getSigningCallback(ledgerTransport, account),
86
+ };
87
+ }
88
+ return {
89
+ neonJsAccount,
90
+ signingCallback: api.signWithPrivateKey(neonJsAccount.privateKey),
91
+ };
55
92
  });
56
93
  }
57
94
  setNetwork(network) {
58
- if (!BSNeoLegacyConstants_1.BSNeoLegacyConstants.ALL_NETWORK_IDS.includes(network.id))
59
- throw new Error('Custom network is not supported');
60
- __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_setTokens).call(this, network);
95
+ const availableURLs = BSNeoLegacyConstants_1.BSNeoLegacyConstants.RPC_LIST_BY_NETWORK_ID[network.id] || [];
96
+ const isValidNetwork = blockchain_service_1.BSUtilsHelper.validateNetwork(network, this.availableNetworks, availableURLs);
97
+ if (!isValidNetwork) {
98
+ throw new Error(`Network with id ${network.id} is not available for ${this.name}`);
99
+ }
61
100
  this.network = network;
101
+ this.legacyNetwork = BSNeoLegacyConstants_1.BSNeoLegacyConstants.LEGACY_NETWORK_BY_NETWORK_ID[network.id];
102
+ this.availableNetworkURLs = availableURLs;
62
103
  this.tokenService = new TokenServiceNeoLegacy_1.TokenServiceNeoLegacy();
63
- this.explorerService = new NeoTubeESNeoLegacy_1.NeoTubeESNeoLegacy(network, this.tokenService);
64
- this.blockchainDataService = new DoraBDSNeoLegacy_1.DoraBDSNeoLegacy(network, this.feeToken, this.claimToken, this.tokens, this.explorerService, this.tokenService);
65
- this.exchangeDataService = new CryptoCompareEDSNeoLegacy_1.CryptoCompareEDSNeoLegacy(network);
104
+ this.explorerService = new NeoTubeESNeoLegacy_1.NeoTubeESNeoLegacy(this);
105
+ this.blockchainDataService = new DoraBDSNeoLegacy_1.DoraBDSNeoLegacy(this);
106
+ this.exchangeDataService = new CryptoCompareEDSNeoLegacy_1.CryptoCompareEDSNeoLegacy(this);
107
+ this.claimDataService = new DoraCDSNeoLegacy_1.DoraCDSNeoLegacy(this);
108
+ this.neo3NeoLegacyMigrationService = new Neo3NeoLegacyMigrationService_1.Neo3NeoLegacyMigrationService(this);
109
+ }
110
+ // This method is done manually because we need to ensure that the request is aborted after timeout
111
+ pingNetwork(network) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const abortController = new AbortController();
114
+ const timeout = setTimeout(() => {
115
+ abortController.abort();
116
+ }, 5000);
117
+ const timeStart = Date.now();
118
+ const response = yield axios_1.default.post(network.url, {
119
+ jsonrpc: '2.0',
120
+ method: 'getblockcount',
121
+ params: [],
122
+ id: 1234,
123
+ }, { timeout: 5000, signal: abortController.signal });
124
+ clearTimeout(timeout);
125
+ const latency = Date.now() - timeStart;
126
+ return {
127
+ latency,
128
+ url: network.url,
129
+ height: response.data.result,
130
+ };
131
+ });
66
132
  }
67
133
  validateAddress(address) {
68
- return neon_js_1.wallet.isAddress(address);
134
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
135
+ return wallet.isAddress(address);
69
136
  }
70
137
  validateEncrypted(key) {
71
- return neon_js_1.wallet.isNEP2(key);
138
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
139
+ return wallet.isNEP2(key);
72
140
  }
73
141
  validateKey(key) {
74
- return neon_js_1.wallet.isWIF(key) || neon_js_1.wallet.isPrivateKey(key);
142
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
143
+ return wallet.isWIF(key) || wallet.isPrivateKey(key);
75
144
  }
76
145
  generateAccountFromMnemonic(mnemonic, index) {
77
- bs_asteroid_sdk_1.keychain.importMnemonic(Array.isArray(mnemonic) ? mnemonic.join(' ') : mnemonic);
146
+ const mnemonicStr = Array.isArray(mnemonic) ? mnemonic.join(' ') : mnemonic;
78
147
  const bip44Path = this.bip44DerivationPath.replace('?', index.toString());
79
- const childKey = bs_asteroid_sdk_1.keychain.generateChildKey('neo', bip44Path);
80
- const key = childKey.getWIF();
81
- const { address } = new neon_js_1.wallet.Account(key);
82
- return { address, key, type: 'wif', bip44Path, blockchain: this.name };
148
+ const key = blockchain_service_1.BSKeychainHelper.generateNeoPrivateKeyFromMnemonic(mnemonicStr, bip44Path);
149
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
150
+ const { address, WIF } = new wallet.Account(key);
151
+ return { address, key: WIF, type: 'wif', bip44Path, blockchain: this.name };
83
152
  }
84
153
  generateAccountFromKey(key) {
85
- const type = neon_js_1.wallet.isWIF(key) ? 'wif' : neon_js_1.wallet.isPrivateKey(key) ? 'privateKey' : undefined;
154
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
155
+ const type = wallet.isWIF(key) ? 'wif' : wallet.isPrivateKey(key) ? 'privateKey' : undefined;
86
156
  if (!type)
87
157
  throw new Error('Invalid key');
88
- const { address } = new neon_js_1.wallet.Account(key);
158
+ const { address } = new wallet.Account(key);
89
159
  return { address, key, type, blockchain: this.name };
90
160
  }
91
161
  generateAccountFromPublicKey(publicKey) {
92
- if (!neon_js_1.wallet.isPublicKey(publicKey))
162
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
163
+ if (!wallet.isPublicKey(publicKey))
93
164
  throw new Error('Invalid public key');
94
- const account = new neon_js_1.wallet.Account(publicKey);
165
+ const account = new wallet.Account(publicKey);
95
166
  return {
96
167
  address: account.address,
97
168
  key: account.publicKey,
@@ -101,40 +172,43 @@ class BSNeoLegacy {
101
172
  }
102
173
  decrypt(encryptedKey, password) {
103
174
  return __awaiter(this, void 0, void 0, function* () {
104
- const key = yield neon_js_1.wallet.decrypt(encryptedKey, password);
175
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
176
+ const key = yield wallet.decrypt(encryptedKey, password);
105
177
  return this.generateAccountFromKey(key);
106
178
  });
107
179
  }
108
180
  encrypt(key, password) {
109
- return neon_js_1.wallet.encrypt(key, password);
181
+ const { wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
182
+ return wallet.encrypt(key, password);
110
183
  }
111
184
  transfer(_a) {
112
- var _b, _c;
113
- var { intents, senderAccount, tipIntent } = _a, params = __rest(_a, ["intents", "senderAccount", "tipIntent"]);
114
185
  return __awaiter(this, void 0, void 0, function* () {
115
- const { neonJsAccount, signingCallback } = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_generateSigningCallback).call(this, senderAccount);
116
- const apiProvider = new neon_js_1.api.neoCli.instance(this.network.url);
186
+ var _b, _c;
187
+ var { intents, senderAccount, tipIntent } = _a, params = __rest(_a, ["intents", "senderAccount", "tipIntent"]);
188
+ const { neonJsAccount, signingCallback } = yield this.generateSigningCallback(senderAccount);
189
+ const { api, sc, u, wallet } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
190
+ const apiProvider = new api.neoCli.instance(this.network.url);
117
191
  const priorityFee = Number((_b = params.priorityFee) !== null && _b !== void 0 ? _b : 0);
118
192
  const nativeIntents = [];
119
- const nep5ScriptBuilder = new neon_js_1.sc.ScriptBuilder();
193
+ const nep5ScriptBuilder = new sc.ScriptBuilder();
120
194
  const concatIntents = [...intents, ...(tipIntent ? [tipIntent] : [])];
121
195
  for (const intent of concatIntents) {
122
196
  const normalizeTokenHash = this.tokenService.normalizeHash(intent.tokenHash);
123
- const nativeAsset = this.NATIVE_ASSETS.find(token => this.tokenService.predicateByHash(normalizeTokenHash, token));
197
+ const nativeAsset = this.nativeTokens.find(token => this.tokenService.predicateByHash(normalizeTokenHash, token));
124
198
  if (nativeAsset) {
125
- nativeIntents.push(...neon_js_1.api.makeIntent({ [nativeAsset.symbol]: Number(intent.amount) }, intent.receiverAddress));
199
+ nativeIntents.push(...api.makeIntent({ [nativeAsset.symbol]: Number(intent.amount) }, intent.receiverAddress));
126
200
  continue;
127
201
  }
128
202
  nep5ScriptBuilder.emitAppCall(normalizeTokenHash, 'transfer', [
129
- neon_js_1.u.reverseHex(neon_js_1.wallet.getScriptHashFromAddress(neonJsAccount.address)),
130
- neon_js_1.u.reverseHex(neon_js_1.wallet.getScriptHashFromAddress(intent.receiverAddress)),
131
- neon_js_1.sc.ContractParam.integer(new neon_js_1.u.Fixed8(intent.amount)
203
+ u.reverseHex(wallet.getScriptHashFromAddress(neonJsAccount.address)),
204
+ u.reverseHex(wallet.getScriptHashFromAddress(intent.receiverAddress)),
205
+ sc.ContractParam.integer(new u.Fixed8(intent.amount)
132
206
  .div(Math.pow(10, 8 - ((_c = intent.tokenDecimals) !== null && _c !== void 0 ? _c : 8)))
133
207
  .toRawNumber()
134
208
  .toString()),
135
209
  ]);
136
210
  }
137
- const hash = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_sendTransfer).call(this, {
211
+ const hash = yield this.sendTransfer({
138
212
  account: neonJsAccount,
139
213
  api: apiProvider,
140
214
  url: this.network.url,
@@ -147,8 +221,9 @@ class BSNeoLegacy {
147
221
  }
148
222
  claim(account) {
149
223
  return __awaiter(this, void 0, void 0, function* () {
150
- const { neonJsAccount, signingCallback } = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_generateSigningCallback).call(this, account);
151
- const apiProvider = new neon_js_1.api.neoCli.instance(this.legacyNetwork);
224
+ const { neonJsAccount, signingCallback } = yield this.generateSigningCallback(account);
225
+ const { api } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
226
+ const apiProvider = new api.neoCli.instance(this.legacyNetwork);
152
227
  return yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_sendClaim).call(this, {
153
228
  api: apiProvider,
154
229
  account: neonJsAccount,
@@ -157,128 +232,9 @@ class BSNeoLegacy {
157
232
  });
158
233
  });
159
234
  }
160
- migrate({ account, neo3Address, neoLegacyMigrationAmounts }) {
161
- return __awaiter(this, void 0, void 0, function* () {
162
- if (!BSNeoLegacyHelper_1.BSNeoLegacyHelper.isMainnet(this.network)) {
163
- throw new Error('Must use Mainnet network');
164
- }
165
- if ((!neoLegacyMigrationAmounts.hasEnoughGasBalance && !neoLegacyMigrationAmounts.hasEnoughNeoBalance) ||
166
- (!neoLegacyMigrationAmounts.gasBalance && !neoLegacyMigrationAmounts.neoBalance)) {
167
- throw new Error('Must have at least 0.1 GAS or 2 NEO');
168
- }
169
- const { neonJsAccount, signingCallback } = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_generateSigningCallback).call(this, account);
170
- const provider = new neon_js_1.api.neoCli.instance(this.network.url);
171
- const intents = [];
172
- if (neoLegacyMigrationAmounts.hasEnoughGasBalance && neoLegacyMigrationAmounts.gasBalance)
173
- intents.push(...neon_js_1.api.makeIntent({ [BSNeoLegacyConstants_1.BSNeoLegacyConstants.GAS_ASSET.symbol]: Number(neoLegacyMigrationAmounts.gasBalance.amount) }, BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_COZ_LEGACY_ADDRESS));
174
- if (neoLegacyMigrationAmounts.hasEnoughNeoBalance && neoLegacyMigrationAmounts.neoBalance)
175
- intents.push(...neon_js_1.api.makeIntent({ [BSNeoLegacyConstants_1.BSNeoLegacyConstants.NEO_ASSET.symbol]: Number(neoLegacyMigrationAmounts.neoBalance.amount) }, BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_COZ_LEGACY_ADDRESS));
176
- return yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_sendTransfer).call(this, {
177
- url: this.network.url,
178
- api: provider,
179
- account: neonJsAccount,
180
- intents,
181
- signingFunction: signingCallback,
182
- override: {
183
- attributes: [
184
- new neon_js_1.tx.TransactionAttribute({
185
- usage: neon_js_1.tx.TxAttrUsage.Remark14,
186
- data: neon_js_1.u.str2hexstring(neo3Address),
187
- }),
188
- new neon_js_1.tx.TransactionAttribute({
189
- usage: neon_js_1.tx.TxAttrUsage.Remark15,
190
- data: neon_js_1.u.str2hexstring('Neon Desktop Migration'),
191
- }),
192
- ],
193
- },
194
- });
195
- });
196
- }
197
- /**
198
- * Reference: https://github.com/CityOfZion/legacy-n3-swap-service/blob/master/policy/policy.go
199
- */
200
- calculateNeo3MigrationAmounts(neoLegacyMigrationAmounts) {
201
- const response = {
202
- gasMigrationReceiveAmount: undefined,
203
- gasMigrationTotalFees: undefined,
204
- neoMigrationReceiveAmount: undefined,
205
- neoMigrationTotalFees: undefined,
206
- };
207
- if (neoLegacyMigrationAmounts.gasBalance && neoLegacyMigrationAmounts.hasEnoughGasBalance) {
208
- // Two transfers fee and one transfer fee left over
209
- const allNep17TransfersFee = BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEP_17_TRANSFER_FEE * 3;
210
- const gasMigrationAmountNumber = Number(neoLegacyMigrationAmounts.gasBalance.amount);
211
- // Necessary to calculate the COZ fee
212
- const gasAmountNumberLessAllNep17TransfersFee = gasMigrationAmountNumber - allNep17TransfersFee;
213
- // Example: ~0.06635710 * 0.01 = ~0.00066357
214
- const cozFee = gasAmountNumberLessAllNep17TransfersFee * BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_COZ_FEE;
215
- // Example: ~0.06635710 - ~0.00066357 = ~0.06569352
216
- const gasAmountNumberLessCozFee = gasAmountNumberLessAllNep17TransfersFee - cozFee;
217
- const allGasFeeNumberThatUserWillPay = cozFee + BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEP_17_TRANSFER_FEE * 2;
218
- const allGasAmountNumberThatUserWillReceive = gasAmountNumberLessCozFee + BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_NEP_17_TRANSFER_FEE;
219
- response.gasMigrationTotalFees = blockchain_service_1.BSBigNumberHelper.format(allGasFeeNumberThatUserWillPay, {
220
- decimals: BSNeoLegacyConstants_1.BSNeoLegacyConstants.GAS_ASSET.decimals,
221
- });
222
- response.gasMigrationReceiveAmount = blockchain_service_1.BSBigNumberHelper.format(allGasAmountNumberThatUserWillReceive, {
223
- decimals: BSNeoLegacyConstants_1.BSNeoLegacyConstants.GAS_ASSET.decimals,
224
- });
225
- }
226
- if (neoLegacyMigrationAmounts.neoBalance && neoLegacyMigrationAmounts.hasEnoughNeoBalance) {
227
- const neoMigrationAmountNumber = Number(neoLegacyMigrationAmounts.neoBalance.amount);
228
- response.neoMigrationTotalFees = blockchain_service_1.BSBigNumberHelper.format(Math.ceil(neoMigrationAmountNumber * BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_COZ_FEE), { decimals: BSNeoLegacyConstants_1.BSNeoLegacyConstants.NEO_ASSET.decimals });
229
- response.neoMigrationReceiveAmount = blockchain_service_1.BSBigNumberHelper.format(neoMigrationAmountNumber - Number(response.neoMigrationTotalFees), { decimals: BSNeoLegacyConstants_1.BSNeoLegacyConstants.NEO_ASSET.decimals });
230
- }
231
- return response;
232
- }
233
- calculateNeoLegacyMigrationAmounts(balance) {
234
- const gasBalance = balance.find(({ token }) => this.tokenService.predicateByHash(BSNeoLegacyConstants_1.BSNeoLegacyConstants.GAS_ASSET, token));
235
- const neoBalance = balance.find(({ token }) => this.tokenService.predicateByHash(BSNeoLegacyConstants_1.BSNeoLegacyConstants.NEO_ASSET, token));
236
- let hasEnoughGasBalance = false;
237
- let hasEnoughNeoBalance = false;
238
- if (gasBalance) {
239
- const gasBalanceNumber = blockchain_service_1.BSBigNumberHelper.fromNumber(gasBalance.amount);
240
- hasEnoughGasBalance = gasBalanceNumber.isGreaterThanOrEqualTo(BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_MIN_GAS);
241
- }
242
- if (neoBalance) {
243
- const neoBalanceNumber = blockchain_service_1.BSBigNumberHelper.fromNumber(neoBalance.amount);
244
- hasEnoughNeoBalance = neoBalanceNumber.isGreaterThanOrEqualTo(BSNeoLegacyConstants_1.BSNeoLegacyConstants.MIGRATION_MIN_NEO);
245
- }
246
- return {
247
- gasBalance,
248
- neoBalance,
249
- hasEnoughGasBalance,
250
- hasEnoughNeoBalance,
251
- };
252
- }
253
235
  }
254
236
  exports.BSNeoLegacy = BSNeoLegacy;
255
- _BSNeoLegacy_instances = new WeakSet(), _BSNeoLegacy_generateSigningCallback = function _BSNeoLegacy_generateSigningCallback(account) {
256
- return __awaiter(this, void 0, void 0, function* () {
257
- const neonJsAccount = new neon_js_1.wallet.Account(account.key);
258
- if (account.isHardware) {
259
- if (!this.ledgerService.getLedgerTransport)
260
- throw new Error('You must provide a getLedgerTransport function to use Ledger');
261
- if (typeof account.bip44Path !== 'string')
262
- throw new Error('Your account must have bip44 path to use Ledger');
263
- const ledgerTransport = yield this.ledgerService.getLedgerTransport(account);
264
- return {
265
- neonJsAccount,
266
- signingCallback: this.ledgerService.getSigningCallback(ledgerTransport, account),
267
- };
268
- }
269
- return {
270
- neonJsAccount,
271
- signingCallback: neon_js_1.api.signWithPrivateKey(neonJsAccount.privateKey),
272
- };
273
- });
274
- }, _BSNeoLegacy_setTokens = function _BSNeoLegacy_setTokens(network) {
275
- const tokens = BSNeoLegacyHelper_1.BSNeoLegacyHelper.getTokens(network);
276
- this.nativeTokens = BSNeoLegacyConstants_1.BSNeoLegacyConstants.NATIVE_ASSETS;
277
- this.tokens = tokens;
278
- this.feeToken = tokens.find(token => token.symbol === 'GAS');
279
- this.burnToken = tokens.find(token => token.symbol === 'NEO');
280
- this.claimToken = tokens.find(token => token.symbol === 'GAS');
281
- }, _BSNeoLegacy_hasTransactionMoreThanMaxSize = function _BSNeoLegacy_hasTransactionMoreThanMaxSize(config) {
237
+ _BSNeoLegacy_instances = new WeakSet(), _BSNeoLegacy_hasTransactionMoreThanMaxSize = function _BSNeoLegacy_hasTransactionMoreThanMaxSize(config) {
282
238
  if (!config.fees || config.fees < BSNeoLegacyConstants_1.BSNeoLegacyConstants.FEE_APPLIED_TO_PLAYABLE_TRANSACTION) {
283
239
  const serializedTransaction = config.tx.serialize(true);
284
240
  const transactionSize = serializedTransaction.length / 2;
@@ -295,47 +251,40 @@ _BSNeoLegacy_instances = new WeakSet(), _BSNeoLegacy_generateSigningCallback = f
295
251
  gasIntent.value = gasIntent.value.sub(BSNeoLegacyConstants_1.BSNeoLegacyConstants.FEE_APPLIED_TO_PLAYABLE_TRANSACTION);
296
252
  }
297
253
  return config;
298
- }, _BSNeoLegacy_signTransfer = function _BSNeoLegacy_signTransfer(config, nep5ScriptBuilder) {
299
- return __awaiter(this, void 0, void 0, function* () {
300
- if (!nep5ScriptBuilder || nep5ScriptBuilder.isEmpty()) {
301
- config = yield neon_js_1.api.createContractTx(config);
302
- }
303
- else {
304
- config.script = nep5ScriptBuilder.str;
305
- config = yield neon_js_1.api.createInvocationTx(config);
306
- }
307
- config = yield neon_js_1.api.modifyTransactionForEmptyTransaction(config);
308
- config = yield neon_js_1.api.addAttributeIfExecutingAsSmartContract(config);
309
- config = yield neon_js_1.api.signTx(config);
310
- config = yield neon_js_1.api.addSignatureIfExecutingAsSmartContract(config);
311
- return config;
312
- });
313
- }, _BSNeoLegacy_sendTransfer = function _BSNeoLegacy_sendTransfer(config, nep5ScriptBuilder) {
314
- return __awaiter(this, void 0, void 0, function* () {
315
- const sharedConfig = yield neon_js_1.api.fillBalance(config);
316
- let signedConfig = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_signTransfer).call(this, Object.assign({}, sharedConfig), nep5ScriptBuilder);
317
- if (__classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_hasTransactionMoreThanMaxSize).call(this, signedConfig)) {
318
- signedConfig = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_signTransfer).call(this, __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_getRequiredTransactionFeeConfig).call(this, sharedConfig), nep5ScriptBuilder);
319
- }
320
- signedConfig = yield neon_js_1.api.sendTx(signedConfig);
321
- return signedConfig.response.txid;
322
- });
323
254
  }, _BSNeoLegacy_signClaim = function _BSNeoLegacy_signClaim(config) {
324
255
  return __awaiter(this, void 0, void 0, function* () {
325
- config = yield neon_js_1.api.createClaimTx(config);
326
- config = yield neon_js_1.api.addAttributeIfExecutingAsSmartContract(config);
327
- config = yield neon_js_1.api.signTx(config);
328
- config = yield neon_js_1.api.addSignatureIfExecutingAsSmartContract(config);
256
+ const { api } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
257
+ config = yield api.createClaimTx(config);
258
+ config = yield api.addAttributeIfExecutingAsSmartContract(config);
259
+ config = yield api.signTx(config);
260
+ config = yield api.addSignatureIfExecutingAsSmartContract(config);
329
261
  return config;
330
262
  });
331
263
  }, _BSNeoLegacy_sendClaim = function _BSNeoLegacy_sendClaim(config) {
332
264
  return __awaiter(this, void 0, void 0, function* () {
333
- const sharedConfig = yield neon_js_1.api.fillClaims(config);
265
+ const { api } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
266
+ const sharedConfig = yield api.fillClaims(config);
334
267
  let signedConfig = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_signClaim).call(this, Object.assign({}, sharedConfig));
335
268
  if (__classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_hasTransactionMoreThanMaxSize).call(this, signedConfig)) {
336
269
  signedConfig = yield __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_signClaim).call(this, __classPrivateFieldGet(this, _BSNeoLegacy_instances, "m", _BSNeoLegacy_getRequiredTransactionFeeConfig).call(this, sharedConfig));
337
270
  }
338
- signedConfig = yield neon_js_1.api.sendTx(signedConfig);
271
+ signedConfig = yield api.sendTx(signedConfig);
339
272
  return signedConfig.response.txid;
340
273
  });
274
+ }, _BSNeoLegacy_signTransfer = function _BSNeoLegacy_signTransfer(config, nep5ScriptBuilder) {
275
+ return __awaiter(this, void 0, void 0, function* () {
276
+ const { api } = BSNeoLegacyNeonJsSingletonHelper_1.BSNeoLegacyNeonJsSingletonHelper.getInstance();
277
+ if (!nep5ScriptBuilder || nep5ScriptBuilder.isEmpty()) {
278
+ config = yield api.createContractTx(config);
279
+ }
280
+ else {
281
+ config.script = nep5ScriptBuilder.str;
282
+ config = yield api.createInvocationTx(config);
283
+ }
284
+ config = yield api.modifyTransactionForEmptyTransaction(config);
285
+ config = yield api.addAttributeIfExecutingAsSmartContract(config);
286
+ config = yield api.signTx(config);
287
+ config = yield api.addSignatureIfExecutingAsSmartContract(config);
288
+ return config;
289
+ });
341
290
  };
@@ -1,42 +1,21 @@
1
- import { Network, NetworkId, Token } from '@cityofzion/blockchain-service';
2
- export type BSNeoLegacyNetworkId = NetworkId<'mainnet' | 'testnet'>;
1
+ import { TBSNetwork, TBSToken } from '@cityofzion/blockchain-service';
2
+ import { TBSNeoLegacyNetworkId } from '../types';
3
3
  export declare class BSNeoLegacyConstants {
4
- #private;
5
- static EXTRA_TOKENS_BY_NETWORK_ID: Partial<Record<BSNeoLegacyNetworkId, Token[]>>;
6
- static NATIVE_ASSETS: {
7
- symbol: string;
8
- name: string;
9
- hash: string;
10
- decimals: number;
11
- }[];
12
- static GAS_ASSET: {
13
- symbol: string;
14
- name: string;
15
- hash: string;
16
- decimals: number;
17
- };
18
- static NEO_ASSET: {
19
- symbol: string;
20
- name: string;
21
- hash: string;
22
- decimals: number;
23
- };
24
- static RPC_LIST_BY_NETWORK_ID: Record<BSNeoLegacyNetworkId, string[]>;
25
- static LEGACY_NETWORK_BY_NETWORK_ID: Record<BSNeoLegacyNetworkId, string>;
26
- static MAINNET_NETWORK_IDS: BSNeoLegacyNetworkId[];
27
- static TESTNET_NETWORK_IDS: BSNeoLegacyNetworkId[];
28
- static ALL_NETWORK_IDS: BSNeoLegacyNetworkId[];
29
- static MAINNET_NETWORKS: Network<BSNeoLegacyNetworkId>[];
30
- static TESTNET_NETWORKS: Network<BSNeoLegacyNetworkId>[];
31
- static ALL_NETWORKS: Network<BSNeoLegacyNetworkId>[];
32
- static DEFAULT_BIP44_DERIVATION_PATH: string;
33
- static DEFAULT_NETWORK: Network<BSNeoLegacyNetworkId>;
4
+ static readonly GAS_ASSET: TBSToken;
5
+ static readonly NEO_ASSET: TBSToken;
6
+ static readonly NATIVE_ASSETS: TBSToken[];
7
+ static readonly RPC_LIST_BY_NETWORK_ID: Record<TBSNeoLegacyNetworkId, string[]>;
8
+ static readonly LEGACY_NETWORK_BY_NETWORK_ID: Record<TBSNeoLegacyNetworkId, string>;
9
+ static readonly MAINNET_NETWORK: TBSNetwork<TBSNeoLegacyNetworkId>;
10
+ static readonly TESTNET_NETWORK: TBSNetwork<TBSNeoLegacyNetworkId>;
11
+ static readonly ALL_NETWORKS: TBSNetwork<TBSNeoLegacyNetworkId>[];
12
+ static readonly DEFAULT_BIP44_DERIVATION_PATH = "m/44'/888'/0'/0/?";
34
13
  static readonly MIGRATION_COZ_LEGACY_ADDRESS = "AaT27thuyPaqERPwERhk7QhfKrbj4xoyAV";
35
14
  static readonly MIGRATION_COZ_FEE = 0.01;
36
15
  static readonly MIGRATION_NEP_17_TRANSFER_FEE = 0.0112143;
37
16
  static readonly MIGRATION_MIN_GAS = 0.1;
38
17
  static readonly MIGRATION_MIN_NEO = 2;
39
18
  static readonly MIGRATION_COZ_NEO3_ADDRESS = "NLMsicDapULKFDmAzTsbhwrZjYZ83j53Ty";
40
- static MAX_TRANSACTION_SIZE_WITHOUT_FEE: number;
41
- static FEE_APPLIED_TO_PLAYABLE_TRANSACTION: number;
19
+ static readonly MAX_TRANSACTION_SIZE_WITHOUT_FEE = 1024;
20
+ static readonly FEE_APPLIED_TO_PLAYABLE_TRANSACTION = 0.05;
42
21
  }