Skip to content

Commit

Permalink
Merge pull request #11926 from rastajpa/fix/ltc-legacy-addr
Browse files Browse the repository at this point in the history
fixes for LTC legacy addresses
  • Loading branch information
gabrielbazan7 committed Oct 13, 2021
2 parents 474fe4d + 70fc0c4 commit 0317f91
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/providers/profile/profile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ describe('Profile Provider', () => {
getBitcoreCash() {
return true;
}
getBitcoreLtc() {
return true;
}
getClient(_walletData, _opts) {
return _.clone(walletClientMock);
}
Expand Down
14 changes: 14 additions & 0 deletions src/providers/tx-format/tx-format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
15 changes: 15 additions & 0 deletions src/providers/tx-format/tx-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum Coin {
@Injectable()
export class TxFormatProvider {
private bitcoreCash;
private bitcoreLTC;

// TODO: implement configService
public pendingTxProposalsCountForUs: number;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0317f91

Please sign in to comment.