Created
June 20, 2018 12:15
-
-
Save ssadler/57d82e9407bb4e80ead97e20e075f965 to your computer and use it in GitHub Desktop.
komodo_cross_chain_migrate.py
This file contains hidden or 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
import sys | |
import subprocess | |
import json | |
# create an output with some funds to send across chain | |
# komodo-cli -ac_name=TXSCL sendtoaddress $inputaddress 2 | |
inputaddress = "RFw7byY4xZpZCrtkMk3nFuuG1NTs9rSGgQ" | |
amount = 1.9 | |
source_chain = 'TXSCL' | |
target_chain = 'TXSCL000' | |
def cmd(*args): | |
print >>sys.stderr, ('\033[90m> %s\033[0m' % ' '.join(args)) | |
p = subprocess.Popen(args, stdout=subprocess.PIPE) | |
assert not p.wait() | |
return p.stdout.read() | |
privkey = cmd('komodo-cli', '-ac_name=' + source_chain, 'dumpprivkey', inputaddress).strip() | |
tx = {"inputs":[{ | |
"txid":"57d570663dd704dc3d46fcdb193f26095b984bb081e410d42de904fc40b3b0bb", | |
"idx":1, | |
"script":{"address":inputaddress}} | |
], | |
"outputs":[{ | |
"script":{"address":inputaddress}, | |
"amount": int(amount * 1e8) | |
}] | |
} | |
print >>sys.stderr, "sign and encode tx" | |
# Tx needs to be signed so it can be encoded | |
txsigned = cmd('hoek', 'signTxBitcoin', json.dumps({"tx":tx, "privateKeys":[privkey]})) | |
# Get tx as hex | |
txraw = json.loads(cmd('hoek', 'encodeTx', txsigned))['hex'] | |
print >>sys.stderr, "convert tx to export and re-sign" | |
# Convert tx to an export, this encodes the transaction's outputs in an OP_RETURN, as well as | |
# a merkle tree leading to the MoM | |
exportData = json.loads(cmd('komodo-cli', '-ac_name=' + source_chain, 'migrate_converttoexport', txraw, target_chain, str(amount))) | |
# Now need to re-sign the burn tx, so copy the OP_RETURN output into our unsigned tx, sign and encode | |
exportTx = tx.copy() | |
exportTx['outputs'] = json.loads(cmd('hoek', 'decodeTx', json.dumps({"hex":exportData['exportTx']})))['outputs'] | |
exportTxSigned = cmd('hoek', 'signTxBitcoin', json.dumps({"tx":exportTx, "privateKeys":[privkey]})) | |
exportTxRawData = json.loads(cmd('hoek', 'encodeTx', exportTxSigned)) | |
print >>sys.stderr, "broadcasting burn" | |
try: | |
burnTxid = cmd("komodo-cli", '-ac_name=' + source_chain, 'sendrawtransaction', exportTxRawData['hex']) | |
except AssertionError as e: | |
burnTxid = exportTxRawData['txid'] | |
print >>sys.stderr, "burn txid:", burnTxid | |
print >>sys.stderr, "createimporttransaction" | |
importTxPart = cmd("komodo-cli", '-ac_name=' + source_chain, "migrate_createimporttransaction", exportTxRawData['hex'], exportData['payouts']).strip() | |
print >>sys.stderr, "completeimporttransaction, this requires two notarisations after the tx in order to succeed" | |
importTx = cmd("komodo-cli", "migrate_completeimporttransaction", importTxPart) | |
print importTx | |
print >>sys.stderr, "broadcast the above on target chain using sendrawtransaction" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment