def block_create(key, previous, representative, balance, link): nb = state_block() nb['account'] = account_get( ed25519_blake2b.publickey(bytes.fromhex(key)).hex()) nb['previous'] = previous nb['representative'] = representative nb['balance'] = balance nb['link'] = link nb['work'] = work_generate(block_hash(nb)) nb['signature'] = sign(key, block=nb) return nb
def key_expand(key): """Derive public key and account number from private key :param str key: 64 hex-char private key :return: (private key, public key, account number) :rtype: tuple :raise AssertionError: for invalid key """ assert len(key) == 64 pk = ed25519_blake2b.publickey(bytes.fromhex(key)).hex() return key, pk, account_get(pk)
def sign(key, block=None, _hash=None, msg=None, account=None, pk=None): sk = bytes.fromhex(key) if msg: m = msg.encode() elif _hash: m = bytes.fromhex(_hash) elif block: m = bytes.fromhex(block_hash(block)) else: return None if not pk: if account: pk = bytes.fromhex(account_key(account)) elif block: pk = bytes.fromhex(account_key(block['account'])) else: pk = ed25519_blake2b.publickey(sk) else: pk = bytes.fromhex(pk) if ed25519_blake2b_c: return ed25519_blake2b.signature(m, os.urandom(32), sk, pk).hex() else: return ed25519_blake2b.signature(m, sk, pk).hex()
def block_create(key, previous, representative, balance, link): """Create a block :param str key: 64 hex-char private key :param str previous: 64 hex-char previous hash :param str representative: representative address :param str balance: balance in raw :param str link: 64 hex-char link :return: a block with work and signature :rtype: dict """ nb = state_block() nb["account"] = account_get( ed25519_blake2b.publickey(bytes.fromhex(key)).hex()) nb["previous"] = previous nb["representative"] = representative nb["balance"] = balance nb["link"] = link nb["work"] = work_generate(block_hash(nb)) nb["signature"] = sign(key, block=nb) return nb
def sign(key, block=None, _hash=None, msg=None, account=None, pk=None): """Sign a block, hash, or message :param str key: 64 hex-char private key :param dict block: "account", "previous", "representative", "balance", and "link" are the required entries :param str _hash: 64 hex-char hash. Overrides ``block``. :param str msg: message to sign. Overrides ``_hash`` and ``block``. :param str account: account :param str pk: 64 hex-char public key :return: 128 hex-char signature :rtype: str """ sk = bytes.fromhex(key) if msg: m = msg.encode() elif _hash: m = bytes.fromhex(_hash) elif block: m = bytes.fromhex(block_hash(block)) else: return None if not pk: if account: pk = bytes.fromhex(account_key(account)) elif block: pk = bytes.fromhex(account_key(block["account"])) else: pk = ed25519_blake2b.publickey(sk) else: pk = bytes.fromhex(pk) if ed25519_blake2b_c: return ed25519_blake2b.signature(m, os.urandom(32), sk, pk).hex() else: return ed25519_blake2b.signature(m, sk, pk).hex()
def key_expand(key): pk = ed25519_blake2b.publickey(bytes.fromhex(key)).hex() return key, pk, account_get(pk)
import os, timeit import nanopy as npy from nanopy.rpc import RPC from nanopy.ed25519_blake2b import checkvalid, publickey # signature tk = "0000000000000000000000000000000000000000000000000000000000000000" m = "test" assert (checkvalid( bytes.fromhex(npy.sign(tk, _hash=None, msg=m)), m.encode(), publickey(bytes.fromhex(tk)), ) == 0) # work computation assert "fffffe0000000000" == npy.from_multiplier(1 / 8) assert "fffffff800000000" == npy.from_multiplier(8) assert 0.125 == npy.to_multiplier("fffffe0000000000") assert 8.0 == npy.to_multiplier("fffffff800000000") h = os.urandom(32).hex() w = npy.work_generate(h, multiplier=1 / 8) print(w) print(npy.work_validate(w, h)) assert npy.work_validate(w, h, multiplier=1 / 8) # n = 20 # print(timeit.timeit("npy.work_generate('0feb848ce9637cbc3b41e0334ecef8cf76350f689604a85bae5a2768891ac6e9', multiplier=1/8)", setup="import nanopy as npy", number=n,)/n)