@cityofzion/bs-neox 1.4.7 → 1.5.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.
package/dist/BSNeoX.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BSEthereum } from '@cityofzion/bs-ethereum';
2
- import { TGetLedgerTransport, INeo3NeoXBridgeService, TBSNetwork } from '@cityofzion/blockchain-service';
2
+ import { TGetLedgerTransport, INeo3NeoXBridgeService, TBSNetwork, TTransferParam } from '@cityofzion/blockchain-service';
3
3
  import { IBSNeoX, TBSNeoXNetworkId } from './types';
4
4
  export declare class BSNeoX<N extends string = string> extends BSEthereum<N, TBSNeoXNetworkId> implements IBSNeoX<N> {
5
5
  neo3NeoXBridgeService: INeo3NeoXBridgeService<N>;
@@ -7,4 +7,5 @@ export declare class BSNeoX<N extends string = string> extends BSEthereum<N, TBS
7
7
  readonly availableNetworks: TBSNetwork<TBSNeoXNetworkId>[];
8
8
  constructor(name: N, network?: TBSNetwork<TBSNeoXNetworkId>, getLedgerTransport?: TGetLedgerTransport<N>);
9
9
  setNetwork(network: TBSNetwork<TBSNeoXNetworkId>): void;
10
+ transfer(params: TTransferParam<N>): Promise<string[]>;
10
11
  }
package/dist/BSNeoX.js CHANGED
@@ -1,14 +1,41 @@
1
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
2
14
  Object.defineProperty(exports, "__esModule", { value: true });
3
15
  exports.BSNeoX = void 0;
4
16
  const bs_ethereum_1 = require("@cityofzion/bs-ethereum");
5
17
  const BSNeoXConstants_1 = require("./constants/BSNeoXConstants");
6
18
  const blockchain_service_1 = require("@cityofzion/blockchain-service");
7
19
  const BlockscoutBDSNeoX_1 = require("./services/blockchain-data/BlockscoutBDSNeoX");
20
+ const WalletConnectServiceNeoX_1 = require("./services/wallet-connect/WalletConnectServiceNeoX");
8
21
  const FlamingoForthewinEDSNeoX_1 = require("./services/exchange-data/FlamingoForthewinEDSNeoX");
9
22
  const BlockscoutESNeoX_1 = require("./services/explorer/BlockscoutESNeoX");
10
23
  const GhostMarketNDSNeoX_1 = require("./services/nft-data/GhostMarketNDSNeoX");
11
24
  const Neo3NeoXBridgeService_1 = require("./services/neo3neoXBridge/Neo3NeoXBridgeService");
25
+ const ethers_1 = require("ethers");
26
+ const axios_1 = __importDefault(require("axios"));
27
+ const consensus_1 = require("./assets/abis/consensus");
28
+ const key_management_1 = require("./assets/abis/key-management");
29
+ const neox_tpke_1 = require("neox-tpke");
30
+ const viem_1 = require("viem");
31
+ // Necessary to run on Node.js
32
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
+ // @ts-expect-error
34
+ if (typeof self === 'undefined') {
35
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
36
+ // @ts-expect-error
37
+ global.self = global;
38
+ }
12
39
  class BSNeoX extends bs_ethereum_1.BSEthereum {
13
40
  constructor(name, network, getLedgerTransport) {
14
41
  super(name, undefined, undefined, getLedgerTransport);
@@ -20,20 +47,122 @@ class BSNeoX extends bs_ethereum_1.BSEthereum {
20
47
  this.setNetwork(network !== null && network !== void 0 ? network : this.defaultNetwork);
21
48
  }
22
49
  setNetwork(network) {
23
- const availableURLs = BSNeoXConstants_1.BSNeoXConstants.RPC_LIST_BY_NETWORK_ID[network.id] || [];
24
- const isValidNetwork = blockchain_service_1.BSUtilsHelper.validateNetwork(network, this.availableNetworks, availableURLs);
50
+ const rpcNetworkUrls = BSNeoXConstants_1.BSNeoXConstants.RPC_LIST_BY_NETWORK_ID[network.id] || [];
51
+ const isValidNetwork = blockchain_service_1.BSUtilsHelper.validateNetwork(network, this.availableNetworks, rpcNetworkUrls);
25
52
  if (!isValidNetwork) {
26
53
  throw new Error(`Network with id ${network.id} is not available for ${this.name}`);
27
54
  }
28
55
  this.network = network;
29
- this.availableNetworkURLs = availableURLs;
56
+ this.rpcNetworkUrls = rpcNetworkUrls;
30
57
  this.nftDataService = new GhostMarketNDSNeoX_1.GhostMarketNDSNeoX(this);
31
58
  this.explorerService = new BlockscoutESNeoX_1.BlockscoutESNeoX(this);
32
59
  this.exchangeDataService = new FlamingoForthewinEDSNeoX_1.FlamingoForthewinEDSNeoX(this);
33
60
  this.neo3NeoXBridgeService = new Neo3NeoXBridgeService_1.Neo3NeoXBridgeService(this);
34
61
  this.blockchainDataService = new BlockscoutBDSNeoX_1.BlockscoutBDSNeoX(this);
35
62
  this.tokenService = new bs_ethereum_1.TokenServiceEthereum();
36
- this.walletConnectService = new bs_ethereum_1.WalletConnectServiceEthereum(this);
63
+ this.walletConnectService = new WalletConnectServiceNeoX_1.WalletConnectServiceNeoX(this);
64
+ }
65
+ transfer(params) {
66
+ const _super = Object.create(null, {
67
+ transfer: { get: () => super.transfer }
68
+ });
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ var _a;
71
+ const isAntiMevRpcNetworkUrl = BSNeoXConstants_1.BSNeoXConstants.ANTI_MEV_RPC_LIST_BY_NETWORK_ID[this.network.id].some(rpcNetworkUrl => rpcNetworkUrl === this.network.url);
72
+ if (!isAntiMevRpcNetworkUrl)
73
+ return yield _super.transfer.call(this, params);
74
+ const signer = yield this.generateSigner(params.senderAccount);
75
+ let nonce = yield signer.getTransactionCount();
76
+ if (isNaN(nonce)) {
77
+ throw new Error('Invalid nonce');
78
+ }
79
+ const chainId = +this.network.id;
80
+ if (isNaN(chainId)) {
81
+ throw new Error('Invalid chainId');
82
+ }
83
+ const transactionHashes = [];
84
+ let error;
85
+ for (const intent of params.intents) {
86
+ try {
87
+ const { transactionParams, gasPrice } = yield this._buildTransferParams(intent);
88
+ let gasLimit;
89
+ try {
90
+ gasLimit = yield signer.estimateGas(transactionParams);
91
+ }
92
+ catch (_b) {
93
+ gasLimit = bs_ethereum_1.BSEthereumConstants.DEFAULT_GAS_LIMIT;
94
+ }
95
+ const [_transaction, transactionError] = yield blockchain_service_1.BSPromisesHelper.tryCatch(() => signer.sendTransaction(Object.assign(Object.assign({}, transactionParams), { chainId,
96
+ nonce,
97
+ gasLimit, maxPriorityFeePerGas: gasPrice, maxFeePerGas: gasPrice })));
98
+ if (!(transactionError === null || transactionError === void 0 ? void 0 : transactionError.error) ||
99
+ transactionError.error.code !== -32000 ||
100
+ transactionError.error.message !== 'transaction cached') {
101
+ throw new Error('Transaction not cached');
102
+ }
103
+ const signature = yield signer.signMessage(nonce.toString());
104
+ const hexNonce = (0, viem_1.toHex)(nonce);
105
+ // Keep using Axios because of the signer and provider don't have the eth_getCachedTransaction method
106
+ const cachedTransactionResponse = yield axios_1.default.post(this.network.url, {
107
+ id: Date.now(),
108
+ jsonrpc: '2.0',
109
+ method: 'eth_getCachedTransaction',
110
+ params: [hexNonce, signature],
111
+ });
112
+ const cachedTransaction = cachedTransactionResponse.data.result;
113
+ const consensusContract = new ethers_1.ethers.Contract(BSNeoXConstants_1.BSNeoXConstants.CONSENSUS_SCRIPT_HASH, consensus_1.CONSENSUS_ABI, signer);
114
+ const consensusSize = BigInt(yield consensusContract.consensusSize());
115
+ const keyManagementContract = new ethers_1.ethers.Contract(BSNeoXConstants_1.BSNeoXConstants.KEY_MANAGEMENT_SCRIPT_HASH, key_management_1.KEY_MANAGEMENT_ABI, signer);
116
+ const roundNumber = BigInt(yield keyManagementContract.roundNumber());
117
+ const aggregatedCommitment = yield keyManagementContract.aggregatedCommitments(roundNumber);
118
+ const publicKey = neox_tpke_1.PublicKey.fromAggregatedCommitment((0, viem_1.toBytes)(aggregatedCommitment), (0, neox_tpke_1.getScaler)(consensusSize, (0, neox_tpke_1.getConsensusThreshold)(consensusSize)));
119
+ const { encryptedKey, encryptedMsg } = publicKey.encrypt((0, viem_1.toBytes)(cachedTransaction));
120
+ const parsedTransaction = (0, viem_1.parseTransaction)(cachedTransaction);
121
+ const envelopedData = (0, viem_1.concat)([
122
+ new Uint8Array([0xff, 0xff, 0xff, 0xff]),
123
+ (0, viem_1.pad)((0, viem_1.toBytes)(roundNumber), { size: 4 }),
124
+ (0, viem_1.pad)((0, viem_1.toBytes)(parsedTransaction.gas), { size: 4 }),
125
+ (0, viem_1.toBytes)((0, viem_1.keccak256)(cachedTransaction)),
126
+ encryptedKey,
127
+ encryptedMsg,
128
+ ]);
129
+ const newTransactionParams = {
130
+ chainId,
131
+ nonce,
132
+ to: BSNeoXConstants_1.BSNeoXConstants.GOVERNANCE_REWARD_SCRIPT_HASH,
133
+ data: (0, viem_1.toHex)(envelopedData),
134
+ };
135
+ try {
136
+ gasLimit = yield signer.estimateGas(newTransactionParams);
137
+ }
138
+ catch (_c) {
139
+ gasLimit = bs_ethereum_1.BSEthereumConstants.DEFAULT_GAS_LIMIT;
140
+ }
141
+ const signedTransaction = yield signer.signTransaction(Object.assign({ gasLimit,
142
+ gasPrice }, newTransactionParams));
143
+ // Keep using Axios because of the signer and provider don't have the eth_sendRawTransaction method
144
+ const transactionResponse = yield axios_1.default.post(this.network.url, {
145
+ id: Date.now(),
146
+ jsonrpc: '2.0',
147
+ method: 'eth_sendRawTransaction',
148
+ params: [signedTransaction],
149
+ });
150
+ const transactionHash = (_a = transactionResponse === null || transactionResponse === void 0 ? void 0 : transactionResponse.data) === null || _a === void 0 ? void 0 : _a.result;
151
+ if (transactionHash) {
152
+ transactionHashes.push(transactionHash);
153
+ nonce++;
154
+ }
155
+ }
156
+ catch (newError) {
157
+ console.error(newError);
158
+ if (!error)
159
+ error = newError;
160
+ }
161
+ }
162
+ if (error && !transactionHashes.some(hash => !!hash))
163
+ throw error;
164
+ return transactionHashes;
165
+ });
37
166
  }
38
167
  }
39
168
  exports.BSNeoX = BSNeoX;
@@ -0,0 +1,46 @@
1
+ export declare const CONSENSUS_ABI: ({
2
+ inputs: {
3
+ internalType: string;
4
+ name: string;
5
+ type: string;
6
+ }[];
7
+ name: string;
8
+ type: string;
9
+ anonymous?: undefined;
10
+ outputs?: undefined;
11
+ stateMutability?: undefined;
12
+ } | {
13
+ anonymous: boolean;
14
+ inputs: {
15
+ indexed: boolean;
16
+ internalType: string;
17
+ name: string;
18
+ type: string;
19
+ }[];
20
+ name: string;
21
+ type: string;
22
+ outputs?: undefined;
23
+ stateMutability?: undefined;
24
+ } | {
25
+ inputs: {
26
+ internalType: string;
27
+ name: string;
28
+ type: string;
29
+ }[];
30
+ name: string;
31
+ outputs: {
32
+ internalType: string;
33
+ name: string;
34
+ type: string;
35
+ }[];
36
+ stateMutability: string;
37
+ type: string;
38
+ anonymous?: undefined;
39
+ } | {
40
+ stateMutability: string;
41
+ type: string;
42
+ inputs?: undefined;
43
+ name?: undefined;
44
+ anonymous?: undefined;
45
+ outputs?: undefined;
46
+ })[];