def create_mnb(mne): collat_outpoint = Transaction.COutPoint(serialize.hs2b(mne['txid']), mne['nout']) mnp = Masternode.CMasternodePing(collat_outpoint) mn_privkey = energi.decode_address(mne['mn_privkey']) block_hash = Masternode.get_block_hash(13) mnp.sign(block_hash, mn_privkey) mnb = Masternode.CMasternodeBroadcast() mnb.outpoint = collat_outpoint mnb.addr = parse_ipport(mne['ip']) mn_private_key = secp256k1.PrivateKey(mn_privkey, raw = True) mn_public_key = mn_private_key.pubkey.serialize(compressed = False) co_ae = walletdb.get_addr_txid(mne['txid'], mne['nout']) if co_ae is None: raise RuntimeError('cannot find address for txid: %s' % mne['txid']) co_public_key = co_ae['pubkey'] mnb.pubkey_collateral = energi.compress_public_key(co_public_key) mnb.pubkey_masternode = mn_public_key mnb.last_ping = mnp print('Signing on ledger:') mnb.hw_sign(energi.serialize_pathd(co_ae)) print('MasternodeBroadcast:\n%s' % mnb) return mnb
def main(): if len(sys.argv) != 5: print( 'usage: %s <from> <to> <send value sats; negative to send everything> <max num inputs>' % basename(sys.argv[0])) sys.exit(0) fr = s2b(sys.argv[1]) to = sys.argv[2] val = int(sys.argv[3]) max_in = int(sys.argv[4]) if not energi.check_address(to): raise RuntimeError('bad to address: %s' % to) if val < 0: val = None print('Sending ALL NRG to %s.' % (to)) else: print('Sending %f NRG to %s.' % (val / _NRGSAT, to)) addr_d = walletdb.get_address_d(with_change=True) addr_d = {fr: addr_d[fr], 'change': addr_d['change']} bal = balance(addr_d) print('\nCurrent balance: %f NRG. (%d Sat)' % (bal / _NRGSAT, bal)) print('Removing locked outputs.') count = 0 for a in addr_d.keys(): nul = [] for u in addr_d[a].get('utxos', []): if not walletdb.is_locked_txid(u['txid'], u['nout']) and count < max_in: nul.append(u) count += 1 addr_d[a]['utxos'] = nul bal = balance(addr_d) print('\nAvailable balance: %f NRG. (%d Sat)' % (bal / _NRGSAT, bal)) print('Creating transaction.') used_inputs = [] tx = Transaction.create_tx(to, val, addr_d, fee_minimum=4096, used_inputs=used_inputs) print('\n\nTransaction: (%d) %s' % (len(tx), serialize.b2hs(tx))) sys.stdout.write('\nVerifying transaction with energid... ') sys.stdout.flush() print(eel.decode_raw_transaction(serialize.b2hs(tx))) input('\n\nVerified. Press <enter> to transmit ^C to cancel. ') txid = eel.send_raw_transaction(serialize.b2hs(tx)) print('Transmitted. Txid = %s' % (serialize.b2s(txid))) for u in used_inputs: walletdb.remove_unspent_db(u['txid'], u['nout'])
def deserialize(self, m): self.masternode_output = Transaction.COutPoint().deserialize(m) self.block_hash = serialize.deser_uint256(m) self.sig_time = serialize.deser_int64(m) self.vchSig = serialize.deser_string(m) self.fSentinelIsCurrent = serialize.deser_bool(m) self.nSentinelVersion = serialize.deser_uint32(m) self.nDaemonVersion = serialize.deser_uint32(m) return self
def __init__(self, mn_outpoint=None): self.mn_outpoint = mn_outpoint if mn_outpoint is not None else Transaction.COutPoint( ) self.block_hash = b'\x00' * 32 self.sig_time = -1 self.vchSig = b'' self.fSentinelIsCurrent = True self.nSentinelVersion = 0x010101 self.nDaemonVersion = 2020100
def deserialize(self, m): self.outpoint = Transaction.COutPoint().deserialize(m) self.addr = CService().deserialize(m) self.pubkey_collateral = serialize.deser_string(m) self.pubkey_masternode = serialize.deser_string(m) self.vchSig = serialize.deser_string(m) self.sig_time = serialize.deser_int64(m) self.n_protocol_version = serialize.deser_int32(m) self.last_ping = CMasternodePing().deserialize(m) return self
def verify_sig(msg_b, sig_s, raw=True): hash_b = msg_b if raw else Transaction.hash256(msg_b) recid = (sig_s[0] - 27) & 0x3 sig_b = sig_s[1:] empty = secp256k1.PublicKey(raw=True, flags=secp256k1.ALL_FLAGS) sig = empty.ecdsa_recoverable_deserialize(sig_b, recid) pubkey_r = empty.ecdsa_recover(hash_b, sig, raw=True) pubkey = secp256k1.PublicKey(pubkey_r, flags=secp256k1.ALL_FLAGS) new_sig = pubkey.ecdsa_recoverable_convert(sig) return pubkey.ecdsa_verify(hash_b, new_sig, raw=True)
def __init__(self): self.n_active_state = 0 # int nActiveState; self.n_protocol_version = _PROTOCOL_VERSION # int nProtocolVersion; self.sig_time = 0 # int64_t sigTime; // mnb message time self.outpoint = Transaction.COutPoint() # COutPoint outpoint; self.addr = CService() # CService addr; self.pubkey_collateral = b'' # CPubKey pubKeyCollateralAddress; (basically just a serialized pubkey) self.pubkey_masternode = b'' # CPubKey pubKeyMasternode; self.n_last_dsq = 0 # dsq count from last dsq broadcast # int64_t nLastDsq; self.n_time_last_checked = 0 # int64_t nTimeLastChecked; self.n_time_last_paid = 0 # int64_t nTimeLastPaid; self.f_info_valid = False # bool fInfoValid;
def sign(privkey_b, msg_b, raw=True): hash_b = msg_b if raw else Transaction.hash256(msg_b) private_key = secp256k1.PrivateKey(privkey=privkey_b, raw=True) signature = private_key.ecdsa_sign_recoverable(hash_b, raw=True) sig, recid = private_key.ecdsa_recoverable_serialize(signature) is_compressed = False r = bytes([27 + recid + (4 if is_compressed else 0)]) + sig if not verify_sig(hash_b, r): raise RuntimeError('could not verify signature') return r
def main(): if len(sys.argv) != 3: print('usage: %s <to> <send value sats; negative to send everything>' % basename(sys.argv[0])) sys.exit(0) to = sys.argv[1] val = int(sys.argv[2]) if not energi.check_address(to): raise RuntimeError('bad to address: %s' % to) if val < 0: val = None print('Sending ALL NRG to %s.' % (to)) else: print('Sending %f NRG to %s.' % (val / _NRGSAT, to)) addr_d = walletdb.get_address_d(with_change=True) bal = walletdb.get_balance() print('\nCurrent balance: %f NRG.' % (bal / _NRGSAT)) print('Removing locked outputs.') for a in addr_d: nul = [] for u in addr_d[a].get('utxos', []): if not walletdb.is_locked_txid(u['txid'], u['nout']): nul.append(u) else: print('removing locked: %s' % u) addr_d[a]['utxos'] = nul sys.stdout.write('Creating transaction; confirm on ledger: ') sys.stdout.flush() tx = Transaction.create_tx(to, val, addr_d) print('\n\nTransaction: %s' % serialize.b2hs(tx)) sys.stdout.write('\nVerifying transaction with energid... ') sys.stdout.flush() print(eel.decode_raw_transaction(serialize.b2hs(tx))) input('\n\nVerified. Press <enter> to transmit ^C to cancel. ') txid = eel.send_raw_transaction(serialize.b2hs(tx)) print('Transmitted. Txid = %s' % (serialize.b2s(txid)))
def deserialize(self, m): self.outpoint = Transaction.COutPoint().deserialize(m) self.addr = CService().deserialize(m) self.pubkey_collateral = serialize.deser_string(m) self.pubkey_masternode = serialize.deser_string(m) self.last_ping = CMasternodePing().deserialize(m) self.vchSig = serialize.deser_string(m) self.sig_time = serialize.deser_int64(m) self.n_last_dsq = serialize.deser_int64(m) self.n_time_last_checked = serialize.deser_int64(m) self.n_last_dsq = serialize.deser_int64(m) self.n_active_state = serialize.deser_int32(m) self.n_collateral_hash = serialize.deser_uint256(m) self.n_block_last_paid = serialize.deser_int32(m) self.n_protocol_version = serialize.deser_int32(m) self.n_po_se_ban_score = serialize.deser_int32(m) self.n_po_se_ban_height = serialize.deser_int32(m) self.f_allow_mixing_tx = serialize.deser_bool(m) self.f_unit_test = serialize.deser_bool(m) self.gov_votes = serialize.deser_map_uint256_int(m) return self
def main(): if len(sys.argv) < 4: print( 'usage: %s <to> <send value sats; negative to send everything> <txid:nout> [txid:nout [...]]' % basename(sys.argv[0])) sys.exit(0) to = sys.argv[1] val = int(sys.argv[2]) utxol = [{ 'txid': y[0], 'nout': int(y[1]) } for y in [x.split(':') for x in sys.argv[3:]]] if len(utxol) < 1: raise RuntimeError('must include at least 1 utxo') if not energi.check_address(to): raise RuntimeError('bad address "%s"' % to) if val < 0: val = None print('Sending ALL NRG to %s.' % (to)) else: print('Sending %f NRG to %s.' % (val / _NRGSAT, to)) addr_d = walletdb.get_address_d(with_change=True) print('Searching for utxo%s in walletdb...' % ('s' if len(utxol) > 1 else '')) for addr in addr_d: if 'utxos' in addr_d[addr]: nutxos = [] for u in addr_d[addr]['utxos']: for iu in utxol: if u['txid'] == iu['txid'] and u['nout'] == iu['nout']: nutxos.append(u) break addr_d[addr]['utxos'] = nutxos bal = balance(addr_d) print('\nCurrent balance: %f NRG. (%d Sat)' % (bal / _NRGSAT, bal)) print('Unlocking given inputs: %s' % utxol) input('Press <enter> to unlock, ^C to cancel') for addr in addr_d: if 'utxos' in addr_d[addr]: for u in addr_d[addr]['utxos']: print('unlocking: %s:%d' % (u['txid'], u['nout'])) walletdb.unlock_txid(u['txid'], u['nout']) print('Removing locked outputs.') count = 0 for a in addr_d.keys(): nul = [] for u in addr_d[a].get('utxos', []): if not walletdb.is_locked_txid(u['txid'], u['nout']): nul.append(u) count += 1 addr_d[a]['utxos'] = nul bal = balance(addr_d) print('\nAvailable balance: %f NRG. (%d Sat)' % (bal / _NRGSAT, bal)) print('Creating transaction.') used_inputs = [] tx = Transaction.create_tx(to, val, addr_d, fee_minimum=4096, used_inputs=used_inputs) print('\n\nTransaction: (%d) %s' % (len(tx), serialize.b2hs(tx))) sys.stdout.write('\nVerifying transaction with energid... ') sys.stdout.flush() print(eel.decode_raw_transaction(serialize.b2hs(tx))) input('\n\nVerified. Press <enter> to transmit ^C to cancel. ') txid = eel.send_raw_transaction(serialize.b2hs(tx)) print('Transmitted. Txid = %s' % (serialize.b2s(txid))) for u in used_inputs: walletdb.remove_unspent_db(u['txid'], u['nout'])
def get_hash(self): self.hash = Transaction.hash256(self.serialize(True)) return self.hash