diff --git a/web/cashtab/src/utils/__tests__/cashMethods.test.js b/web/cashtab/src/utils/__tests__/cashMethods.test.js --- a/web/cashtab/src/utils/__tests__/cashMethods.test.js +++ b/web/cashtab/src/utils/__tests__/cashMethods.test.js @@ -30,6 +30,7 @@ generateSendOpReturn, generateBurnOpReturn, getECPairFromWIF, + hash160ToAddress, } from 'utils/cashMethods'; import { currency } from 'components/Common/Ticker'; import { validAddressArrayInput } from '../__mocks__/mockAddressArray'; @@ -1564,4 +1565,9 @@ mockStringifiedECPair, ); }); + it(`Converts a hash160 to an ecash address`, () => { + expect( + hash160ToAddress('76458db0ed96fe9863fc1ccec9fa2cfab884b0f6'), + ).toBe('ecash:qpmytrdsakt0axrrlswvaj069nat3p9s7cjctmjasj'); + }); }); diff --git a/web/cashtab/src/utils/cashMethods.js b/web/cashtab/src/utils/cashMethods.js --- a/web/cashtab/src/utils/cashMethods.js +++ b/web/cashtab/src/utils/cashMethods.js @@ -1143,3 +1143,19 @@ ws._subs.length > 0 ); }; + +export const hash160ToAddress = hash160 => { + const buffer = Buffer.from(hash160, 'hex'); + + // Because ecashaddrjs only accepts Uint8Array as input type, convert + const hash160ArrayBuffer = new ArrayBuffer(buffer.length); + const hash160Uint8Array = new Uint8Array(hash160ArrayBuffer); + for (let i = 0; i < hash160Uint8Array.length; i += 1) { + hash160Uint8Array[i] = buffer[i]; + } + + // Encode ecash: address + const ecashAddr = cashaddr.encode('ecash', 'P2PKH', hash160Uint8Array); + + return ecashAddr; +};