external: new subdirectory for all external libraries and submodules.

You will want to 'make distclean' after this.

I also removed libsecp; we use the one in in libwally anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-08-29 01:41:01 +09:30
committed by Christian Decker
parent c6976cd947
commit f42f34b82d
363 changed files with 108 additions and 20853 deletions

View File

@@ -0,0 +1,67 @@
"""A wallycore version of https://github.com/trezor/python-mnemonic"""
import wallycore
BIP39_ENTROPY_LEN_128 = wallycore.BIP39_ENTROPY_LEN_128
BIP39_ENTROPY_LEN_160 = wallycore.BIP39_ENTROPY_LEN_160
BIP39_ENTROPY_LEN_192 = wallycore.BIP39_ENTROPY_LEN_192
BIP39_ENTROPY_LEN_224 = wallycore.BIP39_ENTROPY_LEN_224
BIP39_ENTROPY_LEN_256 = wallycore.BIP39_ENTROPY_LEN_256
class Mnemonic(object):
def __init__(self, language):
self.wordlist = wallycore.bip39_get_wordlist(language)
@staticmethod
def list_languages():
return wallycore.bip39_get_languages().split()
def generate(self, strength = wallycore.BIP39_ENTROPY_LEN_128):
from os import urandom
return self.to_mnemonic(bytearray(urandom(strength)))
def to_entropy(self, words):
if isinstance(words, list):
words = ' '.join(words)
buf = bytearray(BIP39_ENTROPY_LEN_256)
length = wallycore.bip39_mnemonic_to_bytes(self.wordlist, words, buf)
return bytearray(buf)[0:length]
def to_mnemonic(self, data):
return wallycore.bip39_mnemonic_from_bytes(self.wordlist, data)
def check(self, mnemonic):
wallycore.bip39_mnemonic_validate(self.wordlist, mnemonic)
def to_seed(self, mnemonic, passphrase = ''):
buf = bytearray(wallycore.BIP39_SEED_LEN_512)
wallycore.bip39_mnemonic_to_seed(mnemonic, passphrase, buf)
return buf
if __name__ == "__main__":
# Just make sure the basics work
for lang in Mnemonic.list_languages():
m = Mnemonic(lang)
phrase = m.generate()
m.check(phrase)
try:
m.generate(BIP39_ENTROPY_LEN_256 - 1)
assert False
except:
pass
try:
m.check(phrase + ' foo')
assert False
except:
pass
assert m.to_entropy(phrase) == m.to_entropy(phrase.split())
assert m.to_mnemonic(m.to_entropy(phrase)) == phrase
assert m.to_seed(phrase, 'foo') != m.to_seed(phrase, 'bar')

View File

@@ -0,0 +1,65 @@
# Support functions for the SWIG python wrapper
def _wrap_bin(fn, length, resize=False):
""" Wrap functions that take an output buffer to create/return it """
def wrapped(*args):
#assert type(args[0]) is not unicode
buf = bytearray(length(*args) if callable(length) else length)
ret = fn(*list(args)+[buf])
if resize:
return buf[0:ret] # Truncate buf to bytes written
return (ret, buf) if ret is not None else buf
return wrapped
_unwrapped_hex_to_bytes = hex_to_bytes
def hex_to_bytes(hex_str):
hex_str = hex_str.encode('ascii')
buf = bytearray(len(hex_str) / 2)
_unwrapped_hex_to_bytes(bytes(hex_str), buf)
return buf
sha256 = _wrap_bin(sha256, SHA256_LEN)
sha256d = _wrap_bin(sha256d, SHA256_LEN)
sha512 = _wrap_bin(sha512, SHA512_LEN)
hash160 = _wrap_bin(hash160, HASH160_LEN)
hmac_sha256 = _wrap_bin(hmac_sha256, HMAC_SHA256_LEN)
hmac_sha512 = _wrap_bin(hmac_sha512, HMAC_SHA512_LEN)
bip32_key_serialize = _wrap_bin(bip32_key_serialize, BIP32_SERIALIZED_LEN)
bip32_key_get_chain_code = _wrap_bin(bip32_key_get_chain_code, 32)
bip32_key_get_priv_key = _wrap_bin(bip32_key_get_priv_key, 32)
bip32_key_get_pub_key = _wrap_bin(bip32_key_get_pub_key, 33)
bip32_key_get_parent160 = _wrap_bin(bip32_key_get_parent160, HASH160_LEN)
bip32_key_get_hash160 = _wrap_bin(bip32_key_get_hash160, HASH160_LEN)
bip38_raw_from_private_key = _wrap_bin(bip38_raw_from_private_key, BIP38_SERIALIZED_LEN)
bip38_raw_to_private_key = _wrap_bin(bip38_raw_to_private_key, 32);
bip38_to_private_key = _wrap_bin(bip38_raw_to_private_key, 32);
bip39_mnemonic_to_seed512 = _wrap_bin(bip39_mnemonic_to_seed, BIP39_SEED_LEN_512)
pbkdf2_hmac_sha256 = _wrap_bin(pbkdf2_hmac_sha256, PBKDF2_HMAC_SHA256_LEN)
pbkdf2_hmac_sha512 = _wrap_bin(pbkdf2_hmac_sha512, PBKDF2_HMAC_SHA512_LEN)
ec_public_key_decompress = _wrap_bin(ec_public_key_decompress, EC_PUBLIC_KEY_UNCOMPRESSED_LEN)
ec_public_key_from_private_key = _wrap_bin(ec_public_key_from_private_key, EC_PUBLIC_KEY_LEN)
ec_sig_from_bytes = _wrap_bin(ec_sig_from_bytes, EC_SIGNATURE_LEN)
ec_sig_from_der = _wrap_bin(ec_sig_from_der, EC_SIGNATURE_LEN)
ec_sig_normalize = _wrap_bin(ec_sig_normalize, EC_SIGNATURE_LEN)
ec_sig_to_der = _wrap_bin(ec_sig_to_der, EC_SIGNATURE_DER_MAX_LEN, resize=True)
def base58check_from_bytes(buf):
return base58_from_bytes(buf, BASE58_FLAG_CHECKSUM)
def _base58_len_fn(base58, flags):
return len(base58) + (4 if (flags & BASE58_FLAG_CHECKSUM) else 0)
base58_to_bytes = _wrap_bin(base58_to_bytes, _base58_len_fn, resize=True)
def base58check_to_bytes(base58):
return base58_to_bytes(base58, BASE58_FLAG_CHECKSUM)
def _format_bitcoin_message_len_fn(msg, flags):
if flags & BITCOIN_MESSAGE_FLAG_HASH:
return SHA256_LEN
msg_len = len(msg)
return 25 + msg_len + (1 if msg_len < 253 else 3)
format_bitcoin_message = _wrap_bin(format_bitcoin_message, _format_bitcoin_message_len_fn, resize=True)

View File

@@ -0,0 +1,188 @@
%module wallycore
%{
#define SWIG_FILE_WITH_INIT
#include <stdbool.h>
#include "../include/wally_core.h"
#include "../include/wally_bip32.h"
#include "bip32_int.h"
#include "../include/wally_bip38.h"
#include "../include/wally_bip39.h"
#include "../include/wally_crypto.h"
#include "../internal.h"
#undef malloc
#undef free
#define malloc(size) wally_malloc(size)
#define free(ptr) wally_free(ptr)
static int check_result(int result)
{
switch (result) {
case WALLY_OK:
break;
case WALLY_EINVAL:
PyErr_SetString(PyExc_ValueError, "Invalid argument");
break;
case WALLY_ENOMEM:
PyErr_SetString(PyExc_MemoryError, "Out of memory");
break;
default: /* WALLY_ERROR */
PyErr_SetString(PyExc_RuntimeError, "Failed");
break;
}
return result;
}
#define capsule_cast(obj, name) \
(struct name *)PyCapsule_GetPointer(obj, "struct " #name " *")
static void destroy_words(PyObject *obj) { (void)obj; }
static void destroy_ext_key(PyObject *obj) {
struct ext_key *contained = capsule_cast(obj, ext_key);
if (contained)
bip32_key_free(contained);
}
#define MAX_LOCAL_STACK 256u
%}
%include pybuffer.i
%include exception.i
/* Raise an exception whenever a function fails */
%exception{
$action
if (check_result(result))
SWIG_fail;
};
/* Return None if we didn't throw instead of 0 */
%typemap(out) int %{
Py_IncRef(Py_None);
$result = Py_None;
%}
%define %pybuffer_nullable_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE)
(int res, Py_ssize_t size = 0, const void *buf = 0) {
if ($input == Py_None)
$2 = 0;
else {
res = PyObject_AsReadBuffer($input, &buf, &size);
if (res<0) {
PyErr_Clear();
%argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
}
$1 = ($1_ltype) buf;
$2 = ($2_ltype) (size / sizeof($*1_type));
}
}
%enddef
/* Input buffers with lengths are passed as python buffers */
%pybuffer_nullable_binary(const unsigned char *bytes_in, size_t len_in);
%pybuffer_binary(const unsigned char *chain_code, size_t chain_code_len);
%pybuffer_nullable_binary(const unsigned char *hash160, size_t hash160_len);
%pybuffer_binary(const unsigned char *iv, size_t iv_len);
%pybuffer_binary(const unsigned char *key, size_t key_len);
%pybuffer_binary(const unsigned char *pass, size_t pass_len);
%pybuffer_nullable_binary(const unsigned char *parent160, size_t parent160_len);
%pybuffer_nullable_binary(const unsigned char *priv_key, size_t priv_key_len);
%pybuffer_nullable_binary(const unsigned char *pub_key, size_t pub_key_len);
%pybuffer_binary(const unsigned char *salt, size_t salt_len);
%pybuffer_binary(const unsigned char *sig_in, size_t sig_in_len);
%pybuffer_mutable_binary(unsigned char *bytes_out, size_t len);
%pybuffer_mutable_binary(unsigned char *bytes_in_out, size_t len);
%pybuffer_mutable_binary(unsigned char *salt_in_out, size_t salt_len);
/* Output integer values are converted into return values. */
%typemap(in, numinputs=0) size_t *written (size_t sz) {
sz = 0; $1 = ($1_ltype)&sz;
}
%typemap(argout) size_t* written {
Py_DecRef($result);
$result = PyInt_FromSize_t(*$1);
}
/* Output strings are converted to native python strings and returned */
%typemap(in, numinputs=0) char** (char* txt) {
txt = NULL;
$1 = ($1_ltype)&txt;
}
%typemap(argout) char** {
if (*$1 != NULL) {
Py_DecRef($result);
$result = PyString_FromString(*$1);
wally_free_string(*$1);
}
}
/* Opaque types are passed along as capsules */
%define %py_opaque_struct(NAME)
%typemap(in, numinputs=0) const struct NAME **output (struct NAME * w) {
w = 0; $1 = ($1_ltype)&w;
}
%typemap(argout) const struct NAME ** {
if (*$1 != NULL) {
Py_DecRef($result);
$result = PyCapsule_New(*$1, "struct NAME *", destroy_ ## NAME);
}
}
%typemap (in) const struct NAME * {
$1 = PyCapsule_GetPointer($input, "struct NAME *");
}
%enddef
/* Integer arrays */
%define %py_int_array(INTTYPE, INTMAX, PNAME, LNAME)
%typemap(in) (const INTTYPE *PNAME, size_t LNAME) (INTTYPE tmp_buf[MAX_LOCAL_STACK/sizeof(INTTYPE)]) {
size_t i;
if (!PyList_Check($input)) {
check_result(WALLY_EINVAL);
SWIG_fail;
}
$2 = PyList_Size($input);
$1 = tmp_buf;
if ($2 * sizeof(INTTYPE) > sizeof(tmp_buf))
if (!($1 = (INTTYPE *) wally_malloc(($2) * sizeof(INTTYPE)))) {
check_result(WALLY_ENOMEM);
SWIG_fail;
}
for (i = 0; i < $2; ++i) {
PyObject *item = PyList_GET_ITEM($input, i);
unsigned long long v;
if (!SWIG_IsOK(SWIG_AsVal_unsigned_SS_long_SS_long(item, &v)) || v > INTMAX) {
PyErr_SetString(PyExc_OverflowError, "Invalid unsigned integer");
SWIG_fail;
}
$1[i] = (INTTYPE)v;
}
}
%typemap(freearg) (const INTTYPE *PNAME, size_t LNAME) {
if ($1 && $1 != tmp_buf$argnum)
wally_free($1);
}
%enddef
%py_int_array(uint32_t, 0xffffffffull, child_num_in, child_num_len)
%py_int_array(uint64_t, 0xffffffffffffffffull, values, values_len)
%py_opaque_struct(words);
%py_opaque_struct(ext_key);
/* Tell SWIG what uint32_t/uint64_t mean */
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
%rename("bip32_key_from_parent") bip32_key_from_parent_alloc;
%rename("bip32_key_from_parent_path") bip32_key_from_parent_path_alloc;
%rename("bip32_key_from_seed") bip32_key_from_seed_alloc;
%rename("bip32_key_init") bip32_key_init_alloc;
%rename("bip32_key_unserialize") bip32_key_unserialize_alloc;
%rename("%(regex:/^wally_(.+)/\\1/)s", %$isfunction) "";
%include "../include/wally_core.h"
%include "../include/wally_bip32.h"
%include "bip32_int.h"
%include "../include/wally_bip38.h"
%include "../include/wally_bip39.h"
%include "../include/wally_crypto.h"

View File

@@ -0,0 +1 @@
https://github.com/jgriffiths/libwally-core