Add cli for importcontract as hex string, save to leveldb in /tmp with initiating txid as key
This commit is contained in:
parent
158519ae0b
commit
9046d44f4d
|
@ -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)
|
|
@ -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))
|
23
utils.py
23
utils.py
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue