Skip to content

Commit bae458d

Browse files
authored
keep back prefix okexchain address (#51)
* convert prefix okexchain to ex * update npm version * keep back prefix okexchain address * keep back prefix okexchain address
1 parent 03e4a7e commit bae458d

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

__tests__/crypto.test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,26 @@ describe("crypto", () => {
6060
expect(crypto.validateSig(sig, msg, publicKey)).toBeTruthy()
6161
})
6262

63-
it("bech32 and hex", () => {
63+
it("convertBech32ToHex", () => {
6464
let bech32Address = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn"
65-
let hexAddress = crypto.convertBech32ToHex(bech32Address)
66-
console.log('hexAddress', hexAddress);
65+
let newHexAddress = crypto.convertBech32ToHex(bech32Address)
66+
console.log('newHexAddress', newHexAddress);
6767

68-
// let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf"
68+
let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf"
69+
let okexchainAddress = "okexchain1ya7dn2rr8nx07tx9ksq8gvz5utvarrh03cen3l"
70+
expect(hexAddress).toBe(newHexAddress[0])
71+
expect(okexchainAddress).toBe(newHexAddress[1])
72+
})
73+
74+
it("convertHexToBech32", () => {
75+
let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf"
6976
let newBech32Address = crypto.convertHexToBech32(hexAddress)
7077
console.log('newBech32Address', newBech32Address)
71-
expect(bech32Address).toBe(newBech32Address)
78+
79+
let exAddress = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn"
80+
let okexchainAddress = "okexchain1ya7dn2rr8nx07tx9ksq8gvz5utvarrh03cen3l"
81+
expect(exAddress).toBe(newBech32Address[0])
82+
expect(okexchainAddress).toBe(newBech32Address[1])
7283
})
7384

7485

src/crypto/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,20 @@ function buf2hex(buffer) { // buffer is an ArrayBuffer
8181
/**
8282
* covert ex address to 0x address
8383
* @param bech32Address
84-
* @returns {String}
84+
* @returns {Array}
8585
*/
8686
export const convertBech32ToHex = (bech32Address) => {
8787
const address = decodeAddressToBuffer(bech32Address)
88-
return toChecksumAddress("0x"+buf2hex(address))
88+
const hexAddress = toChecksumAddress("0x"+buf2hex(address))
89+
return [hexAddress, encodeAddressToBech32(hexAddress.toLowerCase(), 'okexchain')]
8990
}
9091
/**
9192
* covert 0x address to ex address
9293
* @param hexAddress
93-
* @returns {string}
94+
* @returns {Array}
9495
*/
9596
export const convertHexToBech32 = (hexAddress) => {
96-
return encodeAddressToBech32(hexAddress.toLowerCase())
97+
return [encodeAddressToBech32(hexAddress.toLowerCase()), encodeAddressToBech32(hexAddress.toLowerCase(), 'okexchain')]
9798
}
9899

99100

0 commit comments

Comments
 (0)