Skip to content

Commit 0f4fa6a

Browse files
author
diogorac
committed
start serialization after signing
1 parent 9d3d3b4 commit 0f4fa6a

File tree

7 files changed

+176
-4
lines changed

7 files changed

+176
-4
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "libs/trezor-crypto"]
22
path = libs/trezor-crypto
33
url = git@github.com:trezor/trezor-crypto.git
4+
[submodule "libs/base64"]
5+
path = libs/base64
6+
url = https://github.com/rafagafe/base64

example.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import json
2+
3+
import base58
4+
import sha3
5+
from cryptoconditions import Ed25519Sha256
6+
7+
from bigchaindb_driver.crypto import generate_keypair
8+
from cryptoconditions import crypto
9+
from binascii import unhexlify
10+
import base58, base64
11+
from collections import namedtuple
12+
13+
a = "c1cab23edf6065679dcbfbcbd17300384f7586bef4b7bda56d0366f707a91063c552f4a3471b3afd7506cf7a52c59965b9cfaaaf7934e0304535fd1838d76708"
14+
b = base64.b64encode(unhexlify(a))
15+
print(b)
16+
17+
def get_pair():
18+
prv = base58.b58encode(unhexlify("2470319889fed2139a6dd99ea3aef2d396e9e7475132337bdb04d47ca7d2028c"))
19+
# prv = base58.b58encode(unhexlify("92c9578337c55e4f0a706e1fb93a43e8c20bdf7dcdec473f52f4ab55e663dcb6"))
20+
# prv = base64.b58decode("uc+qr3k04DB\Nf0YONdnCA==\\000DhqC9A3ooLgD3i4V34H")
21+
# prv = "uc+qr3k04DBFNf0YONdnCA==\\000DhqC9A3ooLgD3i4V34H"
22+
sk = crypto.Ed25519SigningKey(key=prv)
23+
pk = sk.get_verifying_key().encode(encoding='base58')
24+
return prv, pk
25+
26+
def generate_keypair_2(seed=None):
27+
"""Generates a cryptographic key pair.
28+
29+
Args:
30+
seed (bytes): 32-byte seed for deterministic generation.
31+
Defaults to `None`.
32+
Returns:
33+
:class:`~bigchaindb_driver.crypto.CryptoKeypair`: A
34+
:obj:`collections.namedtuple` with named fields
35+
:attr:`~bigchaindb_driver.crypto.CryptoKeypair.private_key` and
36+
:attr:`~bigchaindb_driver.crypto.CryptoKeypair.public_key`.
37+
38+
"""
39+
CryptoKeypair = namedtuple('CryptoKeypair', ('private_key', 'public_key'))
40+
41+
return CryptoKeypair(
42+
*(k.decode() for k in get_pair()))
43+
44+
45+
alice = generate_keypair_2()
46+
print(alice.public_key)
47+
operation = 'CREATE'
48+
49+
version = '2.0'
50+
51+
asset = {
52+
'data': {
53+
'bicycle': {
54+
'manufacturer': 'bkfab',
55+
'serial_number': 'abcd1234',
56+
},
57+
},
58+
}
59+
60+
metadata = {'planet': 'earth'}
61+
62+
ed25519 = Ed25519Sha256(public_key=base58.b58decode(alice.public_key))
63+
64+
output = {
65+
'amount': '1',
66+
'condition': {
67+
'details': {
68+
'type': ed25519.TYPE_NAME,
69+
'public_key': base58.b58encode(ed25519.public_key).decode(),
70+
},
71+
'uri': ed25519.condition_uri,
72+
},
73+
'public_keys': (alice.public_key,),
74+
}
75+
outputs = (output,)
76+
77+
input_ = {
78+
'fulfillment': None,
79+
'fulfills': None,
80+
'owners_before': (alice.public_key,)
81+
}
82+
inputs = (input_,)
83+
84+
handcrafted_creation_tx = {
85+
'asset': asset,
86+
'metadata': metadata,
87+
'operation': operation,
88+
'outputs': outputs,
89+
'inputs': inputs,
90+
'version': version,
91+
'id': None,
92+
}
93+
94+
message = json.dumps(
95+
handcrafted_creation_tx,
96+
sort_keys=True,
97+
separators=(',', ':'),
98+
ensure_ascii=False,
99+
)
100+
101+
message = sha3.sha3_256(message.encode())
102+
print(message.digest())
103+
ed25519.sign(message.digest(), base58.b58decode(alice.private_key))
104+
105+
fulfillment_uri = ed25519.serialize_uri()
106+
print(ed25519.serialize_binary())
107+
handcrafted_creation_tx['inputs'][0]['fulfillment'] = fulfillment_uri
108+
109+
json_str_tx = json.dumps(
110+
handcrafted_creation_tx,
111+
sort_keys=True,
112+
separators=(',', ':'),
113+
ensure_ascii=False,
114+
)
115+
116+
creation_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
117+
118+
handcrafted_creation_tx['id'] = creation_txid
119+
120+
print(json.dumps(handcrafted_creation_tx,
121+
sort_keys=True,
122+
separators=(',', ':'),
123+
ensure_ascii=False,))

inc/transaction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct {
3636
char id[65];
3737
} BIGCHAIN_TX;
3838

39+
void bigchain_fulfill_and_serialize(BIGCHAIN_TX *tx, uint8_t *json_tx, uint16_t maxlen, uint8_t *sig);
3940
void bigchain_sign_transaction(uint8_t *json_tx, uint16_t len, uint8_t *priv_key, uint8_t *pub_key, uint8_t *sig);
4041
char* bigchain_build_json_inputs(BIGCHAIN_INPUT *inputs, uint8_t num_inputs, char *json_obj);
4142
char* bigchain_build_json_outputs(BIGCHAIN_OUTPUT *outputs, uint8_t num_outputs, char *json_obj);

libs/base64

Submodule base64 added at a949021

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ file(GLOB SOURCES "*.c")
33
file(GLOB JSON_SOURCES "../libs/json-maker/*.c")
44
file(GLOB TREZOR_CRYPTO_ED255 "../libs/trezor-crypto/ed25519-donna/*.c")
55
file(GLOB TREZOR_CRYPTO "../libs/trezor-crypto/*.c")
6+
file(GLOB BASE64 "../libs/base64/base64.c")
7+
68

79
add_library(cbig STATIC
810
${SOURCES}
911
${TREZOR_CRYPTO}
1012
${TREZOR_CRYPTO_ED255}
1113
${JSON_SOURCES}
14+
${BASE64}
1215
)
1316

1417
target_include_directories(cbig PUBLIC
1518
../inc
1619
../libs/trezor-crypto/
1720
../
1821
../libs/json-maker
22+
../libs/base64
1923
)

src/transaction.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
#include "transaction.h"
22

3+
4+
void bigchain_fulfill_and_serialize(BIGCHAIN_TX *tx, uint8_t *json_tx, uint16_t maxlen, uint8_t *sig)
5+
{
6+
char fulfillment[256] = {0};
7+
8+
bintob64(fulfillment, sig, 64);
9+
memcpy(tx->inputs[0].fulfillment, fulfillment, strlen(fulfillment));
10+
11+
memset(json_tx, 0, maxlen);
12+
bigchain_build_json_tx(tx, json_tx);
13+
14+
// sha3_256((const unsigned char*)json_tx, MIN(maxlen, strlen(json_tx)), tx->id);
15+
sha3_256((const unsigned char*)json_tx, strlen(json_tx), tx->id);
16+
17+
// 1. derencode sig
18+
// 2. base64 encode
19+
// 3. remove == from b64 str
20+
21+
memset(json_tx, 0, maxlen);
22+
bigchain_build_json_tx(tx, json_tx);
23+
24+
}
25+
326
/*
427
* Takes a json string, hashes it sha3_256 and signs it with ed25519.
528
*/
@@ -98,6 +121,6 @@ void bigchain_build_json_tx(BIGCHAIN_TX *tx, char *json_tx) {
98121
// VERSION
99122
p = json_str(p, "version", tx->version);
100123
p = json_objClose(p);
101-
124+
p = json_end(p);
102125
}
103126

tests/test_sig.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define TEST_PUBLIC_KEY_0 "6k17YAe4mJsqwPNgGj9tE1aMYLhpeqgJJivqaHeTTU5a"
1414
static const char* tx_json =
1515
"{\"asset\":{\"data\":{\"bicycle\":{\"manufacturer\":\"bkfab\",\"serial_number\":\"abcd1234\"}}},\"id\":null,\"inputs\":[{\"fulfillment\":null,\"fulfills\":null,\"owners_before\":[\"6k17YAe4mJsqwPNgGj9tE1aMYLhpeqgJJivqaHeTTU5a\"]}],\"metadata\":{\"planet\":\"earth\"},\"operation\":\"CREATE\",\"outputs\":[{\"amount\":\"1\",\"condition\":{\"details\":{\"public_key\":\"6k17YAe4mJsqwPNgGj9tE1aMYLhpeqgJJivqaHeTTU5a\",\"type\":\"ed25519-sha-256\"},\"uri\":\"ni:///sha-256;Vta3W592Kt_Y2ljfHEDZLd4OCZPHLiHyCgjNNKrrNwo?fpt=ed25519-sha-256&cost=131072\"},\"public_keys\":[\"6k17YAe4mJsqwPNgGj9tE1aMYLhpeqgJJivqaHeTTU5a\"]}],\"version\":\"2.0\"}";
16+
1617
char privkey[] = {'\x24','\x70','\x31','\x98','\x89','\xfe','\xd2','\x13','\x9a','\x6d','\xd9','\x9e','\xa3','\xae','\xf2','\xd3','\x96','\xe9','\xe7','\x47','\x51','\x32','\x33','\x7b','\xdb','\x04','\xd4','\x7c','\xa7','\xd2','\x02','\x8c'};
1718
char pubkey[] = {'\x55','\x4e','\x89','\x70','\x1b','\x6d','\xca','\x9b','\x28','\x61','\x1f','\xb3','\x61','\x41','\x71','\x3a','\x2e','\x18','\x52','\x02','\x0e','\x8e','\xa8','\xa0','\x2e','\x27','\xda','\x40','\xef','\xac','\xcc','\x25'};
1819

@@ -90,16 +91,32 @@ void test_bigchain_build_json_inputs(void) {
9091

9192
void test_bigchain_build_json_tx(void) {
9293
BIGCHAIN_TX tx;
93-
char json[600] = {0};
94+
char json[6000] = {0};
9495

96+
memset(&tx, 0, sizeof(BIGCHAIN_TX));
9597
prepare_tx(&tx);
9698
bigchain_build_json_tx(&tx, json);
99+
100+
TEST_ASSERT_EQUAL(0, memcmp(tx_json, json, 572));
97101

98-
TEST_ASSERT_EQUAL(0, memcmp(tx_json, json, strlen(tx_json)));
99-
}
102+
char sig[128] = {0};
103+
bigchain_sign_transaction((uint8_t*)json, strlen(json), (uint8_t*)privkey, (uint8_t*)pubkey, (uint8_t*)sig);
104+
bigchain_fulfill_and_serialize(&tx, json, 6000, sig);
105+
106+
// Base64
107+
char fullfil[] = "pGSAIFVOiXAbbcqbKGEfs2FBcTouGFICDo6ooC4n2kDvrMwlgUCNMaKJ6aV03acNBqcYCffEWAcNQdhxS1o7beb6Ispxft0Ymgv2-oUlOFAeT6KeP65wLs_SOSKrLRaAIA_U75kC";
108+
109+
TEST_ASSERT_EQUAL(0, memcmp(tx.inputs->fulfillment, fullfil, 137));
110+
111+
// memcpy(tx.inputs->fulfillment, fullfil, 137); memset(json,0,6000);
112+
// //id
113+
// bigchain_build_json_tx(&tx, json);
100114

101115

102116

117+
}
118+
119+
103120
int main(void) {
104121
UNITY_BEGIN();
105122
RUN_TEST(test_sig);

0 commit comments

Comments
 (0)