|
| 1 | +#include "transaction.h" |
| 2 | + |
| 3 | +/* |
| 4 | + * Takes a json string, hashes it sha3_256 and signs it with ed25519. |
| 5 | + */ |
| 6 | +void bigchain_sign_transaction(uint8_t *json_tx, uint16_t len, uint8_t *priv_key, uint8_t *pub_key, uint8_t *sig) |
| 7 | +{ |
| 8 | + uint8_t hash[32] = {0}; |
| 9 | + sha3_256((const unsigned char*)json_tx, len, hash); |
| 10 | + ed25519_sign(hash, 32, priv_key, pub_key, sig); |
| 11 | +} |
| 12 | + |
| 13 | +char* bigchain_build_json_outputs(BIGCHAIN_OUTPUT *outputs, uint8_t num_outputs, char *json_obj) |
| 14 | +{ |
| 15 | + char *p = json_obj; |
| 16 | + p = json_arrOpen( p, "outputs" ); |
| 17 | + for(uint8_t i = 0; i < num_outputs; i++) |
| 18 | + { |
| 19 | + p = json_objOpen(p, NULL); |
| 20 | + p = json_str(p, "amount", outputs[i].amount); |
| 21 | + p = json_objOpen(p, "condition"); |
| 22 | + |
| 23 | + p = json_objOpen(p, "details"); |
| 24 | + p = json_str(p, "public_key", outputs[i].details_public_key); |
| 25 | + p = json_str(p, "type", "ed25519-sha-256"); |
| 26 | + p = json_objClose(p); |
| 27 | + |
| 28 | + p = json_str(p, "uri", "ni:///sha-256;Vta3W592Kt_Y2ljfHEDZLd4OCZPHLiHyCgjNNKrrNwo?fpt=ed25519-sha-256&cost=131072"); |
| 29 | + p = json_objClose(p); |
| 30 | + |
| 31 | + p = json_arrOpen(p, "public_keys"); |
| 32 | + for(uint8_t j = 0; j < outputs[i].num_public_keys; j++ ) |
| 33 | + { |
| 34 | + p = json_str(p, NULL, outputs[i].public_keys[j]); |
| 35 | + } |
| 36 | + p = json_arrClose(p); |
| 37 | + |
| 38 | + p = json_objClose(p); |
| 39 | + } |
| 40 | + p = json_arrClose(p); |
| 41 | + return p; |
| 42 | +} |
| 43 | + |
| 44 | +char* bigchain_build_json_inputs(BIGCHAIN_INPUT *inputs, uint8_t num_inputs, char *json_obj) |
| 45 | +{ |
| 46 | + char *p = json_obj; |
| 47 | + p = json_arrOpen( p, "inputs" ); |
| 48 | + for(uint8_t i = 0; i < num_inputs; i++) |
| 49 | + { |
| 50 | + p = json_objOpen(p, NULL); |
| 51 | + if(inputs[i].fulfillment[0] != '\0') |
| 52 | + { |
| 53 | + p = json_str(p, "fulfillment", inputs[0].fulfillment); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + p = json_null(p, "fulfillment"); |
| 58 | + } |
| 59 | + |
| 60 | + if(inputs[i].fulfills[0] != '\0') |
| 61 | + { |
| 62 | + p = json_str(p, "fulfills", inputs[0].fulfills); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + p = json_null(p, "fulfills"); |
| 67 | + } |
| 68 | + |
| 69 | + p = json_arrOpen(p, "owners_before"); |
| 70 | + for (uint8_t j = 0; j < inputs[0].num_owners; j++) |
| 71 | + { |
| 72 | + p = json_str(p, NULL, inputs[0].owners_before[j]); |
| 73 | + } |
| 74 | + p = json_arrClose(p); |
| 75 | + p = json_objClose(p); |
| 76 | + } |
| 77 | + p = json_arrClose(p); |
| 78 | + return p; |
| 79 | +} |
| 80 | + |
| 81 | +void bigchain_build_json_tx(BIGCHAIN_TX *tx, char *json_tx) |
| 82 | +{ |
| 83 | + char *p = json_tx; |
| 84 | + p = json_objOpen(p, NULL); |
| 85 | + |
| 86 | + // ASSET |
| 87 | + p = json_objOpen(p, "asset"); |
| 88 | + p = atoa(p, tx->asset); |
| 89 | + p = json_objClose(p); |
| 90 | + |
| 91 | + // ID |
| 92 | + if(tx->id[0] != '\0') |
| 93 | + { |
| 94 | + p = json_str(p, "id", tx->id); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + p = json_null(p, "id"); |
| 99 | + } |
| 100 | + |
| 101 | + // INPUTS |
| 102 | + p = bigchain_build_json_inputs(tx->inputs, tx->num_inputs, p); |
| 103 | + |
| 104 | + // METADA |
| 105 | + p = json_objOpen(p, "metadata"); |
| 106 | + p = atoa(p, tx->metadata); |
| 107 | + p = json_objClose(p); |
| 108 | + |
| 109 | + // OPERATION |
| 110 | + p = json_str(p, "operation", tx->operation); |
| 111 | + |
| 112 | + // OUTPUTS |
| 113 | + p = bigchain_build_json_outputs(tx->outputs, tx->num_outputs, p); |
| 114 | + |
| 115 | + // VERSION |
| 116 | + p = json_str(p, "version", tx->version); |
| 117 | + p = json_objClose(p); |
| 118 | + |
| 119 | +} |
| 120 | + |
0 commit comments