From 70fc0c4c72f064e58050835d44e829bb32714641 Mon Sep 17 00:00:00 2001 From: rastajpa Date: Wed, 13 Oct 2021 11:01:27 -0300 Subject: [PATCH] fixes for LTC legacy addresses --- src/providers/profile/profile.spec.ts | 3 +++ src/providers/tx-format/tx-format.spec.ts | 14 ++++++++++++++ src/providers/tx-format/tx-format.ts | 15 +++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/providers/profile/profile.spec.ts b/src/providers/profile/profile.spec.ts index ceb5eb3cf16..903600b8c6b 100644 --- a/src/providers/profile/profile.spec.ts +++ b/src/providers/profile/profile.spec.ts @@ -298,6 +298,9 @@ describe('Profile Provider', () => { getBitcoreCash() { return true; } + getBitcoreLtc() { + return true; + } getClient(_walletData, _opts) { return _.clone(walletClientMock); } diff --git a/src/providers/tx-format/tx-format.spec.ts b/src/providers/tx-format/tx-format.spec.ts index fa6b7ed978c..b66aee39d33 100644 --- a/src/providers/tx-format/tx-format.spec.ts +++ b/src/providers/tx-format/tx-format.spec.ts @@ -374,4 +374,18 @@ describe('TxFormatProvider', () => { expect(result).toEqual(0.12312312); }); }); + + describe('toLTCAddress', () => { + it('should get the address in new LTC Address format', () => { + let address = '33k1rEnWskMrr8RZEACJdQFRMLWovhSJ5R'; // LTC livenet legacy address (P2SH) + let ltcAddr: string = txFormatProvider.toLTCAddress(address); + expect(ltcAddr).toEqual('M9xAA8CUpsDHedhTL3BeT3Vpg37FyZyZLk'); + }); + + it('should keep the address if it is a new format', () => { + let address = 'M9xAA8CUpsDHedhTL3BeT3Vpg37FyZyZLk'; // LTC livenet new address (P2SH) + let ltcAddr: string = txFormatProvider.toLTCAddress(address); + expect(ltcAddr).toEqual('M9xAA8CUpsDHedhTL3BeT3Vpg37FyZyZLk'); + }); + }); }); diff --git a/src/providers/tx-format/tx-format.ts b/src/providers/tx-format/tx-format.ts index efdd88effc7..c93c4d5372a 100644 --- a/src/providers/tx-format/tx-format.ts +++ b/src/providers/tx-format/tx-format.ts @@ -24,6 +24,7 @@ export enum Coin { @Injectable() export class TxFormatProvider { private bitcoreCash; + private bitcoreLTC; // TODO: implement configService public pendingTxProposalsCountForUs: number; @@ -38,12 +39,17 @@ export class TxFormatProvider { ) { this.logger.debug('TxFormatProvider initialized'); this.bitcoreCash = this.bwcProvider.getBitcoreCash(); + this.bitcoreLTC = this.bwcProvider.getBitcoreLtc(); } public toCashAddress(address: string, withPrefix?: boolean): string { return this.bitcoreCash.Address(address).toString(!withPrefix); } + public toLTCAddress(address: string) { + return this.bitcoreLTC.Address(address).toString(); + } + public toLegacyAddress(address: string): string { let legacyAddr: string = this.bitcoreCash .Address(address) @@ -163,6 +169,13 @@ export class TxFormatProvider { } tx.toAddress = tx.outputs[0].toAddress; + // translate legacy addresses + if (tx.addressTo && coin == 'ltc') { + for (let o of tx.outputs) { + o.address = o.addressToShow = this.toLTCAddress(tx.addressTo); + } + } + // toDo: translate all tx.outputs[x].toAddress ? if (tx.toAddress && coin == 'bch') { tx.toAddress = this.toCashAddress(tx.toAddress); @@ -198,6 +211,8 @@ export class TxFormatProvider { if (tx.addressTo && coin == 'bch') { tx.addressTo = this.toCashAddress(tx.addressTo); + } else if (tx.addressTo && coin == 'ltc') { + tx.addressTo = this.toLTCAddress(tx.addressTo); } return tx;