forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libhsmd: First working build script for libhsmd-python
Changelog-Added: libhsmd: Added python bindings for `libhsmd`
- Loading branch information
1 parent
214fdcc
commit 7f15f86
Showing
10 changed files
with
5,324 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/make | ||
|
||
LIBHSMD_PY_GEN_FILES := contrib/libhsmd_python/swig_wrap.c \ | ||
contrib/libhsmd_python/libhsmd.py | ||
|
||
PYTHON_GENERATED += contrib/libhsmd_python/libhsmd.py | ||
|
||
# Swig by default generates stubs in the file's directory, which is | ||
# what we want. | ||
$(LIBHSMD_PY_GEN_FILES): contrib/libhsmd_python/swig.i $(HSMD_SRC) | ||
swig -python -builtin contrib/libhsmd_python/swig.i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# This file was automatically generated by SWIG (http://www.swig.org). | ||
# Version 3.0.12 | ||
# | ||
# Do not make changes to this file unless you know what you are doing--modify | ||
# the SWIG interface file instead. | ||
|
||
from sys import version_info as _swig_python_version_info | ||
if _swig_python_version_info >= (2, 7, 0): | ||
def swig_import_helper(): | ||
import importlib | ||
pkg = __name__.rpartition('.')[0] | ||
mname = '.'.join((pkg, '_libhsmd')).lstrip('.') | ||
try: | ||
return importlib.import_module(mname) | ||
except ImportError: | ||
return importlib.import_module('_libhsmd') | ||
_libhsmd = swig_import_helper() | ||
del swig_import_helper | ||
elif _swig_python_version_info >= (2, 6, 0): | ||
def swig_import_helper(): | ||
from os.path import dirname | ||
import imp | ||
fp = None | ||
try: | ||
fp, pathname, description = imp.find_module('_libhsmd', [dirname(__file__)]) | ||
except ImportError: | ||
import _libhsmd | ||
return _libhsmd | ||
try: | ||
_mod = imp.load_module('_libhsmd', fp, pathname, description) | ||
finally: | ||
if fp is not None: | ||
fp.close() | ||
return _mod | ||
_libhsmd = swig_import_helper() | ||
del swig_import_helper | ||
else: | ||
import _libhsmd | ||
# pull in all the attributes from _libhsmd | ||
if __name__.rpartition('.')[0] != '': | ||
if _swig_python_version_info >= (2, 7, 0): | ||
try: | ||
from ._libhsmd import * | ||
except ImportError: | ||
from _libhsmd import * | ||
else: | ||
from _libhsmd import * | ||
else: | ||
from _libhsmd import * | ||
del _swig_python_version_info | ||
|
||
try: | ||
_swig_property = property | ||
except NameError: | ||
pass # Python < 2.2 doesn't have 'property'. | ||
|
||
try: | ||
import builtins as __builtin__ | ||
except ImportError: | ||
import __builtin__ | ||
|
||
def _swig_setattr_nondynamic(self, class_type, name, value, static=1): | ||
if (name == "thisown"): | ||
return self.this.own(value) | ||
if (name == "this"): | ||
if type(value).__name__ == 'SwigPyObject': | ||
self.__dict__[name] = value | ||
return | ||
method = class_type.__swig_setmethods__.get(name, None) | ||
if method: | ||
return method(self, value) | ||
if (not static): | ||
if _newclass: | ||
object.__setattr__(self, name, value) | ||
else: | ||
self.__dict__[name] = value | ||
else: | ||
raise AttributeError("You cannot add attributes to %s" % self) | ||
|
||
|
||
def _swig_setattr(self, class_type, name, value): | ||
return _swig_setattr_nondynamic(self, class_type, name, value, 0) | ||
|
||
|
||
def _swig_getattr(self, class_type, name): | ||
if (name == "thisown"): | ||
return self.this.own() | ||
method = class_type.__swig_getmethods__.get(name, None) | ||
if method: | ||
return method(self) | ||
raise AttributeError("'%s' object has no attribute '%s'" % (class_type.__name__, name)) | ||
|
||
|
||
def _swig_repr(self): | ||
try: | ||
strthis = "proxy of " + self.this.__repr__() | ||
except __builtin__.Exception: | ||
strthis = "" | ||
return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,) | ||
|
||
try: | ||
_object = object | ||
_newclass = 1 | ||
except __builtin__.Exception: | ||
class _object: | ||
pass | ||
_newclass = 0 | ||
|
||
# This file is compatible with both classic and new-style classes. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <ccan/str/hex/hex.h> | ||
#include <libhsmd_python.h> | ||
|
||
char *init(char *hex_hsm_secret, char *network_name) { | ||
const struct bip32_key_version *key_version; | ||
struct secret sec; | ||
u8 *response; | ||
setup_locale(); | ||
if (sodium_init() == -1) { | ||
fprintf( | ||
stderr, | ||
"Could not initialize libsodium. Maybe not enough entropy" | ||
" available ?"); | ||
return NULL; | ||
} | ||
|
||
wally_init(0); | ||
secp256k1_ctx = wally_get_secp_context(); | ||
|
||
sodium_mlock(&sec, sizeof(sec)); | ||
if (!hex_decode(hex_hsm_secret, strlen(hex_hsm_secret), sec.data, | ||
sizeof(sec.data))) { | ||
fprintf(stderr, | ||
"Expected hex_hsm_secret of length 64, got %zu\n", | ||
strlen(hex_hsm_secret)); | ||
return NULL; | ||
} | ||
|
||
/* Look up chainparams by their name */ | ||
chainparams = chainparams_for_network(network_name); | ||
if (chainparams == NULL) { | ||
fprintf(stderr, "Could not find chainparams for network %s\n", | ||
network_name); | ||
return NULL; | ||
} | ||
|
||
key_version = &chainparams->bip32_key_version; | ||
|
||
response = hsmd_init(sec, *key_version); | ||
sodium_munlock(&sec, sizeof(sec)); | ||
|
||
char *res = tal_hex(NULL, response); | ||
tal_free(response); | ||
return res; | ||
} | ||
|
||
char *handle(long long cap, long long dbid, char *peer_id, char *hexmsg) { | ||
const tal_t *ctx = tal_arr(NULL, u8, 0); | ||
size_t res_len; | ||
u8 *response, *request = tal_hexdata(ctx, hexmsg, strlen(hexmsg)); | ||
char *res; | ||
struct hsmd_client *client; | ||
struct node_id *peer = NULL; | ||
printf("%llu: %s\n", cap, hexmsg); | ||
if (peer_id != NULL) { | ||
peer = tal(ctx, struct node_id); | ||
node_id_from_hexstr(hexmsg, strlen(hexmsg), peer); | ||
client = hsmd_client_new_peer(ctx, cap, dbid, peer, NULL); | ||
} else { | ||
client = hsmd_client_new_main(ctx, cap, NULL); | ||
} | ||
response = hsmd_handle_client_message(NULL, client, request); | ||
printf("%s\n", tal_hex(ctx, response)); | ||
if (response == NULL) | ||
return tal_free(ctx); | ||
|
||
res = tal_hex(NULL, response); | ||
res_len = hex_str_size(tal_bytelen(response)); | ||
res = malloc(res_len); | ||
hex_encode(response, tal_bytelen(response), res, res_len); | ||
|
||
tal_free(ctx); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef LIGHTNING_CONTRIB_LIBHSMD_PYTHON_LIBHSMD_PYTHON_H | ||
#define LIGHTNING_CONTRIB_LIBHSMD_PYTHON_LIBHSMD_PYTHON_H | ||
|
||
#include <hsmd/libhsmd.h> | ||
char *handle(long long cap, long long dbid, char *peer_id, char *msg); | ||
char *init(char *hex_hsm_secret, char *network_name); | ||
|
||
#endif /* LIGHTNING_CONTRIB_LIBHSMD_PYTHON_LIBHSMD_PYTHON_H */ |
Oops, something went wrong.