Search in sources :

Example 11 with ECKey

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;
}
Also used : Account(io.nuls.account.model.Account) Address(io.nuls.kernel.model.Address) NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) NulsException(io.nuls.kernel.exception.NulsException)

Example 12 with ECKey

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);
}
Also used : Coin(io.nuls.kernel.model.Coin) Script(io.nuls.kernel.script.Script) CoinData(io.nuls.kernel.model.CoinData) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Example 13 with ECKey

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);
}
Also used : Coin(io.nuls.kernel.model.Coin) Script(io.nuls.kernel.script.Script) CoinData(io.nuls.kernel.model.CoinData) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) TransferTransaction(io.nuls.protocol.model.tx.TransferTransaction)

Example 14 with ECKey

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;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException)

Example 15 with 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;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) BigInteger(java.math.BigInteger) ECKey(io.nuls.core.tools.crypto.ECKey) EncryptedData(io.nuls.core.tools.crypto.EncryptedData) CryptoException(io.nuls.core.tools.crypto.Exception.CryptoException) NulsException(io.nuls.kernel.exception.NulsException)

Aggregations

ECKey (io.nuls.core.tools.crypto.ECKey)43 NulsException (io.nuls.kernel.exception.NulsException)26 IOException (java.io.IOException)20 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)15 BigInteger (java.math.BigInteger)14 Account (io.nuls.account.model.Account)12 ArrayList (java.util.ArrayList)11 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)10 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 ValidateResult (io.nuls.kernel.validate.ValidateResult)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)6 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)6 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)4 MultipleAddressTransferModel (io.nuls.account.ledger.model.MultipleAddressTransferModel)3 Alias (io.nuls.account.model.Alias)3 AliasTransaction (io.nuls.account.tx.AliasTransaction)3 Agent (io.nuls.consensus.poc.protocol.entity.Agent)3 ContractResult (io.nuls.contract.dto.ContractResult)3 Script (io.nuls.kernel.script.Script)3