diff --git a/src/main/java/org/tron/core/Wallet.java b/src/main/java/org/tron/core/Wallet.java index 8c177a2434e..fd822497ba1 100755 --- a/src/main/java/org/tron/core/Wallet.java +++ b/src/main/java/org/tron/core/Wallet.java @@ -69,6 +69,7 @@ import org.tron.protos.Protocol.Account; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.Transaction; +import org.tron.protos.Protocol.TransactionSign; @Slf4j @@ -293,6 +294,23 @@ public GrpcAPI.Return broadcastTransaction(Transaction signaturedTransaction) { } } + public TransactionCapsule getTransactionSign(TransactionSign transactionSign) { + byte[] privateKey = transactionSign.getPrivateKey().toByteArray(); + TransactionCapsule trx = new TransactionCapsule(transactionSign.getTransaction()); + trx.sign(privateKey); + return trx; + } + + public byte[] pass2Key(byte[] passPhrase){ + return Sha256Hash.hash(passPhrase); + } + + public byte[] createAdresss(byte[] passPhrase) { + byte[] privateKey = pass2Key(passPhrase); + ECKey ecKey = ECKey.fromPrivate(privateKey); + return ecKey.getAddress(); + } + public Block getNowBlock() { List blockList = dbManager.getBlockStore().getBlockByLatestNum(1); if (CollectionUtils.isEmpty(blockList)) { diff --git a/src/main/java/org/tron/core/capsule/TransactionCapsule.java b/src/main/java/org/tron/core/capsule/TransactionCapsule.java index 6ad69ad614d..3bf6ca21b33 100755 --- a/src/main/java/org/tron/core/capsule/TransactionCapsule.java +++ b/src/main/java/org/tron/core/capsule/TransactionCapsule.java @@ -221,11 +221,10 @@ public boolean checkBalance(byte[] address, byte[] to, long amount, long balance return true; } - @Deprecated public void sign(byte[] privateKey) { ECKey ecKey = ECKey.fromPrivate(privateKey); ECDSASignature signature = ecKey.sign(getRawHash().getBytes()); - ByteString sig = ByteString.copyFrom(signature.toBase64().getBytes()); + ByteString sig = ByteString.copyFrom(signature.toByteArray()); this.transaction = this.transaction.toBuilder().addSignature(sig).build(); } diff --git a/src/main/java/org/tron/core/services/RpcApiService.java b/src/main/java/org/tron/core/services/RpcApiService.java index 4f83011f7a7..8a55f71c3ea 100755 --- a/src/main/java/org/tron/core/services/RpcApiService.java +++ b/src/main/java/org/tron/core/services/RpcApiService.java @@ -26,17 +26,21 @@ import org.tron.api.GrpcAPI.BlockList; import org.tron.api.GrpcAPI.BlockReference; import org.tron.api.GrpcAPI.BytesMessage; +import org.tron.api.GrpcAPI.EasyTransferMessage; +import org.tron.api.GrpcAPI.EasyTransferResponse; import org.tron.api.GrpcAPI.EmptyMessage; import org.tron.api.GrpcAPI.Node; import org.tron.api.GrpcAPI.NodeList; import org.tron.api.GrpcAPI.NumberMessage; import org.tron.api.GrpcAPI.PaginatedMessage; +import org.tron.api.GrpcAPI.Return.response_code; import org.tron.api.GrpcAPI.TransactionList; import org.tron.api.GrpcAPI.WitnessList; import org.tron.api.WalletExtensionGrpc; import org.tron.api.WalletGrpc.WalletImplBase; import org.tron.api.WalletSolidityGrpc.WalletSolidityImplBase; import org.tron.common.application.Service; +import org.tron.common.crypto.ECKey; import org.tron.common.overlay.discover.node.NodeHandler; import org.tron.common.overlay.discover.node.NodeManager; import org.tron.common.utils.ByteArray; @@ -70,6 +74,7 @@ import org.tron.protos.Protocol.DynamicProperties; import org.tron.protos.Protocol.Transaction; import org.tron.protos.Protocol.Transaction.Contract.ContractType; +import org.tron.protos.Protocol.TransactionSign; @Component @Slf4j @@ -372,6 +377,58 @@ private TransactionCapsule createTransactionCapsule(com.google.protobuf.Message return trx; } + @Override + public void getTransactionSign(TransactionSign req, + StreamObserver responseObserver) { + TransactionCapsule retur = wallet.getTransactionSign(req); + responseObserver.onNext(retur.getInstance()); + responseObserver.onCompleted(); + } + + @Override + public void createAdresss(BytesMessage req, + StreamObserver responseObserver) { + byte[] address = wallet.createAdresss(req.getValue().toByteArray()); + BytesMessage.Builder builder = BytesMessage.newBuilder(); + builder.setValue(ByteString.copyFrom(address)); + responseObserver.onNext(builder.build()); + responseObserver.onCompleted(); + } + + @Override + public void easyTransfer(EasyTransferMessage req, + StreamObserver responseObserver) { + byte[] privateKey = wallet.pass2Key(req.getPassPhrase().toByteArray()); + ECKey ecKey = ECKey.fromPrivate(privateKey); + byte[] owner = ecKey.getAddress(); + TransferContract.Builder builder = TransferContract.newBuilder(); + builder.setOwnerAddress(ByteString.copyFrom(owner)); + builder.setToAddress(req.getToAddress()); + builder.setAmount(req.getAmount()); + + TransactionCapsule transactionCapsule = null; + GrpcAPI.Return.Builder returnBuilder = GrpcAPI.Return.newBuilder(); + EasyTransferResponse.Builder responseBuild = EasyTransferResponse.newBuilder(); + try { + transactionCapsule = createTransactionCapsule(builder.build(), + ContractType.TransferContract); + } catch (ContractValidateException e) { + returnBuilder.setResult(false).setCode(response_code.CONTRACT_VALIDATE_ERROR) + .setMessage(ByteString.copyFromUtf8(e.getMessage())); + responseBuild.setResult(returnBuilder.build()); + responseObserver.onNext(responseBuild.build()); + responseObserver.onCompleted(); + return; + } + + transactionCapsule.sign(privateKey); + GrpcAPI.Return retur = wallet.broadcastTransaction(transactionCapsule.getInstance()); + responseBuild.setTransaction(transactionCapsule.getInstance()); + responseBuild.setResult(retur); + responseObserver.onNext(responseBuild.build()); + responseObserver.onCompleted(); + } + @Override public void broadcastTransaction(Transaction req, StreamObserver responseObserver) { diff --git a/src/main/protos/api/api.proto b/src/main/protos/api/api.proto index ce4477a081c..dcfa7d31b94 100644 --- a/src/main/protos/api/api.proto +++ b/src/main/protos/api/api.proto @@ -310,6 +310,36 @@ service Wallet { } }; } + //Warning: do not invoke this interface provided by others. + rpc GetTransactionSign (TransactionSign) returns (Transaction) { + option (google.api.http) = { + post: "/wallet/gettransactionsign" + body: "*" + additional_bindings { + get: "/wallet/gettransactionsign" + } + }; + }; + //Warning: do not invoke this interface provided by others. + rpc CreateAdresss (BytesMessage) returns (BytesMessage) { + option (google.api.http) = { + post: "/wallet/createadresss" + body: "*" + additional_bindings { + get: "/wallet/createadresss" + } + }; + }; + //Warning: do not invoke this interface provided by others. + rpc EasyTransfer (EasyTransferMessage) returns (EasyTransferResponse) { + option (google.api.http) = { + post: "/wallet/easytransfer" + body: "*" + additional_bindings { + get: "/wallet/easytransfer" + } + }; + }; }; @@ -523,3 +553,14 @@ message PaginatedMessage { int64 offset = 1; int64 limit = 2; } + +message EasyTransferMessage{ + bytes passPhrase = 1; + bytes toAddress = 2; + int64 amount = 3; +} + +message EasyTransferResponse{ + Transaction transaction = 1; + Return result = 2; +} \ No newline at end of file diff --git a/src/main/protos/core/Tron.proto b/src/main/protos/core/Tron.proto index 87c83105eec..e804da178ea 100644 --- a/src/main/protos/core/Tron.proto +++ b/src/main/protos/core/Tron.proto @@ -194,6 +194,11 @@ message Transactions { repeated Transaction transactions = 1; } +message TransactionSign { + Transaction transaction = 1; + bytes privateKey = 2; +} + message BlockHeader { message raw { int64 timestamp = 1; diff --git a/src/main/resources/config-localtest.conf b/src/main/resources/config-localtest.conf index e677950b6cd..95f9f6a9546 100644 --- a/src/main/resources/config-localtest.conf +++ b/src/main/resources/config-localtest.conf @@ -109,6 +109,7 @@ node { # The maximum size of header list allowed to be received, default 8192 # maxHeaderListSize = } +} seed.node = { # List of the seed nodes @@ -120,8 +121,8 @@ seed.node = { # ] ip.list = [ "127.0.0.1:7777", - "127.0.0.1:8888", - "127.0.0.1:9999", + # "127.0.0.1:8888", + # "127.0.0.1:9999", ] } @@ -166,20 +167,20 @@ genesis.block = { voteCount = 105 #priKey = f5583fd20e13073900a513f333ed13db8c9e83e7e3cf37e74adacef96c5afeaa 7777 }, - { - address: TEZBh76rouEQpB2zqYVopbRXGx7RfyWorT - #address: 27TfVERREG3FeWMHEAQ95tWHG4sb3ANn3Qe - url = "http://Venus.org", - voteCount = 104 - #priKey = 9f5c5e48bf87cf92017313082e8cf0f58ccfce423097f0fcebf801695fc99bd4 8888 - }, - { - address: TN27wbfCLEN1gP2PZAxHgU3QZrntsLyxdj - #address: 27b8RUuyZnNPFNZGct2bZkNu9MnGWNAdH3Z - url = "http://Earth.org", - voteCount = 103 - #priKey = 6781f44d9a2083b14fad1702b8e9ba82749162b795e2fc3f136192fc63f80de2 9999 - }, + # { + # address: TEZBh76rouEQpB2zqYVopbRXGx7RfyWorT + # #address: 27TfVERREG3FeWMHEAQ95tWHG4sb3ANn3Qe + # url = "http://Venus.org", + # voteCount = 104 + # #priKey = 9f5c5e48bf87cf92017313082e8cf0f58ccfce423097f0fcebf801695fc99bd4 8888 + # }, + # { + # address: TN27wbfCLEN1gP2PZAxHgU3QZrntsLyxdj + # #address: 27b8RUuyZnNPFNZGct2bZkNu9MnGWNAdH3Z + # url = "http://Earth.org", + # voteCount = 103 + # #priKey = 6781f44d9a2083b14fad1702b8e9ba82749162b795e2fc3f136192fc63f80de2 9999 + # }, ] timestamp = "0" #2017-8-26 12:00:00 @@ -187,11 +188,15 @@ genesis.block = { parentHash = "0x0000000000000000000000000000000000000000000000000000000000000000" } -localwitnesskeystore = [ - "src/main/resources/localwitnesskeystore.json" +localwitness = [ + f5583fd20e13073900a513f333ed13db8c9e83e7e3cf37e74adacef96c5afeaa ] +#localwitnesskeystore = [ +# "src/main/resources/localwitnesskeystore.json" +#] + block = { - needSyncCheck = true # first node : false, other : true + needSyncCheck = false # first node : false, other : true maintenanceTimeInterval = 21600000 // 1 day: 86400000(ms), 6 hours: 21600000(ms) }