Giter Club home page Giter Club logo

neo-python-core's Introduction

Library for working with NEO related data in Python, without database dependencies.

image

image

  • Datatypes like UInt160, KeyPair, BigInteger and basic string to address and address to UInt160 methods
  • Includes a useful cli-tool np-utils (see help with np-utils -h)
  • Compatible with Python 3.5+
  • Used by neo-python
  • https://pypi.python.org/pypi/neocore

np-utils examples:

$ np-utils -h
usage: np-utils [-h] [--version] [--address-to-scripthash address]
                [--scripthash-to-address scripthash] [--create-wallet]

optional arguments:
-h, --help            show this help message and exit
--version             show program's version number and exit
--address-to-scripthash address
                        Convert an address to scripthash
--scripthash-to-address scripthash
                        Convert scripthash to address
--create-wallet       Create a wallet

$ np-utils --create-wallet
{
"private_key": "KwJqCbjsmGUCqbkp83Nxi9MJ9mA7F8EN4tebJVWjYZBEoWCNxCaF",
"address": "AHVvg26CNz1vxteJfeHy4R8P4VN8SydCM6"
}

$ np-utils --address-to-scripthash AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y
Scripthash big endian:  0xe9eed8dc39332032dc22e5d6e86332c50327ba23
Scripthash little endian: 23ba2703c53263e8d6e522dc32203339dcd8eee9
Scripthash neo-python format: b'#\xba\'\x03\xc52c\xe8\xd6\xe5"\xdc2 39\xdc\xd8\xee\xe9'

$ np-utils --scripthash-to-address 0xe9eed8dc39332032dc22e5d6e86332c50327ba23
AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y

$ np-utils --scripthash-to-address 23ba2703c53263e8d6e522dc32203339dcd8eee9
Detected little endian scripthash. Converting to big endian for internal use.
Big endian scripthash: 0xe9eed8dc39332032dc22e5d6e86332c50327ba23
AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y

Getting started

You need Python 3.5 or higher!

You can install neocore from PyPI with easy_install or pip:

$ pip install -U neocore

Alternatively, if you want to work on the code, clone this repository and setup your venv:

  • Clone the repo: git clone https://github.com/CityOfZion/neo-python-core.git
  • Create a Python 3 virtual environment and activate it:
$ python3 -m venv venv
$ source venv/bin/activate
  • Then install the requirements:
$ pip install -e .
$ pip install -r requirements_dev.txt

Useful commands

$ make lint
$ make test
$ make coverage

Release checklist

(Only for admins)

Releasing a new version on GitHub automatically uploads this release to PyPI. This is a checklist for releasing a new version:

# Only in case you want to increase the version number again (eg. scope changed from patch to minor):
bumpversion --no-tag minor|major

# Update ``HISTORY.rst`` with the new version number and the changes and commit this
vi HISTORY.rst
git commit -m "Updated HISTORY.rst" HISTORY.rst

# Set the release version number and create the tag
bumpversion release

# Increase patch number and add `-dev`
bumpversion --no-tag patch

# Push to GitHub, which also updates the PyPI package
git push && git push --tags

neo-python-core's People

Contributors

ambethia avatar belane avatar fl0wjacky avatar hbasria avatar igormcoelho avatar ixje avatar jseagrave21 avatar localhuman avatar metachris avatar mgraczyk avatar pyup-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

neo-python-core's Issues

Bring coverage back to 100% - add tests for cli.py

  • neo-python-core version: v0.3.11-dev
  • Python version: 3.x
  • Operating System: osx

Description

Test coverage dropped to 98% after the addition of /neocore/bin/cli.py. We should bring it back to ๐Ÿ’ฏ

Add Python 2 support

Currently neocore is only compatible with Python 3, because several of the internal modules do some py3 specific byte transformations.

Could be made compatible with Python 2 with some effort, would be great to have I think, but perhaps not a super priority. All the requirements work already with Python 2.

How to hack on this

Remove the line that restricts neocore to Python 3: setup.py#L21

Then create a venv, install the deps and run the tests

virtualenv2 venv-py2
. venv-py2/bin/activate
pip install -e .
pip install -r requirements_dev.txt
python -m unittest discover tests

No value for argument 'message' in unbound method call

  • neo-python-core version:latest
  • Python version:3.7
  • Operating System:windows

Description

a = 'AKibPRzkoZpHnPkF6qvuW2Q4hG9gKBwGpR'
b = neoapi.CryptoInstance.Hash160(a)
print (b)

error:E1120:No value for argument 'message' in unbound method call

What I Did

why? how to use this function?

Move Cryptography and KeyPair from neo-python to neocore

Also migrate the related tests from neo-python, and add new ones where applicable. We aim for >90% test coverage on neocore ;)

Please keep the same namespaces, directory names and filenames for the Cryptography stuff, so we can later just replace neo.Cryptography.* with neocore.Cryptography.* in neo-python. KeyPair will probably go to neocore.KeyPair.

Steps:

  • Move Cryptography and KeyPair from neo-python to neocore, along with all the related tests, add more tests where applicable.
  • Once merged, we release a new version of neocore to PyPI
  • Then we make a PR to neo-python which uses the new neocore and replaces the references of the related classes

Difference in BigInteger causing fork (neo-python vs neo-cli C#)

  • neo-python-core version: master
  • Python version: 3
  • Operating System: Deepin

Description

I deployed this contract (in C#) and tested storage "size" and "value":

using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.Numerics;
namespace NeoContract1
{
    public class Contract1 : SmartContract
    {
        public static int Main()
        {
            Runtime.Notify("oi");
            BigInteger bi = 1;
            Runtime.Notify(bi);
            bi -= 1;
            byte[] ba = bi.ToByteArray();
            Runtime.Notify(ba.Length);
            if(ba.Length > 0)
                Runtime.Notify(ba[0]);
            Storage.Put(Storage.CurrentContext, "size", ba.Length+10);
            Storage.Put(Storage.CurrentContext, "value", ba);
            
            return 3;
        }
    }
}

According to Python RPC, "size is 10" (because Bigint 0 to bytearray is empty), but according to C# RPC, "size is 11" (because BigInt 0 in C# is b'\x00', not empty).

What I Did

I used neocompiler.io Eco platform, that already integrates full neo-python and neo-cli + neo C# ecosystem.
It's easy to change the RPC node (on top of the screen), python is localhost:10332 and C# is localhost:30335 (if doing locally). Online is https://node1.neocompiler.io and https://eco.neocompiler.io (a current workaround for python rpc... will receive a better name in a few days :D)

It's worth mentioning that theses tests are part of the fork competition Vitor and I are promoting at NeoResearch, with the goal of fixing differences in Neo ecosystem modules that could cause future forks in the blockchain.

ECCurve.py: Add tests for full coverage, or perhaps even replace

See also #26 and #21

ECCurve.py is currently hardly covered, but is proven to be functional by real world usage. #21 was initiated to look at a maintained ECDSA package to replace the homebrew instance, but we could not conclude for now.

This exclusion suggestion has been discussed with @localhuman on slack and we agreed to keep #21 open to stay reminded of the fact that we 1) have to replace the module some day or 2) fix coverage.

Would be great to write tests for full coverage of ECCurve.py as well.

Sign without initialized KeyPair uses incorrect curve parameters

  • neo-python-core version: 0.5.7-dev
  • Python version: 3.6.3
  • Operating System: OSX

Description

As described by user Vinc in Discord, the following code demonstrates two different results for Sign/Verify using the same input and private key:

from neocore.Cryptography.Crypto import Crypto
from neocore.KeyPair import KeyPair
import binascii

message = binascii.hexlify(b'Hello World')
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))

print(signature.hex())
print(Crypto.VerifySignature(message, signature, key.PublicKey, unhex = True))


message = binascii.hexlify(b'Hello World')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')

print(signature.hex())
print(Crypto.VerifySignature(message, signature, key.PublicKey, unhex = True))

The only difference in the two tests is that in the first test, Sign is called before KeyPair, and it returns False when time for verification. The second test, with KeyPair called before Sign, returns True.

This is related to the Sign function not initializing the bitcoin library with the secp256r1 curve values as the initialization in KeyPair does. So the first test actually signs the data with secp256k1, the default for the bitcoin library.

I recommend adding code to initialize the bitcoin library with secp256r1 curve values in Sign (and any other operation that could be called where a private key could be passed in directly instead of as part of an already-initialized KeyPair object.

[Brainstorm] what Cryptography Helper functions do we want/need?

Description

The purpose of this issue is to brainstorm and collect what functions are useful to have as helper methods for cryptography related operations. Some of these might already be present, which is fine since we're trying to collect an overview.

  • public key (ECPoint or bytes) to redeem script
  • public key to script hash
  • public key to public address
  • redeem script to script hash
  • redeem script to public address
  • script hash to public address
  • signature to public address
  • WIF to public address
  • WIF to script hash
  • WIF to private key
  • private key to WIF
  • double SHA256 (convenience wrapper)

Crypto related, not necessarily as helper?
Some of these are arguably better suited to run/implement in KeyPair. Opening the discussion here to see if e.g. convenience wrappers should be provided.

  • isValidWIF
  • isValidPublicAddress
  • encrypt WIF (NEP2)
  • decrypt WIF (NEP2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.