use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class AccountTool method createAccount.
public static Account createAccount(String prikey) throws NulsException {
ECKey key = null;
if (StringUtils.isBlank(prikey)) {
key = new ECKey();
} else {
try {
key = ECKey.fromPrivate(new BigInteger(1, Hex.decode(prikey)));
} catch (Exception e) {
throw new NulsException(AccountErrorCode.PRIVATE_KEY_WRONG, e);
}
}
Address address = new Address(NulsContext.getInstance().getDefaultChainId(), NulsContext.DEFAULT_ADDRESS_TYPE, SerializeUtils.sha256hash160(key.getPubKey()));
Account account = new Account();
account.setEncryptedPriKey(new byte[0]);
account.setAddress(address);
account.setPubKey(key.getPubKey());
account.setEcKey(key);
account.setPriKey(key.getPrivKeyBytes());
account.setCreateTime(TimeService.currentTimeMillis());
return account;
}
use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class ScriptTransactionTestTool method test1.
// @Test
public void test1() throws IOException {
NulsContext.MAIN_NET_VERSION = 2;
TransferTransaction tx = new TransferTransaction();
tx.setRemark("test script".getBytes());
CoinData data = new CoinData();
Coin coin = new Coin();
coin.setOwner(ArraysTool.concatenate(Hex.decode("0020dab71b3cd376e2ccf2f290e384d2917cc0929f8de582f63a01fc15144fe38371"), new byte[] { 0 }));
coin.setNa(Na.parseNuls(9997));
coin.setLockTime(0);
List<Coin> from = new ArrayList<>();
from.add(coin);
data.setFrom(from);
Coin toCoin = new Coin();
toCoin.setLockTime(0);
Script script = ScriptBuilder.createOutputScript(AddressTool.getAddress("NsdvuzHyQJEJkz4LEKweDeCs97845xN9"), 1);
toCoin.setOwner(script.getProgram());
toCoin.setNa(Na.parseNuls(9994));
List<Coin> to = new ArrayList<>();
to.add(toCoin);
data.setTo(to);
tx.setCoinData(data);
// ECKey ecKey = ECKey.fromPrivate(new BigInteger(1,Hex.decode("00b491621168dffd80c4684f7445ef378ba4d381b2fe2a7b1fbf905864ed8fbeb9")));
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("4b19caef601a45531b7068430a5b0e380a004001f14bfec025ddf16d5d87fa8e")));
List<ECKey> signEckeys = new ArrayList<>();
signEckeys.add(ecKey);
List<ECKey> scriptEckeys = new ArrayList<>();
SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
String param = "{\"txHex\": \"" + Hex.encode(tx.serialize()) + "\"}";
String res = post("http://127.0.0.1:7001/api/accountledger/transaction/valiTransaction", param, "utf-8");
System.out.println(res);
res = post("http://127.0.0.1:7001/api/accountledger/transaction/broadcast", param, "utf-8");
System.out.println(res);
}
use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class ScriptTransactionTestTool method test.
// @Test
public void test() throws Exception {
NulsContext.MAIN_NET_VERSION = 2;
TransferTransaction tx = new TransferTransaction();
tx.setRemark("test script".getBytes());
CoinData data = new CoinData();
Coin coin = new Coin();
coin.setOwner(ArraysTool.concatenate(Hex.decode("0020dab71b3cd376e2ccf2f290e384d2917cc0929f8de582f63a01fc15144fe38371"), new byte[] { 0 }));
coin.setNa(Na.parseNuls(9997));
coin.setLockTime(0);
List<Coin> from = new ArrayList<>();
from.add(coin);
data.setFrom(from);
Coin toCoin = new Coin();
toCoin.setLockTime(0);
Script script = ScriptBuilder.createOutputScript(AddressTool.getAddress("NsdvuzHyQJEJkz4LEKweDeCs97845xN9"), 1);
toCoin.setOwner(script.getProgram());
toCoin.setNa(Na.parseNuls(9994));
List<Coin> to = new ArrayList<>();
to.add(toCoin);
data.setTo(to);
tx.setCoinData(data);
// ECKey ecKey = ECKey.fromPrivate(new BigInteger(1,Hex.decode("00b491621168dffd80c4684f7445ef378ba4d381b2fe2a7b1fbf905864ed8fbeb9")));
ECKey ecKey = ECKey.fromPrivate(new BigInteger(1, Hex.decode("4b19caef601a45531b7068430a5b0e380a004001f14bfec025ddf16d5d87fa8e")));
List<ECKey> signEckeys = new ArrayList<>();
signEckeys.add(ecKey);
List<ECKey> scriptEckeys = new ArrayList<>();
SignatureUtil.createTransactionSignture(tx, scriptEckeys, signEckeys);
String param = "{\"txHex\": \"" + Hex.encode(tx.serialize()) + "\"}";
String res = post("http://127.0.0.1:7001/api/accountledger/transaction/valiTransaction", param, "utf-8");
System.out.println(res);
res = post("http://127.0.0.1:7001/api/accountledger/transaction/broadcast", param, "utf-8");
System.out.println(res);
}
use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class Account method getEcKey.
/**
* 根据密码获取ECKey
*/
public ECKey getEcKey(String password) throws NulsException {
ECKey eckey = null;
byte[] unencryptedPrivateKey;
// 判断当前账户是否存在私钥,如果不存在私钥这为锁定账户
BigInteger newPriv = null;
if (this.isLocked()) {
AssertUtil.canNotEmpty(password, "the password can not be empty");
if (!validatePassword(password)) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
try {
unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
newPriv = new BigInteger(1, unencryptedPrivateKey);
} catch (CryptoException e) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
} else {
newPriv = new BigInteger(1, this.getPriKey());
}
eckey = ECKey.fromPrivate(newPriv);
if (!Arrays.equals(eckey.getPubKey(), getPubKey())) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
return eckey;
}
use of io.nuls.core.tools.crypto.ECKey in project nuls by nuls-io.
the class Account method decrypt.
/**
* 根据解密账户, 包括生成账户明文私钥
* According to the decryption account, including generating the account plaintext private key
*/
private boolean decrypt(String password) throws NulsException {
try {
byte[] unencryptedPrivateKey = AESEncrypt.decrypt(this.getEncryptedPriKey(), password);
BigInteger newPriv = new BigInteger(1, unencryptedPrivateKey);
ECKey key = ECKey.fromPrivate(newPriv);
if (!Arrays.equals(key.getPubKey(), getPubKey())) {
return false;
}
key.setEncryptedPrivateKey(new EncryptedData(this.getEncryptedPriKey()));
this.setPriKey(key.getPrivKeyBytes());
this.setEcKey(key);
} catch (Exception e) {
throw new NulsException(AccountErrorCode.PASSWORD_IS_WRONG);
}
return true;
}
Aggregations