Skip to content

Instantly share code, notes, and snippets.

@Nazgolze
Last active December 4, 2018 15:30
Show Gist options
  • Save Nazgolze/0ec761523ac769ed1d11b92ecc559dd4 to your computer and use it in GitHub Desktop.
Save Nazgolze/0ec761523ac769ed1d11b92ecc559dd4 to your computer and use it in GitHub Desktop.
#!/usr/bin/python2
import hashlib
import ed25519ll
import struct
import json
import urllib2
import time
LISK_EPOCH = 1464066000
passphrase = 'Your 12 word passphrase here'
recipient_id = "54834883818072291L" # Put in the real recipient
def create_transaction(recipient_id, passphrase, amount, timestamp):
# create ed25519 seed from passphrase (which ends up being the private key)
seed = hashlib.sha256(passphrase).digest()
# create key pair
keypair = ed25519ll.crypto_sign_keypair(seed=seed)
# this dictionary is used to create the json object
transaction = {};
# strip off L, convert passed in recipient_id into an integer, then make it big endian - for use in byte array
recipient_id_temp = struct.pack('>q', int(recipient_id[:len(recipient_id) - 1]))
transaction["amount"] = str(amount) # string
transaction["recipientId"] = recipient_id # string
transaction["senderPublicKey"] = keypair.vk.encode('hex') # hex string
transaction["timestamp"] = timestamp # int
transaction["type"] = 0 # int
transaction["fee"] = "10000000" # string
# create a byte array / unpadded struct with the dictionary members - order matters
transaction_bytes = struct.pack('=bi32s' + str(len(recipient_id_temp)) + 'sq', transaction["type"], transaction["timestamp"], keypair.vk, recipient_id_temp, int(transaction["amount"]))
# hash the byte array
transaction_hash = hashlib.sha256(transaction_bytes).digest()
# sign transaction
signed = ed25519ll.crypto_sign(transaction_hash, keypair.sk)
signed = signed[:64] # ed25519 signatures are 512 bits in length
transaction["signature"] = signed.encode('hex')
# add signature to byte array
transaction_bytes = struct.pack('=bi32s' + str(len(recipient_id_temp)) + 'sq64s', transaction["type"], transaction["timestamp"], keypair.vk, recipient_id_temp, int(transaction["amount"]), signed)
# hash with signature - to use when creating transaction id
transaction_hash = hashlib.sha256(transaction_bytes).digest()
# create transaction id
transaction["id"] = [s for s in transaction_hash[:8]]
transaction["id"].reverse()
transaction["id"] = str(int("".join(transaction["id"]).encode('hex'), 16))
transaction["asset"] = {} # needed for json
# return transaction json
return json.dumps(transaction)
t = int(time.time()) - LISK_EPOCH
transaction = create_transaction(recipient_id, passphrase, 1, t)
transaction = transaction.strip()
print transaction
# post transaction
req = urllib2.Request("http://testnet.lisk.io:7000/api/transactions", transaction, {'Content-Type': 'application/json', 'Content-Length': len(transaction)})
try:
f = urllib2.urlopen(req)
response = f.read()
f.close()
print response
except Exception as e:
print e.msg
print e.hdrs
print e.geturl()
print e.readlines()
print dir(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment