From aad181946ef2b09fdfcf0b950ae09c3eecc07f56 Mon Sep 17 00:00:00 2001 From: hotequil Date: Tue, 1 Oct 2024 13:22:06 -0300 Subject: [PATCH] CU-86a53wqqc-BS Lib - Implement cloneSelf method --- .../CU-86a53wqqc_2024-10-01-16-13.json | 10 ++++++++++ .../bs-ethereum/CU-86a53wqqc_2024-10-01-16-13.json | 10 ++++++++++ .../bs-neo-legacy/CU-86a53wqqc_2024-10-01-16-13.json | 10 ++++++++++ .../bs-neo3/CU-86a53wqqc_2024-10-01-16-13.json | 10 ++++++++++ packages/blockchain-service/src/interfaces.ts | 6 +++++- packages/bs-ethereum/src/BSEthereum.ts | 11 +++++++++-- packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts | 10 ++++++++-- .../src/__tests__/BlockscoutEDSEthereum.spec.ts | 2 +- .../src/__tests__/EthersLedgerServiceEthereum.spec.ts | 2 +- .../services/ledger/EthersLedgerServiceEthereum.ts | 5 +++-- .../bs-neo-legacy/src/__tests__/BDSNeoLegacy.spec.ts | 2 +- .../bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts | 8 +++++++- packages/bs-neo-legacy/src/services/BSNeoLegacy.ts | 4 ++++ packages/bs-neo3/src/BSNeo3.ts | 11 +++++++++-- .../bs-neo3/src/__tests__/services/BSNeo3.spec.ts | 10 ++++++++-- .../services/blockchain-data/DoraBDSNeo3.spec.ts | 2 +- .../services/ledger/NeonDappKitLedgerServiceNeo3.ts | 5 +++-- 17 files changed, 100 insertions(+), 18 deletions(-) create mode 100644 common/changes/@cityofzion/blockchain-service/CU-86a53wqqc_2024-10-01-16-13.json create mode 100644 common/changes/@cityofzion/bs-ethereum/CU-86a53wqqc_2024-10-01-16-13.json create mode 100644 common/changes/@cityofzion/bs-neo-legacy/CU-86a53wqqc_2024-10-01-16-13.json create mode 100644 common/changes/@cityofzion/bs-neo3/CU-86a53wqqc_2024-10-01-16-13.json diff --git a/common/changes/@cityofzion/blockchain-service/CU-86a53wqqc_2024-10-01-16-13.json b/common/changes/@cityofzion/blockchain-service/CU-86a53wqqc_2024-10-01-16-13.json new file mode 100644 index 0000000..ff176d6 --- /dev/null +++ b/common/changes/@cityofzion/blockchain-service/CU-86a53wqqc_2024-10-01-16-13.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/blockchain-service", + "comment": "Implement clone self method", + "type": "minor" + } + ], + "packageName": "@cityofzion/blockchain-service" +} diff --git a/common/changes/@cityofzion/bs-ethereum/CU-86a53wqqc_2024-10-01-16-13.json b/common/changes/@cityofzion/bs-ethereum/CU-86a53wqqc_2024-10-01-16-13.json new file mode 100644 index 0000000..49032d5 --- /dev/null +++ b/common/changes/@cityofzion/bs-ethereum/CU-86a53wqqc_2024-10-01-16-13.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-ethereum", + "comment": "Implement clone self method", + "type": "minor" + } + ], + "packageName": "@cityofzion/bs-ethereum" +} diff --git a/common/changes/@cityofzion/bs-neo-legacy/CU-86a53wqqc_2024-10-01-16-13.json b/common/changes/@cityofzion/bs-neo-legacy/CU-86a53wqqc_2024-10-01-16-13.json new file mode 100644 index 0000000..6844505 --- /dev/null +++ b/common/changes/@cityofzion/bs-neo-legacy/CU-86a53wqqc_2024-10-01-16-13.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-neo-legacy", + "comment": "Implement clone self method", + "type": "minor" + } + ], + "packageName": "@cityofzion/bs-neo-legacy" +} diff --git a/common/changes/@cityofzion/bs-neo3/CU-86a53wqqc_2024-10-01-16-13.json b/common/changes/@cityofzion/bs-neo3/CU-86a53wqqc_2024-10-01-16-13.json new file mode 100644 index 0000000..2538bc1 --- /dev/null +++ b/common/changes/@cityofzion/bs-neo3/CU-86a53wqqc_2024-10-01-16-13.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@cityofzion/bs-neo3", + "comment": "Implement clone self method", + "type": "minor" + } + ], + "packageName": "@cityofzion/bs-neo3" +} diff --git a/packages/blockchain-service/src/interfaces.ts b/packages/blockchain-service/src/interfaces.ts index f112d53..3b43265 100644 --- a/packages/blockchain-service/src/interfaces.ts +++ b/packages/blockchain-service/src/interfaces.ts @@ -46,6 +46,7 @@ export interface BlockchainService + clone: () => BlockchainService setNetwork: (partialNetwork: Network) => void generateAccountFromMnemonic(mnemonic: string | string, index: number): Account generateAccountFromKey(key: string): Account @@ -240,9 +241,12 @@ export type LedgerServiceEmitter = TypedEmitter<{ getSignatureStart(): void | Promise getSignatureEnd(): void | Promise }> + +export type GetLedgerTransport = (account: Account) => Promise + export interface LedgerService { emitter: LedgerServiceEmitter - getLedgerTransport?: (account: Account) => Promise + getLedgerTransport?: GetLedgerTransport getAccounts(transport: Transport): Promise getAccount(transport: Transport, index: number): Promise } diff --git a/packages/bs-ethereum/src/BSEthereum.ts b/packages/bs-ethereum/src/BSEthereum.ts index b8480ff..ab69e18 100644 --- a/packages/bs-ethereum/src/BSEthereum.ts +++ b/packages/bs-ethereum/src/BSEthereum.ts @@ -14,6 +14,7 @@ import { NftDataService, Token, TransferParam, + GetLedgerTransport, } from '@cityofzion/blockchain-service' import { ethers } from 'ethers' import * as ethersJsonWallets from '@ethersproject/json-wallets' @@ -21,7 +22,6 @@ import * as ethersBytes from '@ethersproject/bytes' import * as ethersBigNumber from '@ethersproject/bignumber' import { BSEthereumConstants, BSEthereumNetworkId } from './constants/BSEthereumConstants' import { EthersLedgerServiceEthereum } from './services/ledger/EthersLedgerServiceEthereum' -import Transport from '@ledgerhq/hw-transport' import { BSEthereumHelper } from './helpers/BSEthereumHelper' import { BlockscoutBDSEthereum } from './services/blockchain-data/BlockscoutBDSEthereum' import { BlockscoutEDSEthereum } from './services/exchange-data/BlockscoutEDSEthereum' @@ -39,6 +39,8 @@ export class BSEthereum BSWithLedger, BSWithExplorerService { + readonly #getLedgerTransport?: GetLedgerTransport + readonly blockchainName: BSCustomName readonly bip44DerivationPath: string @@ -54,12 +56,13 @@ export class BSEthereum constructor( blockchainName: BSCustomName, network?: Network, - getLedgerTransport?: (account: Account) => Promise + getLedgerTransport?: GetLedgerTransport ) { network = network ?? BSEthereumConstants.DEFAULT_NETWORK this.blockchainName = blockchainName this.ledgerService = new EthersLedgerServiceEthereum(this, getLedgerTransport) + this.#getLedgerTransport = getLedgerTransport this.bip44DerivationPath = BSEthereumConstants.DEFAULT_BIP44_DERIVATION_PATH this.setNetwork(network) @@ -121,6 +124,10 @@ export class BSEthereum this.feeToken = nativeAsset } + clone() { + return new BSEthereum(this.blockchainName, this.network, this.#getLedgerTransport) + } + setNetwork(network: Network) { this.#setTokens(network) diff --git a/packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts b/packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts index 47b11cb..4eaf43f 100644 --- a/packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts +++ b/packages/bs-ethereum/src/__tests__/BSEthereum.spec.ts @@ -80,6 +80,12 @@ describe('BSEthereum', () => { ) }) + it('Should be able to clone the BSEthereum', () => { + const newBsEthereum = bsEthereum.clone() + + expect(newBsEthereum).toEqual(bsEthereum) + }) + it.skip('Should be able to calculate transfer fee', async () => { const account = bsEthereum.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string) @@ -179,7 +185,7 @@ describe('BSEthereum', () => { await transport.close() }, 50000) - it('Should be able to resolve a name service domain', async () => { + it.skip('Should be able to resolve a name service domain', async () => { const address = await bsEthereum.resolveNameServiceDomain('alice.eth') expect(address).toEqual('0xa974890156A3649A23a6C0f2ebd77D6F7A7333d4') }, 10000) @@ -247,7 +253,7 @@ describe('BSEthereum', () => { expect(fee).toEqual(expect.any(String)) }, 50000) - it.only('Should be able to transfer more than one intent', async () => { + it.skip('Should be able to transfer more than one intent', async () => { const account = bsEthereum.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string) const transactionHashes = await bsEthereum.transfer({ diff --git a/packages/bs-ethereum/src/__tests__/BlockscoutEDSEthereum.spec.ts b/packages/bs-ethereum/src/__tests__/BlockscoutEDSEthereum.spec.ts index e9dc200..9761da0 100644 --- a/packages/bs-ethereum/src/__tests__/BlockscoutEDSEthereum.spec.ts +++ b/packages/bs-ethereum/src/__tests__/BlockscoutEDSEthereum.spec.ts @@ -5,7 +5,7 @@ import { BlockscoutEDSEthereum } from '../services/exchange-data/BlockscoutEDSEt const network = BSEthereumConstants.MAINNET_NETWORKS.find(network => network.id === '47763')! describe('BlockscoutEDSEthereum', () => { - it('Should return token prices', async () => { + it.skip('Should return token prices', async () => { const params: GetTokenPricesParams = { tokens: [ { diff --git a/packages/bs-ethereum/src/__tests__/EthersLedgerServiceEthereum.spec.ts b/packages/bs-ethereum/src/__tests__/EthersLedgerServiceEthereum.spec.ts index 51570b8..68a4c5c 100644 --- a/packages/bs-ethereum/src/__tests__/EthersLedgerServiceEthereum.spec.ts +++ b/packages/bs-ethereum/src/__tests__/EthersLedgerServiceEthereum.spec.ts @@ -9,7 +9,7 @@ let ledgerService: EthersLedgerServiceEthereum let transport: Transport let bsEthereum: BSEthereum -describe('EthersLedgerServiceEthereum', () => { +describe.skip('EthersLedgerServiceEthereum', () => { beforeAll(async () => { const network = BSEthereumConstants.TESTNET_NETWORKS.find(network => network.id === '11155111')! bsEthereum = new BSEthereum('ethereum', network) diff --git a/packages/bs-ethereum/src/services/ledger/EthersLedgerServiceEthereum.ts b/packages/bs-ethereum/src/services/ledger/EthersLedgerServiceEthereum.ts index 40824f2..10fe54c 100644 --- a/packages/bs-ethereum/src/services/ledger/EthersLedgerServiceEthereum.ts +++ b/packages/bs-ethereum/src/services/ledger/EthersLedgerServiceEthereum.ts @@ -3,6 +3,7 @@ import { LedgerService, LedgerServiceEmitter, fetchAccountsForBlockchainServices, + GetLedgerTransport, } from '@cityofzion/blockchain-service' import Transport from '@ledgerhq/hw-transport' import LedgerEthereumApp, { ledgerService as LedgerEthereumAppService } from '@ledgerhq/hw-app-eth' @@ -160,9 +161,9 @@ export class EthersLedgerSigner extends Signer implements TypedDataSigner { export class EthersLedgerServiceEthereum implements LedgerService { #blockchainService: BSEthereum emitter: LedgerServiceEmitter = new EventEmitter() as LedgerServiceEmitter - getLedgerTransport?: (account: Account) => Promise + getLedgerTransport?: GetLedgerTransport - constructor(blockchainService: BSEthereum, getLedgerTransport?: (account: Account) => Promise) { + constructor(blockchainService: BSEthereum, getLedgerTransport?: GetLedgerTransport) { this.#blockchainService = blockchainService this.getLedgerTransport = getLedgerTransport } diff --git a/packages/bs-neo-legacy/src/__tests__/BDSNeoLegacy.spec.ts b/packages/bs-neo-legacy/src/__tests__/BDSNeoLegacy.spec.ts index 63a6846..efd2052 100644 --- a/packages/bs-neo-legacy/src/__tests__/BDSNeoLegacy.spec.ts +++ b/packages/bs-neo-legacy/src/__tests__/BDSNeoLegacy.spec.ts @@ -83,7 +83,7 @@ describe('BDSNeoLegacy', () => { }) it.each([doraBDSNeoLegacy])('Should be able to get token info - %s', async (bdsNeoLegacy: BlockchainDataService) => { - const hash = '0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7' + const hash = '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7' const token = await bdsNeoLegacy.getTokenInfo(hash) expect(token).toEqual({ decimals: 8, diff --git a/packages/bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts b/packages/bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts index da8ac8e..c68df36 100644 --- a/packages/bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts +++ b/packages/bs-neo-legacy/src/__tests__/BSNeoLegacy.spec.ts @@ -70,6 +70,12 @@ describe('BSNeoLegacy', () => { expect(encryptedKey).toEqual(expect.any(String)) }) + it('Should be able to clone the BSNeoLegacy', () => { + const newBsNeoLegacy = bsNeoLegacy.clone() + + expect(newBsNeoLegacy).toEqual(bsNeoLegacy) + }) + it.skip('Should be able to transfer a native asset', async () => { const account = bsNeoLegacy.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string) const balance = await bsNeoLegacy.blockchainDataService.getBalance(account.address) @@ -143,7 +149,7 @@ describe('BSNeoLegacy', () => { expect(transactionHash).toEqual(expect.any(String)) }) - it.only('Should be able to transfer more than one intent', async () => { + it.skip('Should be able to transfer more than one intent', async () => { bsNeoLegacy.setNetwork(BSNeoLegacyConstants.DEFAULT_NETWORK) const account = bsNeoLegacy.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string) const balance = await bsNeoLegacy.blockchainDataService.getBalance(account.address) diff --git a/packages/bs-neo-legacy/src/services/BSNeoLegacy.ts b/packages/bs-neo-legacy/src/services/BSNeoLegacy.ts index 6abf117..010bbea 100644 --- a/packages/bs-neo-legacy/src/services/BSNeoLegacy.ts +++ b/packages/bs-neo-legacy/src/services/BSNeoLegacy.ts @@ -55,6 +55,10 @@ export class BSNeoLegacy this.claimToken = tokens.find(token => token.symbol === 'GAS')! } + clone() { + return new BSNeoLegacy(this.blockchainName, this.network) + } + setNetwork(network: Network) { if (!BSNeoLegacyConstants.ALL_NETWORK_IDS.includes(network.id)) throw new Error('Custom network is not supported') diff --git a/packages/bs-neo3/src/BSNeo3.ts b/packages/bs-neo3/src/BSNeo3.ts index 7a34495..898cb6e 100644 --- a/packages/bs-neo3/src/BSNeo3.ts +++ b/packages/bs-neo3/src/BSNeo3.ts @@ -12,6 +12,7 @@ import { BSWithSwap, ExchangeDataService, ExplorerService, + GetLedgerTransport, Network, NftDataService, SwapService, @@ -23,7 +24,6 @@ import Neon from '@cityofzion/neon-core' import { NeonInvoker, NeonParser } from '@cityofzion/neon-dappkit' import { ContractInvocation } from '@cityofzion/neon-dappkit-types' import { api, u, wallet } from '@cityofzion/neon-js' -import Transport from '@ledgerhq/hw-transport' import { BSNeo3Helper } from './helpers/BSNeo3Helper' import { DoraBDSNeo3 } from './services/blockchain-data/DoraBDSNeo3' import { FlamingoEDSNeo3 } from './services/exchange-data/FlamingoEDSNeo3' @@ -44,6 +44,8 @@ export class BSNeo3 BSWithLedger, BSWithSwap { + readonly #getLedgerTransport?: GetLedgerTransport + blockchainName: BSCustomName bip44DerivationPath: string @@ -63,12 +65,13 @@ export class BSNeo3 constructor( blockchainName: BSCustomName, network?: Network, - getLedgerTransport?: (account: Account) => Promise + getLedgerTransport?: GetLedgerTransport ) { network = network ?? BSNeo3Constants.DEFAULT_NETWORK this.blockchainName = blockchainName this.ledgerService = new NeonDappKitLedgerServiceNeo3(this, getLedgerTransport) + this.#getLedgerTransport = getLedgerTransport this.bip44DerivationPath = BSNeo3Constants.DEFAULT_BIP44_DERIVATION_PATH this.setNetwork(network) @@ -83,6 +86,10 @@ export class BSNeo3 this.claimToken = tokens.find(token => token.symbol === 'GAS')! } + clone() { + return new BSNeo3(this.blockchainName, this.network, this.#getLedgerTransport) + } + async generateSigningCallback(account: Account, isLedger?: boolean) { const neonJsAccount = new wallet.Account(account.key) diff --git a/packages/bs-neo3/src/__tests__/services/BSNeo3.spec.ts b/packages/bs-neo3/src/__tests__/services/BSNeo3.spec.ts index 78b296f..0b8abe0 100644 --- a/packages/bs-neo3/src/__tests__/services/BSNeo3.spec.ts +++ b/packages/bs-neo3/src/__tests__/services/BSNeo3.spec.ts @@ -86,6 +86,12 @@ describe('BSNeo3', () => { expect(encryptedKey).toEqual(expect.any(String)) }) + it('Should be able to clone the BSNeo3', () => { + const newBsNeo3 = bsNeo3.clone() + + expect(newBsNeo3).toEqual(bsNeo3) + }) + it.skip('Should be able to calculate transfer fee', async () => { const account = bsNeo3.generateAccountFromKey(process.env.TESTNET_PRIVATE_KEY as string) @@ -125,7 +131,7 @@ describe('BSNeo3', () => { expect(transactionHash).toEqual(expect.any(String)) }) - it.only('Should be able to transfer with ledger', async () => { + it.skip('Should be able to transfer with ledger', async () => { const transport = await TransportNodeHid.create() const service = new BSNeo3('neo3', network, async () => transport) @@ -225,7 +231,7 @@ describe('BSNeo3', () => { expect(transactionHash).toEqual(expect.any(String)) }) - it.only('Should be able to transfer more than one intent with ledger', async () => { + it.skip('Should be able to transfer more than one intent with ledger', async () => { const transport = await TransportNodeHid.create() const service = new BSNeo3('neo3', network, async () => transport) diff --git a/packages/bs-neo3/src/__tests__/services/blockchain-data/DoraBDSNeo3.spec.ts b/packages/bs-neo3/src/__tests__/services/blockchain-data/DoraBDSNeo3.spec.ts index f888c6f..fbec9a5 100644 --- a/packages/bs-neo3/src/__tests__/services/blockchain-data/DoraBDSNeo3.spec.ts +++ b/packages/bs-neo3/src/__tests__/services/blockchain-data/DoraBDSNeo3.spec.ts @@ -110,7 +110,7 @@ describe('DoraBDSNeo3', () => { }, }) }) - }) + }, 10000) it('Should be able to get unclaimed', async () => { const address = 'NNmTVFrSPhe7zjgN6iq9cLgXJwLZziUKV6' diff --git a/packages/bs-neo3/src/services/ledger/NeonDappKitLedgerServiceNeo3.ts b/packages/bs-neo3/src/services/ledger/NeonDappKitLedgerServiceNeo3.ts index 5d9f07b..2350d22 100644 --- a/packages/bs-neo3/src/services/ledger/NeonDappKitLedgerServiceNeo3.ts +++ b/packages/bs-neo3/src/services/ledger/NeonDappKitLedgerServiceNeo3.ts @@ -3,6 +3,7 @@ import { LedgerService, LedgerServiceEmitter, fetchAccountsForBlockchainServices, + GetLedgerTransport, } from '@cityofzion/blockchain-service' import { NeonParser } from '@cityofzion/neon-dappkit' import { api, u, wallet } from '@cityofzion/neon-js' @@ -28,9 +29,9 @@ export class NeonDappKitLedgerServiceNeo3 implements LedgerService { #blockchainService: BSNeo3 emitter: LedgerServiceEmitter = new EventEmitter() as LedgerServiceEmitter - getLedgerTransport?: (account: Account) => Promise + getLedgerTransport?: GetLedgerTransport - constructor(blockchainService: BSNeo3, getLedgerTransport?: (account: Account) => Promise) { + constructor(blockchainService: BSNeo3, getLedgerTransport?: GetLedgerTransport) { this.#blockchainService = blockchainService this.getLedgerTransport = getLedgerTransport }