Skip to content

Commit fc34114

Browse files
author
JK
committed
Added signrawtranction command for private keys
1 parent 0e0527e commit fc34114

5 files changed

Lines changed: 87 additions & 6 deletions

File tree

src/main/java/multichain/command/RAWTransactionCommand.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import java.util.List;
1111

1212
import multichain.command.builders.QueryBuilderRAWTransaction;
13+
import multichain.command.tools.MultichainTestParameter;
1314
import multichain.object.Address;
1415
import multichain.object.AddressBalanceAsset;
16+
import multichain.object.SignRawTransactionOut;
1517
import multichain.object.TransactionRAW;
1618
import multichain.object.formatters.RAWTransactionFormatter;
1719
import multichain.object.queryobjects.TxIdVout;
@@ -163,17 +165,29 @@ public static String appendRawMetaData(String txHex, String dataHex) throws Mult
163165
* Result:
164166
* "transaction" (string) hex string of the transaction
165167
*
166-
* @param txids
167-
* @param vouts
168-
* @param addresses
169-
* @param amounts
170168
* @return
171169
* @throws MultichainException
172170
*/
173171
public static String createRawTransaction(List<TxIdVout> inputs, List<AddressBalanceAsset> addessAssets) throws MultichainException {
174172
return executeCreateRawTransaction(inputs, addessAssets);
175173
}
176174

175+
/**
176+
* Create a raw transaction from specified address
177+
* @param blockchainAddress the from address
178+
* @param asset if any asset is being transferred, the raw form i.e. '{"1...adfafdsaf":{"asset0":2000}}'
179+
* @param streamItem if publishing to a stream, the raw form i.e.
180+
* '[{"for":"stream0","key":"key0","data":"45787465726e616c20697320736166657374"}]'
181+
* @return hexidecimal blob as String
182+
* @throws MultichainException
183+
*/
184+
public static String createRawSendFrom(String blockchainAddress, String asset, String streamItem) throws MultichainException {
185+
if(asset == null || asset.isEmpty()) {
186+
asset = "'{}'";
187+
}
188+
return executeCreateRawSendFrom(blockchainAddress, asset, streamItem);
189+
}
190+
177191
/**
178192
*
179193
* decoderawtransaction "hexstring"
@@ -420,4 +434,16 @@ public static String sendRawTransaction(String hexString) throws MultichainExcep
420434
public static String signRawTransaction(String hexString) throws MultichainException {
421435
return executeSignRawTransaction(hexString);
422436
}
437+
438+
/**
439+
* Sign the transaction hex string using specified private key to get bigger hexadecimal blob output
440+
* @param hexString the hex string to sign
441+
* @param privKey the key with which to sign
442+
* @return SignRawTransactionOut object
443+
* @throws MultichainException
444+
*/
445+
public static SignRawTransactionOut signRawTransactionWithPrivKey(String hexString, String privKey) throws MultichainException {
446+
String hexOut = executeSignRawTransactionWithPrivKey(hexString, privKey);
447+
return RAWTransactionFormatter.formatSignTransactionOut(hexOut);
448+
}
423449
}

src/main/java/multichain/command/builders/QueryBuilderCommon.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected enum CommandEnum {
4242
CREATEKEYPAIRS,
4343
CREATEMULTISIG,
4444
CREATERAWEXCHANGE,
45+
CREATERAWSENDFROM,
4546
CREATERAWTRANSACTION,
4647
DECODERAWEXCHANGE,
4748
DECODERAWTRANSACTION,
@@ -112,7 +113,7 @@ protected enum CommandEnum {
112113
SENDWITHMETADATAFROM,
113114
SETLASTBLOCK,
114115
SIGNMESSAGE,
115-
SIGNTAWTRANSACTION,
116+
SIGNRAWTRANSACTION,
116117
STOP,
117118
SUBSCRIBE,
118119
UNSUBSCRIBE,

src/main/java/multichain/command/builders/QueryBuilderRAWTransaction.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ protected static String executeCreateRawTransaction(List<TxIdVout> inputs, List<
184184
return execute(CommandEnum.CREATERAWTRANSACTION, formatJson(inputs), formatJson(addessAssets));
185185
}
186186

187+
/**
188+
* Create a raw transaction from specified address
189+
* @param blockchainAddress the from address
190+
* @param asset if any asset is being transferred, the raw form i.e. '{"1...adfafdsaf":{"asset0":2000}}'
191+
* @param streamItem if publishing to a stream, the raw form i.e.
192+
* '[{"for":"stream0","key":"key0","data":"45787465726e616c20697320736166657374"}]'
193+
* @return hexidecimal blob as String
194+
* @throws MultichainException
195+
*/
196+
protected static String executeCreateRawSendFrom(String blockchainAddress, String asset, String streamItem) throws MultichainException {
197+
return execute(CommandEnum.CREATERAWSENDFROM, blockchainAddress, asset, streamItem);
198+
}
199+
187200
/**
188201
*
189202
* decoderawtransaction "hexstring"
@@ -402,7 +415,20 @@ protected static String executeSendRawTransaction(String hexString) throws Multi
402415
*/
403416
protected static String executeSignRawTransaction(String hexString) throws MultichainException{
404417
MultichainTestParameter.isNotNullOrEmpty("hexString", hexString);
405-
return execute(CommandEnum.SIGNTAWTRANSACTION, formatJson(hexString));
418+
return execute(CommandEnum.SIGNRAWTRANSACTION, formatJson(hexString));
419+
}
420+
421+
/**
422+
* Sign the transaction hex string using specified private key to get bigger hexadecimal blob output
423+
* @param hexString the hex string to sign
424+
* @param privKey the key with which to sign
425+
* @return String containing bigger blob in {"hex": "adfadf", "complete": true} format
426+
* @throws MultichainException
427+
*/
428+
protected static String executeSignRawTransactionWithPrivKey(String hexString, String privKey) throws MultichainException {
429+
MultichainTestParameter.isNotNullOrEmpty("hexString", hexString);
430+
MultichainTestParameter.isNotNullOrEmpty("privKey", privKey);
431+
return execute(CommandEnum.SIGNRAWTRANSACTION, formatJson(hexString),"[]" ,formatJson(privKey));
406432
}
407433

408434
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package multichain.object;
2+
3+
public class SignRawTransactionOut {
4+
String hex;
5+
boolean complete;
6+
7+
public String getHex() {
8+
return hex;
9+
}
10+
11+
public void setHex(String hex) {
12+
this.hex = hex;
13+
}
14+
15+
public boolean isComplete() {
16+
return complete;
17+
}
18+
19+
public void setComplete(boolean complete) {
20+
this.complete = complete;
21+
}
22+
}

src/main/java/multichain/object/formatters/RAWTransactionFormatter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package multichain.object.formatters;
99

10+
import multichain.object.SignRawTransactionOut;
1011
import multichain.object.TransactionRAW;
1112

1213
import com.google.gson.Gson;
@@ -24,4 +25,9 @@ public final static TransactionRAW formatTransactionRAW(String stringTransaction
2425

2526
return transactionRAW;
2627
}
28+
29+
public final static SignRawTransactionOut formatSignTransactionOut(String hexString) {
30+
final Gson gson = new GsonBuilder().create();
31+
return gson.fromJson(hexString, SignRawTransactionOut.class);
32+
}
2733
}

0 commit comments

Comments
 (0)