Skip to content

Instantly share code, notes, and snippets.

@danintel
Created June 21, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danintel/41a5a92d876fb25545debeda053de8d6 to your computer and use it in GitHub Desktop.
Save danintel/41a5a92d876fb25545debeda053de8d6 to your computer and use it in GitHub Desktop.
Patch to add CBOR serialization to Sawtooth Cookiejar application
Showing with 20 additions and 8 deletions.
1 pyclient/Dockerfile
@@ -38,6 +38,7 @@ RUN echo "deb http://repo.sawtooth.me/ubuntu/ci xenial universe" >> /etc/apt/sou
python3-cbor \
python3-cchardet=2.0a3-1 \
python3-chardet=2.3.0-1 \
+ python3-cbor \
python3-colorlog \
python3-cov-core \
python3-cryptography-vectors=1.7.2-1 \
10 pyclient/cookiejar_client.py
@@ -22,6 +22,7 @@
import time
import requests
import yaml
+import cbor
from sawtooth_signing import create_context
from sawtooth_signing import CryptoFactory
@@ -141,10 +142,11 @@ def _wrap_and_send(self, action, amount):
Called by bake() and eat().
'''
- # Generate a CSV UTF-8 encoded string as the payload.
- raw_payload = action
- raw_payload = ",".join([raw_payload, str(amount)])
- payload = raw_payload.encode() # Convert Unicode to bytes
+ # Generate a CBOR UTF-8 encoded string as the payload.
+ payload = cbor.dumps({
+ 'Action': action,
+ 'Amount': amount
+ })
# Construct the address where we'll store our state.
# We just have one input and output address (the same one).
1 pyprocessor/Dockerfile
@@ -23,6 +23,7 @@ RUN \
apt-transport-https \
build-essential \
ca-certificates \
+ python3-cbor \
python3-sawtooth-sdk \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
16 pyprocessor/cookiejar_tp.py
@@ -18,6 +18,7 @@
import hashlib
import logging
+import cbor
from sawtooth_sdk.processor.handler import TransactionHandler
from sawtooth_sdk.processor.exceptions import InvalidTransaction
@@ -87,11 +88,18 @@ def apply(self, transaction, context):
# Get the payload and extract the cookiejar-specific information.
# It has already been converted from Base64, but needs deserializing.
- # It was serialized with CSV: action, value
+ # It was serialized with CBOR: action, amount
header = transaction.header
- payload_list = transaction.payload.decode().split(",")
- action = payload_list[0]
- amount = payload_list[1]
+ payload_dictionary = cbor.loads(transaction.payload)
+ try:
+ action = payload_dictionary['Action']
+ except AttributeError:
+ raise InvalidTransaction('Action is required')
+
+ try:
+ amount = payload_dictionary['Amount']
+ except AttributeError:
+ raise InvalidTransaction('Amount is required')
# Get the signer's public key, sent in the header from the client.
from_key = header.signer_public_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment