Skip to content

Instantly share code, notes, and snippets.

@evd0kim
Last active September 8, 2019 15:50
Show Gist options
  • Save evd0kim/9af6ed6f96b223772783d00b312bd7fa to your computer and use it in GitHub Desktop.
Save evd0kim/9af6ed6f96b223772783d00b312bd7fa to your computer and use it in GitHub Desktop.
libwally message signing using extended key
import os
import base64
import wallycore as w
h2b = w.hex_to_bytes
b2h = w.hex_from_bytes
mnemonic = "come fury oil antique match off jar ship six feel fall inflict path race impact monitor loan math goose shop autumn area icon barely"
written, seed = w.bip39_mnemonic_to_seed512(mnemonic, None)
assert written == w.BIP39_SEED_LEN_512
FLAG_KEY_PRIVATE, FLAG_KEY_PUBLIC, FLAG_SKIP_HASH, = 0x0, 0x1, 0x0
root_key = w.bip32_key_from_seed(seed, w.BIP32_VER_MAIN_PRIVATE, FLAG_SKIP_HASH)
priv_serialized = w.bip32_key_serialize(root_key, FLAG_KEY_PRIVATE)
ec_key_hex = w.hex_from_bytes(priv_serialized)[91:155]
message = b'hello, world'
ec_key = w.hex_to_bytes(ec_key_hex)
try:
res = w.ec_private_key_verify(ec_key)
print("Checking EC key: OK")
except ValueError:
print("Checking EC key: ERROR")
ec_pub = w.ec_public_key_from_private_key(ec_key)
message = b'hello, world'
formatted = w.format_bitcoin_message(message, 1)
#formatted = bytearray(32)
signature = w.ec_sig_from_bytes(ec_key, formatted, w.EC_FLAG_ECDSA)
try:
res = w.ec_sig_verify(ec_pub, formatted, w.EC_FLAG_ECDSA, signature)
print("Checking signature: OK")
except ValueError:
print("Checking signature: ERROR")
# changed code from here https://github.com/afilini/wally-examples/blob/master/SignMessage.ipynb
# by Valerio Vaccaro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment