Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed Nov 20, 2018
2 parents e9d5250 + 11c86d2 commit 22927a9
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 677 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ node/test_data/go_example_image.tar
coverage.out

__pycache__
*.pyc
661 changes: 0 additions & 661 deletions GNU-AGPL-3.0.txt

This file was deleted.

30 changes: 18 additions & 12 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
c3-sdk-python is an sdk for developing c3 apps in python.
Copyright (C) 2018 C3 Labs
The MIT License (MIT)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Copyright (c) 2018 C3 Labs

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,21 @@ deps: clean
cp "${GOPATH}/src/github.com/c3systems/c3-go/lib/c/common/stringutil/stringutil.so" ./lib/ && \
cp "${GOPATH}/src/github.com/c3systems/c3-go/lib/c/config/config.so" ./lib/

.PHONY: freeze
freeze:
@python -m pip freeze > requirements.txt

.PHONY: clean
clean:
@-find . -type f -name *.so -delete

.PHONY: test
test: test/hexutil test/hashutil

.PHONY: test/hexutil
test/hexutil:
@python util/hexutil_test.py

.PHONY: test/hashutil
test/hashutil:
@python util/hashutil_test.py
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
[![License](http://img.shields.io/badge/license-GNU%20AGPL%203.0-blue.svg)](https://raw.githubusercontent.com/c3systems/c3-sdk-python/master/LICENSE) [![Build Status](https://travis-ci.org/c3systems/c3-sdk-python.svg?branch=master)](https://travis-ci.org/c3systems/c3-sdk-python) [![Coverage Status](https://coveralls.io/repos/github/c3systems/c3-sdk-python/badge.svg?branch=master)](https://coveralls.io/github/c3systems/c3-sdk-python?branch=master) [![Automated Release Notes by gren](https://img.shields.io/badge/%F0%9F%A4%96-release%20notes-00B2EE.svg)](https://github-tools.github.io/github-release-notes/) [![stability-experimental](https://img.shields.io/badge/stability-experimental-orange.svg)](https://github.com/emersion/stability-badges#experimental)

## Dependencies
1. Go
2. Python 3

## Usage
1. [Install Go](https://golang.org/doc/install)
Expand All @@ -12,4 +15,4 @@

## License

[GNU AGPL 3.0](LICENSE)
[MIT](LICENSE)
143 changes: 143 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
acme==0.9.3
altgraph==0.10.2
appnope==0.1.0
backports.shutil-get-terminal-size==1.0.0
base58==1.0.0
bdist-mpkg==0.5.0
beautifulsoup4==4.5.1
bitcoin==1.1.42
bonjour-py==0.3
boto3==1.4.1
botocore==1.4.67
certbot==0.9.3
certifi==2016.9.26
cffi==1.8.3
click==6.6
ConfigArgParse==0.11.0
configobj==5.0.6
configparser==3.5.0
contextlib2==0.5.4
cryptography==1.5.3
decorator==4.1.2
dnspython==1.15.0
docutils==0.12
dpkt==1.8.6
enum34==1.1.6
eth-testrpc==0.8.7
ethereum==1.6.0
ethereum-abi-utils==0.2.2
ethereum-tester-client==1.2.5
funcsigs==1.0.2
futures==3.0.5
gevent==1.1.2
geventhttpclient==1.3.1
greenlet==0.4.10
idna==2.1
image2ipfs==0.0.5
ipaddress==1.0.17
ipython==5.5.0
ipython-genutils==0.2.0
ISStreamer==0.2.2
jmespath==0.9.0
json-rpc==1.10.3
knockpy==4.0
letsencrypt==0.7.0
letsencrypt-s3front==0.1.3
macholib==1.5.1
matplotlib==1.3.1
mock==2.0.0
modulegraph==0.10.4
ndg-httpsclient==0.4.2
numpy==1.8.0rc1
parsedatetime==2.1
pathlib2==2.3.0
pbkdf2==1.3
pbr==1.10.0
pexpect==4.2.1
pickleshare==0.7.4
populus==1.4.1
prompt-toolkit==1.0.15
ptyprocess==0.5.2
py==1.4.31
py-geth==1.4.1
py-solc==0.6.0
py2app==0.7.3
pyasn1==0.1.9
pycparser==2.17
pycryptodome==3.6.6
pyethash==0.1.27
pyfiglet==0.7.5
Pygments==2.2.0
pylru==1.0.9
pyobjc-core==2.5.1
pyobjc-framework-Accounts==2.5.1
pyobjc-framework-AddressBook==2.5.1
pyobjc-framework-AppleScriptKit==2.5.1
pyobjc-framework-AppleScriptObjC==2.5.1
pyobjc-framework-Automator==2.5.1
pyobjc-framework-CFNetwork==2.5.1
pyobjc-framework-Cocoa==2.5.1
pyobjc-framework-Collaboration==2.5.1
pyobjc-framework-CoreData==2.5.1
pyobjc-framework-CoreLocation==2.5.1
pyobjc-framework-CoreText==2.5.1
pyobjc-framework-DictionaryServices==2.5.1
pyobjc-framework-EventKit==2.5.1
pyobjc-framework-ExceptionHandling==2.5.1
pyobjc-framework-FSEvents==2.5.1
pyobjc-framework-InputMethodKit==2.5.1
pyobjc-framework-InstallerPlugins==2.5.1
pyobjc-framework-InstantMessage==2.5.1
pyobjc-framework-LatentSemanticMapping==2.5.1
pyobjc-framework-LaunchServices==2.5.1
pyobjc-framework-Message==2.5.1
pyobjc-framework-OpenDirectory==2.5.1
pyobjc-framework-PreferencePanes==2.5.1
pyobjc-framework-PubSub==2.5.1
pyobjc-framework-QTKit==2.5.1
pyobjc-framework-Quartz==2.5.1
pyobjc-framework-ScreenSaver==2.5.1
pyobjc-framework-ScriptingBridge==2.5.1
pyobjc-framework-SearchKit==2.5.1
pyobjc-framework-ServiceManagement==2.5.1
pyobjc-framework-Social==2.5.1
pyobjc-framework-SyncServices==2.5.1
pyobjc-framework-SystemConfiguration==2.5.1
pyobjc-framework-WebKit==2.5.1
pyOpenSSL==0.13.1
pyparsing==2.0.1
pypcap==1.1.1
pyRFC3339==1.0
pysha3==0.3
pytest==3.0.4
python-dateutil==2.5.3
python-firebase==1.2
python2-pythondialog==3.4.0
pytz==2013.7
pyusb==1.0.0
PyYAML==3.12
repoze.lru==0.6
requests==2.6.0
rethinkdb==2.0.0.post1
rlp==0.4.7
s3transfer==0.1.9
scandir==1.6
scapy==2.3.3
scipy==0.13.0b1
scrypt==0.8.0
secp256k1==0.13.2
selenium==3.3.1
simplegeneric==0.8.1
six==1.10.0
termcolor==1.1.0
toposort==1.5
traitlets==4.3.2
vboxapi==1.0
wcwidth==0.1.7
web3==3.2.0
websocket-client==0.46.0
Werkzeug==0.11.11
xattr==0.6.4
zope.component==4.3.0
zope.event==4.2.0
zope.interface==4.1.1
4 changes: 2 additions & 2 deletions sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def newMethod(*args):
def setInitialState(self):
currState = ""

file = open(self.statefile, "r")
currState = file.read()
with open(self.statefile, "r") as file:
currState = file.read()

if len(currState) == 0:
print("no current state")
Expand Down
2 changes: 1 addition & 1 deletion sdk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sdk
import json

hexutil = CDLL('./lib/hexutil/hexutil.so')
hexutil = CDLL('./lib/hexutil.so')

c3 = sdk.NewC3(stateFilePath = './lib/state.json')

Expand Down
17 changes: 17 additions & 0 deletions util/hashutil/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
from Crypto.Hash import SHA512
import hexutil

def new ():
h = SHA512.new(data=os.urandom(8), truncate='256')
return h.digest()

def hash (data):
h = SHA512.new(data=data.encode(), truncate='256')
return h.digest()

def hashToHexString (data):
return hexutil.encodeToString(hash(data))

def equals (hexHash, data):
return hexHash == hashToHexString(data)
18 changes: 18 additions & 0 deletions util/hashutil_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
import hashutil

class Test(unittest.TestCase):
def test_new(self):
self.assertEqual(len(hashutil.new().hex()), 64)

def test_hash(self):
self.assertEqual(hashutil.hash('hello world').hex(), '0ac561fac838104e3f2e4ad107b4bee3e938bf15f2b15f009ccccd61a913f017')

def test_hashToHexString(self):
self.assertEqual(hashutil.hashToHexString('hello world'), '0x0ac561fac838104e3f2e4ad107b4bee3e938bf15f2b15f009ccccd61a913f017')

def test_equals(self):
self.assertEqual(hashutil.equals('0x0ac561fac838104e3f2e4ad107b4bee3e938bf15f2b15f009ccccd61a913f017', 'hello world'), True)

if __name__ == '__main__':
unittest.main()
44 changes: 44 additions & 0 deletions util/hexutil/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import codecs
import re
import binascii
import struct

def encodeString(text):
return addPrefix(str(binascii.hexlify(text.encode()), 'ascii').lower())

def decodeString(hex_str):
return codecs.decode(stripPrefix(hex_str), 'hex')

def encodeToString(btext):
return addPrefix(str(binascii.hexlify(btext), 'ascii').lower())

def encodeBytes(btext):
return binascii.hexlify(btext)

def decodeBytes(bhex):
return binascii.unhexlify(bhex)

def encodeBigInt(i):
return i.to_bytes((i.bit_length() + 7) // 8 or 1, 'big')

def decodeBigInt(hex_str):
return int(stripPrefix(hex_str), 16)

def encodeInt64(i):
return i.to_bytes((i.bit_length() + 7) // 8 or 1, 'big')

def decodeInt64(hex_str):
return int(stripPrefix(hex_str), 16)

# https://www.h-schmidt.net/FloatConverter/IEEE754.html
def encodeFloat64(f):
return struct.pack('!f', f)

def decodeFloat64(hexstr):
return struct.unpack('>f', bytes.fromhex(stripPrefix(hexstr)))[0]

def stripPrefix(text):
return re.sub(r'^{0}'.format('0x'), '', text)

def addPrefix(text):
return '0x'+stripPrefix(text)
56 changes: 56 additions & 0 deletions util/hexutil_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import unittest
import hexutil

class Test(unittest.TestCase):
def test_encodeString(self):
self.assertEqual(hexutil.encodeString('hello'), '0x68656c6c6f')
self.assertEqual(hexutil.encodeString('123'), '0x313233')

def test_decodeStr(self):
self.assertEqual(hexutil.decodeString('0x1234'), b'\x124')
def test_encodeToString(self):
self.assertEqual(hexutil.encodeToString(b'hello'), '0x68656c6c6f')

def test_encodeBytes(self):
self.assertEqual(hexutil.encodeBytes(b'hello'), b'68656c6c6f')

def test_decodeBytes(self):
self.assertEqual(hexutil.decodeBytes(b'68656c6c6f'), b'hello')

def test_encodeBigInt(self):
self.assertEqual(hexutil.encodeBigInt(123), b'\x7b')
self.assertEqual(hexutil.encodeBigInt(53452345), bytes.fromhex('032f9e39'))
self.assertEqual(hexutil.encodeBigInt(7237005577332262213973186563042994240829374041602535253248099000494570602496), bytes.fromhex('10000000000000000000000000000000000000000000002a646e18c953780000'))

def test_decodeBigInt(self):
self.assertEqual(hexutil.decodeBigInt('0x7B'), 123)
self.assertEqual(hexutil.decodeBigInt('0x10000000000000000000000000000000000000000000002a646e18c953780000'), 7237005577332262213973186563042994240829374041602535253248099000494570602496)

def test_encodeInt64(self):
self.assertEqual(hexutil.encodeInt64(123), b'\x7b')
self.assertEqual(hexutil.encodeInt64(922337203685477807), bytes.fromhex('0ccccccccccccdaf'))

def test_decodeInt64(self):
self.assertEqual(hexutil.decodeInt64('0x7b'), 123)
self.assertEqual(hexutil.decodeInt64('0xccccccccccccdaf'), 922337203685477807)

def test_encodeFloat64(self):
self.assertEqual(hexutil.encodeFloat64(123).hex(), '42f60000')
self.assertEqual(hexutil.encodeFloat64(-561.2863).hex(), 'c40c5253')

def test_decodeFloat64(self):
self.assertEqual(hexutil.decodeFloat64('0x42f60000'), 123)
self.assertEqual(hexutil.decodeFloat64('0x424e4b31'), 51.573429107666016)
self.assertEqual(hexutil.decodeFloat64('c40c5253'), -561.2863159179688)

def test_stripPrefix(self):
self.assertEqual(hexutil.stripPrefix('0x1234'), '1234')
self.assertEqual(hexutil.stripPrefix('1234'), '1234')
self.assertEqual(hexutil.stripPrefix('0x'), '')

def test_addPrefix(self):
self.assertEqual(hexutil.addPrefix('1234'), '0x1234')
self.assertEqual(hexutil.addPrefix('0x1234'), '0x1234')

if __name__ == '__main__':
unittest.main()

0 comments on commit 22927a9

Please sign in to comment.