Skip to content

Commit 35cd866

Browse files
zhshy11shaoyun.zhan@okcoin.com
andauthored
Merge PR: modify prefix from okexchain to ex (#94)
* add 'ex' address prefix * add 'ex' address prefix * add 'ex' address prefix, modify version to 0.17.0 * add 'ex' address prefix : add ut Co-authored-by: shaoyun.zhan@okcoin.com <shaoyun.zhan@okcoin.com>
1 parent 21f57a7 commit 35cd866

5 files changed

Lines changed: 141 additions & 12 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.okexchain</groupId>
88
<artifactId>okexchain-java-sdk</artifactId>
9-
<version>0.16.6</version>
9+
<version>0.17.0</version>
1010

1111
<name>okexchain-java-sdk</name>
1212
<!-- FIXME change it to the project's website -->

src/main/java/com/okexchain/env/EnvBase.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.okexchain.env;
22

3-
import com.okexchain.utils.Utils;
4-
53
public class EnvBase {
64

75
protected String restServerUrl;
@@ -17,13 +15,16 @@ public class EnvBase {
1715

1816
public EnvBase() {
1917
this.restServerUrl = "http://127.0.0.1:8545";
20-
this.mainPrefix = "okexchain";
18+
this.mainPrefix = "ex";
2119
this.denom = "okt";
20+
//todo
2221
this.chainID = "okexchain-1";
2322
this.hdPath = "M/44H/60H/0H/0/0";
24-
this.validatorAddrPrefix = "okexchainvaloper";
25-
this.pubPrefix = "okexchainpub";
23+
this.validatorAddrPrefix = "exvaloper";
24+
this.pubPrefix = "expub";
25+
//todo
2626
this.restPathPrefix = "/okexchain/v1";
27+
//todo
2728
this.txUrlPath = "/okexchain/v1/txs";
2829
this.accountUrlPath = "/auth/accounts/";
2930
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.okexchain.sample;
2+
3+
import com.okexchain.utils.crypto.AddressConvertUtil;
4+
import com.okexchain.utils.crypto.PrivateKey;
5+
6+
/**
7+
* @author shaoyun.zhan
8+
* @date 2021/3/31
9+
* <p>
10+
* 描述:
11+
*/
12+
public class AddressConvertUtilSample {
13+
public static void main(String[] args) {
14+
//"eth_address": "0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a"
15+
//"privateKey": "2843de7dec93946f1022ec9355678fdec3dc49d3140d2314b452a3a4afe78191"
16+
System.out.println(new PrivateKey("2843de7dec93946f1022ec9355678fdec3dc49d3140d2314b452a3a4afe78191").getAddress());
17+
System.out.println(AddressConvertUtil.convertFromBech32ToHex("okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56").equals("0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a"));
18+
System.out.println(AddressConvertUtil.convertFromBech32ToHex("ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk").equals("0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a"));
19+
System.out.println(AddressConvertUtil.convertFromHexToOkexchainBech32("0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a").equals("okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56"));
20+
System.out.println(AddressConvertUtil.convertFromHexToExBech32("0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a").equals("ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk"));
21+
System.out.println(AddressConvertUtil.convertFromExBech32ToOkexchainBech32("ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk").equals("okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56"));
22+
System.out.println(AddressConvertUtil.convertFromOkexchainBech32ToExBech32("okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56").equals("ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk"));
23+
24+
}
25+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.okexchain.utils.crypto;
2+
3+
import com.okexchain.utils.crypto.encode.Bech32;
4+
import com.okexchain.utils.crypto.encode.ConvertBits;
5+
import com.okexchain.utils.exception.AddressFormatException;
6+
import org.web3j.crypto.Keys;
7+
import org.web3j.utils.Numeric;
8+
9+
public class AddressConvertUtil {
10+
11+
private static final String okexchain = "okexchain";
12+
private static final String ex = "ex";
13+
14+
// nothing to do with prefix
15+
public static String convertFromBech32ToHex(String address) {
16+
return convertAddressFromBech32ToHex(address);
17+
}
18+
19+
public static String convertFromHexToOkexchainBech32(String address) {
20+
return convertAddressFromHexToBech32(okexchain, address);
21+
}
22+
23+
public static String convertFromHexToExBech32(String address) {
24+
return convertAddressFromHexToBech32(ex, address);
25+
}
26+
27+
public static String convertFromExBech32ToOkexchainBech32(String address) {
28+
String hexAddress = convertAddressFromBech32ToHex(address);
29+
return convertAddressFromHexToBech32(okexchain, hexAddress);
30+
}
31+
32+
public static String convertFromOkexchainBech32ToExBech32(String address) {
33+
String hexAddress = convertAddressFromBech32ToHex(address);
34+
return convertAddressFromHexToBech32(ex, hexAddress);
35+
}
36+
37+
38+
public static String convertAddressFromHexToBech32(String prefix, String hexAddress) {
39+
byte[] address = Numeric.hexStringToByteArray(hexAddress);
40+
41+
String bech32Address = null;
42+
try {
43+
byte[] bytes = encode(0, address);
44+
bech32Address = Bech32.encode(prefix, bytes);
45+
} catch (Exception e) {
46+
throw new RuntimeException(e);
47+
}
48+
return bech32Address;
49+
}
50+
51+
public static String convertAddressFromBech32ToHex(String bech32Address){
52+
String hexAddress = null;
53+
try {
54+
byte[] bytes = decodeAddress(bech32Address);
55+
hexAddress = Numeric.toHexString(bytes);
56+
} catch (Exception e) {
57+
throw new RuntimeException(e);
58+
}
59+
return Keys.toChecksumAddress(hexAddress);
60+
}
61+
62+
public static String convertAddressFromValToBech32(String prefix, String valAddress){
63+
String bech32Address = null;
64+
try {
65+
byte[] bytes = Bech32.decode(valAddress).getData();
66+
bech32Address = Bech32.encode(prefix, bytes);
67+
} catch (Exception e) {
68+
throw new RuntimeException(e);
69+
}
70+
return bech32Address;
71+
}
72+
73+
public static String convertAddressFromBech32ToVal(String prefix, String bech32Address){
74+
String valAddress = null;
75+
try {
76+
byte[] bytes = Bech32.decode(bech32Address).getData();
77+
valAddress = Bech32.encode(prefix, bytes);
78+
} catch (Exception e) {
79+
throw new RuntimeException(e);
80+
}
81+
return valAddress;
82+
}
83+
84+
85+
private static byte[] decodeAddress(String address){
86+
byte[] dec = Bech32.decode(address).getData();
87+
return ConvertBits.convertBits(dec, 0, dec.length, 5, 8, false);
88+
}
89+
90+
91+
private static byte[] encode(int witnessVersion, byte[] witnessProgram) throws AddressFormatException {
92+
byte[] convertedProgram = ConvertBits.convertBits(witnessProgram, 0, witnessProgram.length, 8, 5, true);
93+
return convertedProgram;
94+
}
95+
96+
97+
}

src/test/java/crypto/CryptoTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package crypto;
22

33
import com.okexchain.env.EnvInstance;
4-
import com.okexchain.utils.crypto.Crypto;
4+
import com.okexchain.utils.crypto.AddressConvertUtil;
55
import com.okexchain.utils.crypto.AddressUtil;
6+
import com.okexchain.utils.crypto.Crypto;
67
import org.bouncycastle.util.encoders.Hex;
78
import org.junit.Assert;
89
import org.junit.Test;
@@ -84,11 +85,16 @@ public void generatePrivateKeyFromMnemonic() {
8485
}
8586

8687
@Test
87-
public void testPubKey() {
88-
String pub = "02061a2f5b59ede92abf079084aa231bcb031d47cddb7a895af880b130c85af614";
89-
String pubBech = Crypto.generateBechPub(pub);
90-
String expectedPubBech = "okexchainpub1addwnpepqgrp5t6mt8k7j24lq7ggf23rr09sx828ehdh4z26lzqtzvxgttmpgrfngzd";
91-
Assert.assertEquals(expectedPubBech, pubBech);
88+
public void prefixTest() {
89+
//"eth_address": "0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a"
90+
//"privateKey": "2843de7dec93946f1022ec9355678fdec3dc49d3140d2314b452a3a4afe78191"
91+
Assert.assertEquals(AddressConvertUtil.convertFromBech32ToHex("okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56"),"0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a");
92+
Assert.assertEquals(AddressConvertUtil.convertFromBech32ToHex("ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk"),"0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a");
93+
Assert.assertEquals(AddressConvertUtil.convertFromHexToOkexchainBech32("0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a"),"okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56");
94+
Assert.assertEquals(AddressConvertUtil.convertFromHexToExBech32("0x64fAB0187AF0BCfF8499079161d8a0D68Ee8827a"),"ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk");
95+
Assert.assertEquals(AddressConvertUtil.convertFromExBech32ToOkexchainBech32("ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk"),"okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56");
96+
Assert.assertEquals(AddressConvertUtil.convertFromOkexchainBech32ToExBech32("okexchain1vnatqxr67z70lpyeq7gkrk9q668w3qn6sufu56"),"ex1vnatqxr67z70lpyeq7gkrk9q668w3qn6hhzuhk");
97+
9298
}
9399

94100
}

0 commit comments

Comments
 (0)