Skip to content

Commit

Permalink
Merge pull request #39 from zheng-bin/dev-github-sdk
Browse files Browse the repository at this point in the history
Dev GitHub sdk
  • Loading branch information
skyargos authored May 19, 2022
2 parents 0b0b2ce + 662c804 commit d2a99e2
Show file tree
Hide file tree
Showing 63 changed files with 4,854 additions and 890 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# IDE related
.vscode/
.idea/

.DS_Store

target/
pom.xml.tag
pom.xml.releaseBackup
Expand Down
19 changes: 13 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@
<scope>test</scope>
</dependency>



<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down Expand Up @@ -200,19 +198,20 @@
<version>${io.grpc.version}</version>
</dependency>


<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>

<!--国密依赖包-->
<!-- https://search.maven.org/artifact/org.bouncycastle/bcpkix-jdk15on/1.70/jar -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15to18</artifactId>
<version>1.69</version>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.56</version>
</dependency>


<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand All @@ -224,6 +223,14 @@
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>

<dependency>
<groupId>org.apache.fluo</groupId>
<artifactId>fluo-api</artifactId>
<version>1.2.0</version>
</dependency>


</dependencies>

<build>
Expand Down
223 changes: 124 additions & 99 deletions src/main/java/com/baidu/xuper/api/Account.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.baidu.xuper.api;

import com.baidu.xuper.crypto.Base58;
import com.baidu.xuper.crypto.ECKeyPair;
import com.baidu.xuper.crypto.Hash;
import com.baidu.xuper.crypto.Crypto;
import com.baidu.xuper.crypto.account.PrivatePubKey;
import com.baidu.xuper.crypto.xchain.sign.ECKeyPair;
import com.baidu.xuper.crypto.account.ECDSAAccount;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import java.io.*;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -31,149 +31,107 @@ private Account(ECKeyPair ecKeyPair, String address, String mnemonic) {
}

/**
* @param inputStream ./data/keys/private.key fileInputStream
* @return
* @throws Exception
*/
public static Account create(InputStream inputStream) {
Gson gson = new Gson();
privatePubKey json;
try {
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
json = gson.fromJson(reader, privatePubKey.class);
} catch (Exception e) {
throw new RuntimeException(e);
}

if (json.D == null) {
throw new RuntimeException("invalid private.key file");
}
return create(ECKeyPair.create(json.D));
}

/**
* @param keyPath the path to ./data/keys which contains private.key file
* @return
* @throws Exception
*/
public static Account create(String keyPath) {
try {
String privateKeyPath = Paths.get(keyPath, "private.key").toString();
File privateKeyFile=new File(privateKeyPath);
return create(new FileInputStream(privateKeyFile));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* @param keyPair the private and public key of account
* @return
*/
public static Account create(ECKeyPair keyPair) {
byte[] pubkey = keyPair.getPublicKey().getEncoded(false);
byte[] hash = Hash.ripeMD128(Hash.sha256(pubkey));
String address = Base58.encodeChecked(1, hash);
return new Account(keyPair, address);
}

/**
* Create a account using random private key
* create an account.
*
* @return
*/
public static Account create() {
return create(ECKeyPair.create());
}

/**
* @param strength 助记词强度。
* @param strength 助记词强度, 1弱(12个助记词),2中(18个助记词),3强(24个助记词)。
* @param language 助记词语言,1中文,2英文。
* @return Account 账户信息。
*/
public static Account create(int strength, int language) {
ECDSAAccount ecdsaAccount = new ECDSAAccount();
ecdsaAccount.createAccountWithMnemonic(strength, language);
Crypto cli = CryptoClient.getCryptoClient();
ECDSAAccount ecdsaAccount = cli.createNewAccountWithMnemonic(language, strength);
return new Account(ecdsaAccount.ecKeyPair, ecdsaAccount.address, ecdsaAccount.mnemonic);
}

/**
* @param mnemonic 助记词。
* retrieve account from mnemonic.
*
* @param mnemonic 助记词,例如:"玉 脸 驱 协 介 跨 尔 籍 杆 伏 愈 即"。
* @param language 助记词语言,1中文,2英文。
* @return Account 账户信息。
*/
public static Account retrieve(String mnemonic, int language) {
ECDSAAccount ecdsaAccount = new ECDSAAccount();
ecdsaAccount.createByMnemonic(mnemonic, language);
Crypto cli = CryptoClient.getCryptoClient();
ECDSAAccount ecdsaAccount = cli.retrieveAccountByMnemonic(mnemonic, language);
return new Account(ecdsaAccount.ecKeyPair, ecdsaAccount.address, ecdsaAccount.mnemonic);
}

/**
* @param path 保存的路径。
* @param passwd 密码。
* @param strength 助记词强度,1弱 12个助记词,2中 18个助记词,3强 24个助记词。
* @param strength 助记词强度, 1弱(12个助记词,2中18个助记词,3强24个助记词
* @param language 助记词语言,1中文,2英文。
* @return Account 账户信息。
*/
public static Account createAndSave(String path, String passwd, int strength, int language) {
ECDSAAccount ecdsaAccount = new ECDSAAccount();
ecdsaAccount.createAccountWithMnemonic(strength, language);
ecdsaAccount.saveToFile(path, passwd);
return new Account(ecdsaAccount.ecKeyPair, ecdsaAccount.address, ecdsaAccount.mnemonic);
}
public static Account createAndSaveToFile(String path, String passwd, Integer strength, Integer language) {
Crypto cli = CryptoClient.getCryptoClient();
ECDSAAccount ecdsaAccount = cli.createNewAccountWithMnemonic(language, strength);

/**
* @param path 文件路径。
* @param passwd 密码。
* @return Account 账户信息。
*/
public static Account getAccountFromFile(String path, String passwd) {
byte[] p = ECDSAAccount.getBinaryECDSAPrivateKey(path, passwd);
Gson gson = new Gson();
privatePubKey json = gson.fromJson(new String(p), privatePubKey.class);
if (json.D == null) {
throw new RuntimeException("invalid private.key file");
}
return create(ECKeyPair.create(json.D));
Common.mkdir(path);
cli.retrieveAccountByMnemonicAndSavePrivKey(path, language, ecdsaAccount.mnemonic, passwd);

return new Account(ecdsaAccount.ecKeyPair, ecdsaAccount.address, ecdsaAccount.mnemonic);
}

/**
* import account from plain files which are JSON encoded
*
* @param path 文件路径。
* The structure of path is like below:
* - keys
* |-- address
* |-- private.key
* |-- public.key
* |-- mnemonic
* @return 账户信息。
* @return Account 账户信息。
*/
public static Account getAccountFromPlainFile(String path) {
try {
byte[] address = Files.readAllBytes(Paths.get(path + "/address"));
byte[] pubKey = Files.readAllBytes(Paths.get(path + "/public.key"));
byte[] privKey = Files.readAllBytes(Paths.get(path + "/private.key"));
byte[] priKey = Files.readAllBytes(Paths.get(path + "/private.key"));

Gson gson = new Gson();
privatePubKey json = gson.fromJson(new String(privKey), privatePubKey.class);
if (json.D == null) {
PrivatePubKey privatePubKey = gson.fromJson(new String(priKey), PrivatePubKey.class);
if (privatePubKey.D == null) {
throw new RuntimeException("invalid private.key file");
}

Account a = create(ECKeyPair.create(json.D));
if (!a.getAKAddress().equals(new String(address))) {
Crypto cli = CryptoClient.getCryptoClient();
Account account = create(cli.getECKeyPairFromPrivateKey(privatePubKey.D));
if (!account.getAKAddress().equals(new String(address))) {
throw new RuntimeException("address and private key not match.");
}

if (!a.getKeyPair().getJSONPublicKey().equals(new String(pubKey))) {
if (!account.getKeyPair().getJSONPublicKey().equals(new String(pubKey))) {
throw new RuntimeException("public key and private key not match.");
}
return a;
return account;

} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* get an account from file and password.
*
* @param path 文件路径。
* @param password 密码。
* @return Account 账户信息。
*/
public static Account getAccountFromFile(String path, String password) {
Crypto cli = CryptoClient.getCryptoClient();
byte[] p = cli.getEcdsaPrivateKeyFromFileByPassword(path, password);

Gson gson = new Gson();
PrivatePubKey privatePubKeyJson = gson.fromJson(new String(p), PrivatePubKey.class);
if (privatePubKeyJson.D == null) {
throw new RuntimeException("invalid private.key file");
}
return create(cli.getECKeyPairFromPrivateKey(privatePubKeyJson.D));
}

/**
* @return 公私钥相关。
*/
Expand Down Expand Up @@ -216,11 +174,19 @@ public String getContractAccount() {
* @param name 合约账户。
*/
public void setContractAccount(String name) {
// todo name正则表达验证
this.contractAccount = name;
}

/**
* @return AuthRequireId。
* remove contract account from this account.
*/
public void RemoveContractAccount() {
this.contractAccount = "";
}

/**
* @return String
*/
public String getAuthRequireId() {
if (this.contractAccount != null) {
Expand All @@ -229,10 +195,69 @@ public String getAuthRequireId() {
return this.address;
}

class privatePubKey {
String CurvName;
BigInteger D;
BigInteger X;
BigInteger Y;
/**
* @return boolean
*/
public boolean HasContractAccount() {
return this.contractAccount != "";
}



/**
* @param keyPath the path to ./data/keys which contains private.key file
* @return
* @throws Exception
*/
public static Account create(String keyPath) {
try {
String privateKeyPath = Paths.get(keyPath, "private.key").toString();
File privateKeyFile = new File(privateKeyPath);
return create(new FileInputStream(privateKeyFile));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* @param inputStream ./data/keys/private.key fileInputStream
* @return
* @throws Exception
*/
public static Account create(InputStream inputStream) {
Gson gson = new Gson();
PrivatePubKey json;
Crypto cli = CryptoClient.getCryptoClient();
try {
JsonReader reader = new JsonReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
json = gson.fromJson(reader, PrivatePubKey.class);
} catch (Exception e) {
throw new RuntimeException(e);
}

if (json.D == null) {
throw new RuntimeException("invalid private.key file");
}
return create(cli.getECKeyPairFromPrivateKey(json.D));
}

/**
* @param keyPair the private and public key of account
* @return
*/
public static Account create(ECKeyPair keyPair) {
Crypto cli = CryptoClient.getCryptoClient();
String address = cli.getAddressFromPublicKey(keyPair.publicKey);
return new Account(keyPair, address);
}

/**
* Create a account using random private key
*
* @return
*/
public static Account create() {
Crypto cli = CryptoClient.getCryptoClient();
return create(cli.createECKeyPair());
}
}
Loading

0 comments on commit d2a99e2

Please sign in to comment.