Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation fileTree(include: ['*.jar', "*.dylib", "*.so"], dir: 'libs')
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation group: 'org.json', name: 'json', version: '20200518'
implementation 'one.block:eosiojava:0.1.2'
implementation 'one.block:eosiojava:0.1.3-eosio2.1-amqp'
implementation 'org.jetbrains:annotations:17.0.0'
}

Expand Down Expand Up @@ -71,7 +71,7 @@ clean.dependsOn 'gitSubmodulesUpdate'

def libraryGroupId = 'one.block'
def libraryArtifactId = 'eosio-java-abieos-serialization-provider'
def libraryVersion = '0.1.2'
def libraryVersion = '0.1.3-eosio2.1-amqp'

task sourcesJar(type: Jar, dependsOn: classes){
classifier = 'sources'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,36 @@ private static Map<String, String> initAbiEosJsonMap() {
" }\n" +
" ]\n" +
"}");

jsonMap.put("packed.transaction.abi.json", "{\n" +
" \"version\": \"eosio::abi/1.0\",\n" +
" \"structs\": [\n" +
" {\n" +
" \"name\": \"packed_transaction_v0\",\n" +
" \"base\": \"\",\n" +
" \"fields\": [\n" +
" {\n" +
" \"name\": \"signatures\",\n" +
" \"type\": \"signature[]\"\n" +
" },\n" +
" {\n" +
" \"name\": \"compression\",\n" +
" \"type\": \"uint8\"\n" +
" },\n" +
" {\n" +
" \"name\": \"packed_context_free_data\",\n" +
" \"type\": \"bytes\"\n" +
" },\n" +
" {\n" +
" \"name\": \"packed_trx\",\n" +
" \"type\": \"bytes\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}");

return Collections.unmodifiableMap(jsonMap);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,28 @@ public String serializeAbi(String json) throws SerializeAbiError {
}
}

/**
* Convenience method to transform a serialized transaction (v0) JSON string to a hex string.
*
* @param json - JSON string representing the serialized transaction (v0) to serialize.
* @return - Serialized hex string representing the transaction JSON.
* @throws SerializePackedTransactionError - A serialization error is thrown if there are any exceptions during the
* * conversion process.
*/
@NotNull
public String serializePackedTransaction(String json) throws SerializePackedTransactionError {
try {
String abi = getAbiJsonString("packed.transaction.abi.json");
AbiEosSerializationObject serializationObject = new AbiEosSerializationObject(null,
"", "packed_transaction_v0", abi);
serializationObject.setJson(json);
serialize(serializationObject);
return serializationObject.getHex();
} catch (SerializationProviderError serializationProviderError) {
throw new SerializePackedTransactionError(serializationProviderError);
}
}

/**
* Perform a deserialization process to convert a hex string to a JSON string given the parameters
* provided in the input deserilizationObject. The result will be placed in the json field of
Expand Down Expand Up @@ -323,7 +345,6 @@ public String deserializeTransaction(String hex) throws DeserializeTransactionEr
@NotNull
public String deserializeAbi(String hex) throws DeserializeAbiError {
try {
refreshContext();
byte[] hexData = Hex.decode(hex);
String jsonStr = abiBinToJson(context, hexData);
if(jsonStr == null) {
Expand All @@ -340,6 +361,29 @@ public String deserializeAbi(String hex) throws DeserializeAbiError {
}
}

/**
* Convenience method to transform a packed transaction (v0) hex string to a JSON string.
*
* @param hex - Hex string representing the packed transaction (v0) to deserialize.
* @return - Deserialized JSON string representing the transaction hex.
* @throws DeserializePackedTransactionError - A deserialization error is thrown if there are any exceptions during the
* * conversion process.
*/
@NotNull
public String deserializePackedTransaction(String hex) throws DeserializePackedTransactionError {
try {
refreshContext();
String abi = getAbiJsonString("packed.transaction.abi.json");
AbiEosSerializationObject serializationObject = new AbiEosSerializationObject(null,
"", "packed_transaction_v0", abi);
serializationObject.setHex(hex);
deserialize(serializationObject);
return serializationObject.getJson();
} catch (SerializationProviderError serializationProviderError) {
throw new DeserializePackedTransactionError(serializationProviderError);
}
}

/**
* Reset the underlying C++ context by destroying and recreating it. This allows multiple
* conversions to be done using the same AbiEosSerializationProviderImpl instance.
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/AbieosTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,40 @@ public void testQueryItAbiDeserialize() {
assertEquals(json, jsonResult);
}

@Test
public void jsonToHexPackedTransaction() {
String json = "{\"signatures\":[\"SIG_K1_K5PGhrkUBkThs8zdTD9mGUJZvxL4eU46UjfYJSEdZ9PXS2Cgv5jAk57yTx4xnrdSocQm6DDvTaEJZi5WLBsoZC4XYNS8b3\"],\"compression\":0,\"packed_context_free_data\":\"\",\"packed_trx\":\"D3029649D2042E160000000000000100A6823403EA3055000000572D3CCDCD01608C31C6187315D600000000A8ED323221608C31C6187315D6708C31C6187315D6010000000000000004535953000000000000\"}";
String hexResult = "01001F4D6C791D32E38CA1A0A5F3139B8D1D521B641FE2EE675311FCA4C755ACDFCA2D13FE4DEE9953D2504FCB4382EEACBCEF90E3E8034BDD32EBA11F1904419DF6AF000053D3029649D2042E160000000000000100A6823403EA3055000000572D3CCDCD01608C31C6187315D600000000A8ED323221608C31C6187315D6708C31C6187315D6010000000000000004535953000000000000";

String hex = null;

try {
hex = abieos.serializePackedTransaction(json);
} catch (SerializePackedTransactionError err) {
err.printStackTrace();
}

assertNotNull(hex);
assertEquals(hex, hexResult);
}

@Test
public void hexToJsonAbiPackedTransaction() {
String hex = "01001F4D6C791D32E38CA1A0A5F3139B8D1D521B641FE2EE675311FCA4C755ACDFCA2D13FE4DEE9953D2504FCB4382EEACBCEF90E3E8034BDD32EBA11F1904419DF6AF000053D3029649D2042E160000000000000100A6823403EA3055000000572D3CCDCD01608C31C6187315D600000000A8ED323221608C31C6187315D6708C31C6187315D6010000000000000004535953000000000000";
String jsonResult = "{\"signatures\":[\"SIG_K1_K5PGhrkUBkThs8zdTD9mGUJZvxL4eU46UjfYJSEdZ9PXS2Cgv5jAk57yTx4xnrdSocQm6DDvTaEJZi5WLBsoZC4XYNS8b3\"],\"compression\":0,\"packed_context_free_data\":\"\",\"packed_trx\":\"D3029649D2042E160000000000000100A6823403EA3055000000572D3CCDCD01608C31C6187315D600000000A8ED323221608C31C6187315D6708C31C6187315D6010000000000000004535953000000000000\"}";

String json = null;

try {
json = abieos.deserializePackedTransaction(hex);
} catch (DeserializePackedTransactionError err) {
err.printStackTrace();
}

assertNotNull(json);
assertEquals(json, jsonResult);
}

// Direct comparison of the JSON strings is fragile. This compares the expected JSON to a result by checking
// that its keys are a sub-set of the results keys since abieos can add keys for empty values. Values are not
// included since they can be anything and are not comparable. If abieos gets the keys right, that is
Expand Down