Skip to content

Commit

Permalink
keep back prefix okexchain address (#51)
Browse files Browse the repository at this point in the history
* convert prefix okexchain to ex

* update npm version

* keep back prefix okexchain address

* keep back prefix okexchain address
  • Loading branch information
zccst authored Apr 6, 2021
1 parent 03e4a7e commit bae458d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
21 changes: 16 additions & 5 deletions __tests__/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})


Expand Down
9 changes: 5 additions & 4 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
}


Expand Down

0 comments on commit bae458d

Please sign in to comment.