Add cli for importcontract as hex string, save to leveldb in /tmp with initiating txid as key

This commit is contained in:
Jay Graber 2017-07-03 09:48:05 -07:00
parent 158519ae0b
commit 9046d44f4d
4 changed files with 67 additions and 2 deletions

18
cli.py Normal file
View File

@ -0,0 +1,18 @@
import argparse, textwrap
from utils import *
import database as db
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description=textwrap.dedent('''\
== Contracts ==
importcontract "hexstr" - import an existing trade from a hex string
exportcontract - (not implemented) export the data of an existing xcat trade as a hex string
'''))
parser.add_argument("-importcontract", type=str, action="store", help="import an existing trade from a hex string.")
args = parser.parse_args()
if args.importcontract:
hexstr = args.importcontract
db.create(hexstr)

26
database.py Normal file
View File

@ -0,0 +1,26 @@
import plyvel
from utils import *
import binascii
import sys
db = plyvel.DB('/tmp/testdb', create_if_missing=True)
trade = get_trade()
## txid we retrieve by
txid = trade['sell']['fund_tx']
def hex2dict(hexstr):
jsonstr = x2s(hexstr)
return json.loads(jsonstr)
def create(hexstr):
trade = hex2dict(hexstr)
txid = trade['sell']['fund_tx']
# Save trade by initiating txid
db.put(b(txid), b(hexstr))
def get(txid):
return db.get(b(txid))
hexstr = get(txid)
print(x2s(hexstr))

View File

@ -3,9 +3,30 @@ import json
import random
import binascii
def hex2str(hexstring):
def b(string):
"""Convert a string to bytes"""
return str.encode(string)
def x(h):
"""Convert a hex string to bytes"""
return binascii.unhexlify(h.encode('utf8'))
def b2x(b):
"""Convert bytes to a hex string"""
return binascii.hexlify(b).decode('utf8')
def x2s(hexstring):
"""Convert hex to a utf-8 string"""
return binascii.unhexlify(hexstring).decode('utf-8')
def s2x(string):
"""Convert a utf-8 string to hex"""
return b2x(b(string))
######################
def sha256(secret):
preimage = secret.encode('utf8')
h = hashlib.sha256(preimage).digest()

View File

@ -117,7 +117,7 @@ def parse_secret(txid):
print("Decoded", scriptSig)
asm = scriptSig['asm'].split(" ")
pubkey = asm[1]
secret = hex2str(asm[2])
secret = x2s(asm[2])
redeemPubkey = P2PKHBitcoinAddress.from_pubkey(x(pubkey))
print('redeemPubkey', redeemPubkey)
print(secret)