Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
tsconfig.json
.history/
/dist
/bulid
/lib
8 changes: 4 additions & 4 deletions __tests__/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import * as crypto from "../src/crypto"
const mnemonic = "total lottery arena when pudding best candy until army spoil drill pool"
const privateKey_996 = "29892b64003fc5c8c89dc795a2ae82aa84353bb4352f28707c2ed32aa1011884"
const privateKey = "828e61f969a7369f3340b07dd2080740d8445d7f802899ddacf9bc4db8608997"
const from_996 = "okexchain1pt7xrmxul7sx54ml44lvv403r06clrdkgmvr9g"
const from = "okexchain1ya7dn2rr8nx07tx9ksq8gvz5utvarrh03cen3l"
const from_996 = "ex1jjvpmgwwgs99nhlje3aag0lackunqgj7pcgnd4"
const from = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn"
const serverUrl = "https://exchaintest.okexcn.com"
// const serverUrl = "https://exchaintest.okexcn.com"
const userAddress = "okexchain1jjvpmgwwgs99nhlje3aag0lackunqgj7xnrnwe"
const userAddress = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn"
const chainId = "okexchain-65" // -testnet1
const baseCoin = "okt"
const testCoin = "xxb-781"
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("OKEXChainClient test", async () => {
it("send sendRegisterDexOperatorTransaction", async () => {
jest.setTimeout(10000)
const data = await prepareAccount()
const res = await data.okclient.sendRegisterDexOperatorTransaction("http://test.json", "okexchain14zg88reaad4czrcnf93esftwe44gpev9cqhkny", "add deposit", data.sequence)
const res = await data.okclient.sendRegisterDexOperatorTransaction("http://test.json", "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn", "add deposit", data.sequence)
console.log(JSON.stringify(res))
expect(res.status).toBe(200)
})
Expand Down
19 changes: 11 additions & 8 deletions __tests__/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ describe("crypto", () => {

it("getAddressFromPrivateKey", () => {
const address = crypto.getAddressFromPrivateKey(privateKey)
expect(address).toBe("okexchain1jjvpmgwwgs99nhlje3aag0lackunqgj7xnrnwe")
expect(address.length).toBe(48)
expect(address).toBe("ex1jjvpmgwwgs99nhlje3aag0lackunqgj7pcgnd4")
expect(address.length).toBe(41)
})

it("getAddressFromPubKey", () => {
const publicKey = crypto.getPubKeyHexFromPrivateKey(privateKey)
const address = crypto.getAddressFromPubKey(publicKey)
expect(address).toBe("okexchain1jjvpmgwwgs99nhlje3aag0lackunqgj7xnrnwe")
expect(address).toBe("ex1jjvpmgwwgs99nhlje3aag0lackunqgj7pcgnd4")
})

it("getPrivateKeyFromKeyStore", () => {
const keyStore = crypto.generateKeyStore(privateKey, "okexchain")
const pk = crypto.getPrivateKeyFromKeyStore(keyStore, "okexchain")
const keyStore = crypto.generateKeyStore(privateKey, "ex")
const pk = crypto.getPrivateKeyFromKeyStore(keyStore, "ex")
expect(pk).toBe(privateKey)
})

Expand All @@ -41,9 +41,10 @@ describe("crypto", () => {
})

it("decodeAddressToBuffer", ()=>{
let address = "okexchain1g7c3nvac7mjgn2m9mqllgat8wwd3aptddw77gw"
let address = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn"
const decod = crypto.decodeAddressToBuffer(address)
expect(decod.toString("hex")).toBe("47b119b3b8f6e489ab65d83ff47567739b1e856d")
console.log(decod.toString("hex"));
expect(decod.toString("hex")).toBe("277cd9a8633cccff2cc5b400743054e2d9d18eef")
})

it("sign", () => {
Expand All @@ -60,9 +61,11 @@ describe("crypto", () => {
})

it("bech32 and hex", () => {
let bech32Address = "okexchain1g7c3nvac7mjgn2m9mqllgat8wwd3aptddw77gw"
let bech32Address = "ex1ya7dn2rr8nx07tx9ksq8gvz5utvarrh0knjnjn"
let hexAddress = crypto.convertBech32ToHex(bech32Address)
console.log('hexAddress', hexAddress);

// let hexAddress = "0x277CD9a8633ccCFF2Cc5B400743054e2d9d18eEf"
let newBech32Address = crypto.convertHexToBech32(hexAddress)
console.log('newBech32Address', newBech32Address)
expect(bech32Address).toBe(newBech32Address)
Expand Down
4 changes: 2 additions & 2 deletions docs/okexchain-jssdk-doc-crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Encodes address from hex to bech32 format.
<a name="module_crypto.convertBech32ToHex"></a>

### crypto.convertBech32ToHex ⇒ <code>String</code>
covert okexchain address to 0x address
covert ex address to 0x address

**Kind**: static constant of [<code>crypto</code>](#module_crypto)

Expand All @@ -92,7 +92,7 @@ covert okexchain address to 0x address
<a name="module_crypto.convertHexToBech32"></a>

### crypto.convertHexToBech32 ⇒ <code>string</code>
covert 0x address to okexchain address
covert 0x address to ex address

**Kind**: static constant of [<code>crypto</code>](#module_crypto)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okexchain/javascript-sdk",
"version": "0.0.32",
"version": "0.16.1",
"license": "Apache-2.0",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as wallet from './wallet'

const defaultChainId = "okexchain-66"
const defaultRelativePath = "/okexchain/v1"
const bech32Head = "okexchain"
const bech32Head = "ex"
const mode = "block"
const nativeDenom = "okt"
const defaultTestnetFee = {
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const decodeAddressToBuffer = (addr) => {
export const validateAddress = (addr) => {
try {
const decodeAddress = bech32.decode(addr)
if(decodeAddress.prefix === "okexchain") {
if(decodeAddress.prefix === "ex") {
return true
}

Expand All @@ -68,7 +68,7 @@ export const validateAddress = (addr) => {
* @param {string} prefix address prefix
* @return {string} address with bech32 format
*/
export const encodeAddressToBech32 = (hexAddr, prefix = "okexchain") => {
export const encodeAddressToBech32 = (hexAddr, prefix = "ex") => {
hexAddr = hexAddr.slice(0, 2) === '0x' ? hexAddr.slice(2) : hexAddr
const words = bech32.toWords(Buffer.from(hexAddr, "hex"))
return bech32.encode(prefix, words)
Expand All @@ -79,7 +79,7 @@ function buf2hex(buffer) { // buffer is an ArrayBuffer
}

/**
* covert okexchain address to 0x address
* covert ex address to 0x address
* @param bech32Address
* @returns {String}
*/
Expand All @@ -88,7 +88,7 @@ export const convertBech32ToHex = (bech32Address) => {
return toChecksumAddress("0x"+buf2hex(address))
}
/**
* covert 0x address to okexchain address
* covert 0x address to ex address
* @param hexAddress
* @returns {string}
*/
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const GET_SIGN = {
method: 'okt_signTransaction'
};

const OKEXCHAIN = 'okexchain';
const EXCHAIN = 'ex';

// const DURING = 5000;

Expand Down Expand Up @@ -48,7 +48,7 @@ class Connector {
onDisconnect() {
this.killSession();
}

async getAccounts() {
const walletConnector = this.walletConnector;
if(!walletConnector) return '';
Expand All @@ -65,7 +65,7 @@ class Connector {
console.log('get address params: ' + JSON.stringify(params));
walletConnector.sendCustomRequest(params).then((res) => {
const okexchainAccount = res.find((account) => {
return account.address.startsWith(OKEXCHAIN);
return account.address.startsWith(EXCHAIN);
});
if (okexchainAccount) {
address = okexchainAccount.address;
Expand Down Expand Up @@ -172,7 +172,7 @@ class Connector {
doCallback(type,params) {
if(typeof this.callback[type] === 'function' )this.callback[type](params);
}

async getSession(callback) {
this.setCallback(callback);
let session = '';
Expand Down