From bae458db5645499a04e39e103721c9c3d4c2e9fe Mon Sep 17 00:00:00 2001 From: zccst Date: Tue, 6 Apr 2021 18:43:44 +0800 Subject: [PATCH] keep back prefix okexchain address (#51) * convert prefix okexchain to ex * update npm version * keep back prefix okexchain address * keep back prefix okexchain address --- __tests__/crypto.test.js | 21 ++++++++++++++++----- src/crypto/index.js | 9 +++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/__tests__/crypto.test.js b/__tests__/crypto.test.js index 2ccbfc7..48653e8 100644 --- a/__tests__/crypto.test.js +++ b/__tests__/crypto.test.js @@ -60,15 +60,26 @@ describe("crypto", () => { expect(crypto.validateSig(sig, msg, publicKey)).toBeTruthy() }) - it("bech32 and hex", () => { + it("convertBech32ToHex", () => { let bech32Address = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn" - let hexAddress = crypto.convertBech32ToHex(bech32Address) - console.log('hexAddress', hexAddress); + let newHexAddress = crypto.convertBech32ToHex(bech32Address) + console.log('newHexAddress', newHexAddress); - // let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf" + let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf" + let okexchainAddress = "okexchain1ya7dn2rr8nx07tx9ksq8gvz5utvarrh03cen3l" + expect(hexAddress).toBe(newHexAddress[0]) + expect(okexchainAddress).toBe(newHexAddress[1]) + }) + + it("convertHexToBech32", () => { + let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf" let newBech32Address = crypto.convertHexToBech32(hexAddress) console.log('newBech32Address', newBech32Address) - expect(bech32Address).toBe(newBech32Address) + + let exAddress = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn" + let okexchainAddress = "okexchain1ya7dn2rr8nx07tx9ksq8gvz5utvarrh03cen3l" + expect(exAddress).toBe(newBech32Address[0]) + expect(okexchainAddress).toBe(newBech32Address[1]) }) diff --git a/src/crypto/index.js b/src/crypto/index.js index b5d92fa..8e3ab4e 100644 --- a/src/crypto/index.js +++ b/src/crypto/index.js @@ -81,19 +81,20 @@ function buf2hex(buffer) { // buffer is an ArrayBuffer /** * covert ex address to 0x address * @param bech32Address - * @returns {String} + * @returns {Array} */ export const convertBech32ToHex = (bech32Address) => { const address = decodeAddressToBuffer(bech32Address) - return toChecksumAddress("0x"+buf2hex(address)) + const hexAddress = toChecksumAddress("0x"+buf2hex(address)) + return [hexAddress, encodeAddressToBech32(hexAddress.toLowerCase(), 'okexchain')] } /** * covert 0x address to ex address * @param hexAddress - * @returns {string} + * @returns {Array} */ export const convertHexToBech32 = (hexAddress) => { - return encodeAddressToBech32(hexAddress.toLowerCase()) + return [encodeAddressToBech32(hexAddress.toLowerCase()), encodeAddressToBech32(hexAddress.toLowerCase(), 'okexchain')] }