Skip to content

Commit 49862f3

Browse files
author
Troy McConaghy
authored
Problem: Error when building Handcrafting Transactions docs (#476)
TypeError: Object of type 'bytes' is not JSON serializable Solution: Use base58.b58encode(key).decode() which is a str, instead of base58.b58encode(key) which is a bytes object
1 parent 0f63125 commit 49862f3

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

docs/handcraft.rst

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,13 @@ As for the details:
273273

274274
.. ipython::
275275

276+
In [0]: def b58(key):
277+
...: # convert key to a base58 string
278+
...: return base58.b58encode(key).decode()
279+
276280
In [0]: condition_details = {
277281
...: 'type': ed25519.TYPE_NAME,
278-
...: 'public_key': base58.b58encode(ed25519.public_key),
282+
...: 'public_key': b58(ed25519.public_key),
279283
...: }
280284

281285
We can now easily assemble the ``dict`` for the output:
@@ -304,7 +308,7 @@ Let's recap and set the ``outputs`` key with our self-constructed condition:
304308
...: 'condition': {
305309
...: 'details': {
306310
...: 'type': ed25519.TYPE_NAME,
307-
...: 'public_key': base58.b58encode(ed25519.public_key),
311+
...: 'public_key': b58(ed25519.public_key),
308312
...: },
309313
...: 'uri': ed25519.condition_uri,
310314
...: },
@@ -459,7 +463,7 @@ Let's recap how we've put all the code together to generate the above payload:
459463
'condition': {
460464
'details': {
461465
'type': ed25519.TYPE_NAME,
462-
'public_key': base58.b58encode(ed25519.public_key),
466+
'public_key': b58(ed25519.public_key),
463467
},
464468
'uri': ed25519.condition_uri,
465469
},
@@ -608,7 +612,7 @@ Handcrafting a ``CREATE`` transaction can be done as follows:
608612
'condition': {
609613
'details': {
610614
'type': ed25519.TYPE_NAME,
611-
'public_key': base58.b58encode(ed25519.public_key),
615+
'public_key': b58(ed25519.public_key),
612616
},
613617
'uri': ed25519.condition_uri,
614618
},
@@ -816,7 +820,7 @@ outputs
816820
...: 'condition': {
817821
...: 'details': {
818822
...: 'type': ed25519.TYPE_NAME,
819-
...: 'public_key': base58.b58encode(ed25519.public_key),
823+
...: 'public_key': b58(ed25519.public_key),
820824
...: },
821825
...: 'uri': ed25519.condition_uri,
822826
...: },
@@ -932,7 +936,7 @@ Let's recap how we got here:
932936
'condition': {
933937
'details': {
934938
'type': ed25519.TYPE_NAME,
935-
'public_key': base58.b58encode(ed25519.public_key),
939+
'public_key': b58(ed25519.public_key),
936940
},
937941
'uri': ed25519.condition_uri,
938942
},
@@ -1064,7 +1068,7 @@ In a nutshell
10641068
'condition': {
10651069
'details': {
10661070
'type': ed25519.TYPE_NAME,
1067-
'public_key': base58.b58encode(ed25519.public_key),
1071+
'public_key': b58(ed25519.public_key),
10681072
},
10691073
'uri': ed25519.condition_uri,
10701074
},
@@ -1189,7 +1193,7 @@ Handcrafting the ``CREATE`` transaction for our :ref:`bicycle sharing example
11891193
# CRYPTO-CONDITIONS: construct an unsigned fulfillment dictionary
11901194
unsigned_fulfillment_dict = {
11911195
'type': ed25519.TYPE_NAME,
1192-
'public_key': base58.b58encode(ed25519.public_key),
1196+
'public_key': b58(ed25519.public_key),
11931197
}
11941198
11951199
output = {
@@ -1300,12 +1304,12 @@ to Bob:
13001304
# CRYPTO-CONDITIONS: get the unsigned fulfillment dictionary (details)
13011305
bob_unsigned_fulfillment_dict = {
13021306
'type': bob_ed25519.TYPE_NAME,
1303-
'public_key': base58.b58encode(bob_ed25519.public_key),
1307+
'public_key': b58(bob_ed25519.public_key),
13041308
}
13051309
13061310
carly_unsigned_fulfillment_dict = {
13071311
'type': carly_ed25519.TYPE_NAME,
1308-
'public_key': base58.b58encode(carly_ed25519.public_key),
1312+
'public_key': b58(carly_ed25519.public_key),
13091313
}
13101314
13111315
bob_output = {
@@ -1554,7 +1558,7 @@ Generate the output condition:
15541558
In [0]: condition_details = {
15551559
...: 'subconditions': [
15561560
...: {'type': s['body'].TYPE_NAME,
1557-
...: 'public_key': base58.b58encode(s['body'].public_key)}
1561+
...: 'public_key': b58(s['body'].public_key)}
15581562
...: for s in threshold_sha256.subconditions
15591563
...: if (s['type'] == 'fulfillment' and
15601564
...: s['body'].TYPE_NAME == 'ed25519-sha-256')
@@ -1673,7 +1677,7 @@ The transfer to Carol:
16731677

16741678
In [0]: unsigned_fulfillments_dict = {
16751679
...: 'type': carol_ed25519.TYPE_NAME,
1676-
...: 'public_key': base58.b58encode(carol_ed25519.public_key),
1680+
...: 'public_key': b58(carol_ed25519.public_key),
16771681
...: }
16781682

16791683
In [0]: condition_uri = carol_ed25519.condition.serialize_uri()
@@ -1820,7 +1824,7 @@ Handcrafting the ``'CREATE'`` transaction
18201824
condition_details = {
18211825
'subconditions': [
18221826
{'type': s['body'].TYPE_NAME,
1823-
'public_key': base58.b58encode(s['body'].public_key)}
1827+
'public_key': b58(s['body'].public_key)}
18241828
for s in threshold_sha256.subconditions
18251829
if (s['type'] == 'fulfillment' and
18261830
s['body'].TYPE_NAME == 'ed25519-sha-256')
@@ -1921,7 +1925,7 @@ Handcrafting the ``'TRANSFER'`` transaction
19211925
19221926
unsigned_fulfillments_dict = {
19231927
'type': carol_ed25519.TYPE_NAME,
1924-
'public_key': base58.b58encode(carol_ed25519.public_key),
1928+
'public_key': b58(carol_ed25519.public_key),
19251929
}
19261930
19271931
condition_uri = carol_ed25519.condition.serialize_uri()
@@ -2077,7 +2081,7 @@ Handcrafting the ``'CREATE'`` transaction
20772081
condition_details = {
20782082
'subconditions': [
20792083
{'type': s['body'].TYPE_NAME,
2080-
'public_key': base58.b58encode(s['body'].public_key)}
2084+
'public_key': b58(s['body'].public_key)}
20812085
for s in threshold_sha256.subconditions
20822086
if (s['type'] == 'fulfillment' and
20832087
s['body'].TYPE_NAME == 'ed25519-sha-256')
@@ -2187,7 +2191,7 @@ Handcrafting the ``'TRANSFER'`` transaction
21872191
'condition': {
21882192
'details': {
21892193
'type': carol_ed25519.TYPE_NAME,
2190-
'public_key': base58.b58encode(carol_ed25519.public_key),
2194+
'public_key': b58(carol_ed25519.public_key),
21912195
},
21922196
'uri': condition_uri,
21932197
},

0 commit comments

Comments
 (0)