Skip to content

Commit

Permalink
Merge pull request #62 from jpeng-go/master
Browse files Browse the repository at this point in the history
账户接口添加导入导出秘钥文件
  • Loading branch information
andyYuanFZM authored Jan 31, 2021
2 parents ad0b80c + 4cececc commit 0a00c94
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 8 deletions.
75 changes: 74 additions & 1 deletion src/main/java/cn/chain33/javasdk/client/Account.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package cn.chain33.javasdk.client;

import cn.chain33.javasdk.model.AccountInfo;
import cn.chain33.javasdk.utils.DesUtil;
import cn.chain33.javasdk.utils.HexUtil;
import cn.chain33.javasdk.utils.StringUtil;
import cn.chain33.javasdk.utils.TransactionUtil;

import java.io.*;

public class Account {

/**
*
* @description 在本地创建账户信息
Expand All @@ -27,4 +31,73 @@ public AccountInfo newAccountLocal() {
return accountInfo;
}

/**
* 本地创建账户信息,加密输出到指定路径
*/
public AccountInfo newAccountLocal(String name, String passwd, String path) throws IOException {
AccountInfo accountInfo = new AccountInfo();
accountInfo.setName(name);

// 生成私钥匙
String privateKey = TransactionUtil.generatorPrivateKeyString();
accountInfo.setPrivateKey(privateKey);

// 导出到文件
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
if(StringUtil.isEmpty(passwd)){
fos.write(privateKey.getBytes());
} else {
fos.write(DesUtil.encrypt(privateKey.getBytes(), DesUtil.padding(passwd)));
}
fos.close();

// 生成公钥匙
String publicKey = TransactionUtil.getHexPubKeyFromPrivKey(privateKey);
accountInfo.setPublicKey(publicKey);
byte[] publicKeyByte = HexUtil.fromHexString(publicKey);
// 生成地址
accountInfo.setAddress(TransactionUtil.genAddress(publicKeyByte));
return accountInfo;
}

/**
* 导出本地账户信息
*/
public AccountInfo loadAccountLocal(String name, String passwd, String path) throws Exception {
File file = new File(path);
if (!file.exists()) {
return null;
}

FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte buffer[] = new byte[64];
int size;
while ((size = fis.read(buffer)) != -1) {
baos.write(buffer, 0, size);
}
fis.close();

String privateKey;
if(StringUtil.isEmpty(passwd)) {
privateKey = baos.toString();
} else{
privateKey = new String(DesUtil.decrypt(baos.toByteArray(), DesUtil.padding(passwd)));
}
AccountInfo accountInfo = new AccountInfo();
accountInfo.setName(name);
accountInfo.setPrivateKey(privateKey);
// 生成公钥匙
String publicKey = TransactionUtil.getHexPubKeyFromPrivKey(privateKey);
accountInfo.setPublicKey(publicKey);
byte[] publicKeyByte = HexUtil.fromHexString(publicKey);
// 生成地址
accountInfo.setAddress(TransactionUtil.genAddress(publicKeyByte));
return accountInfo;
}
}
11 changes: 10 additions & 1 deletion src/main/java/cn/chain33/javasdk/model/AccountInfo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package cn.chain33.javasdk.model;

public class AccountInfo {

private String name;

private String privateKey;

private String publicKey;

private String address;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPrivateKey() {
return privateKey;
}
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/cn/chain33/javasdk/utils/DesUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import javax.crypto.spec.DESKeySpec;

public class DesUtil {


private final static int BLOCKSIZE = 8;

/**
*
* @description encrypt
Expand All @@ -34,7 +36,6 @@ public static byte[] encrypt(byte[] content, String password) {
/**
*
* @description decrypt
* @param src
* @param password
* @return
*/
Expand All @@ -48,6 +49,26 @@ public static byte[] decrypt(byte[] contentEncrypt, String password) throws Exce
return cipher.doFinal(contentEncrypt);
}

/**
* 8字节填充
*/
public static String padding(String password) {
int length = password.length();

//计算需填充长度
if (length % BLOCKSIZE == 0) {
return password;
}

length += BLOCKSIZE - (length % BLOCKSIZE);
byte[] plaintext = new byte[length];

//填充
System.arraycopy(password.getBytes(), 0, plaintext, 0, password.length());

return new String(plaintext);
}

// 测试
public static void main(String args[]) {
// 待加密内容
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/cn/chain33/javasdk/model/AccountTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cn.chain33.javasdk.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.chain33.javasdk.client.RpcClient;
import org.junit.Assert;
import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
Expand Down Expand Up @@ -277,4 +279,19 @@ private void manageAccount(String[] accountIds, String op, String level) throws
// 账户地址
System.out.println("地址:" + resultJson.getString("addr"));
}

@Test
public void testAccountStore() throws Exception {
AccountInfo accountInfo = account.newAccountLocal("testa", "12345678", "testa");
System.out.println("name is:" + accountInfo.getName());
System.out.println("privateKey is:" + accountInfo.getPrivateKey());
System.out.println("publicKey is:" + accountInfo.getPublicKey());
System.out.println("Address is:" + accountInfo.getAddress());

AccountInfo accountInfo1 = account.loadAccountLocal("testa", "12345678", "testa");
Assert.assertEquals(accountInfo.getName(), accountInfo1.getName());
Assert.assertEquals(accountInfo.getPrivateKey(), accountInfo1.getPrivateKey());
Assert.assertEquals(accountInfo.getPublicKey(), accountInfo1.getPublicKey());
Assert.assertEquals(accountInfo.getAddress(), accountInfo1.getAddress());
}
}
3 changes: 3 additions & 0 deletions src/test/java/cn/chain33/javasdk/model/GMTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.chain33.javasdk.model;

import cn.chain33.javasdk.model.gm.*;
import cn.chain33.javasdk.utils.HexUtil;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -24,6 +25,8 @@ public void testSM2SignAndVerify() {
if (!flag) {
Assert.fail("verify failed");
}

System.out.println(HexUtil.toHexString(keyPair.getPublicKey().getEncoded(true)));
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class Performance {

/**
*
* @param args
*/
public void runTest(String ip, String port, String num) {

Expand Down Expand Up @@ -64,8 +63,6 @@ private void startthread1(String privateKey, int num, RpcClient client, String e
* 获取区块高度
*
* @param client
* @param privateKey
* @param toaddress
*/
private void startthread2(RpcClient client) {
Thread2 st = new Thread2(client);
Expand Down Expand Up @@ -162,7 +159,7 @@ public void run() {
try {
txHeight = client.getLastHeader().getHeight();
Thread.sleep(1000);
} catch (InterruptedException e) {
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Expand Down

0 comments on commit 0a00c94

Please sign in to comment.