def init(self): if self.getIndexBlockByHash(self.CHAIN_HEAD_INDEX) == None: genesisBlockGasLimit = Config.getIntValue("GENESIS_BLOCK_GAS_LIMIT") genesisBlockGasUsed = Config.getIntValue("GENESIS_BLOCK_GAS_USED") genesisBlockGasPrice = Config.getIntValue("GENESIS_BLOCK_GAS_PRICE") genesisBlock = Block() genesisBlock.previousHash = Config.getBytesValue("GENESIS_BLOCK_PREVIOUS_HASH", False) genesisBlock.gasLimit = genesisBlockGasLimit genesisBlock.gasUsed = genesisBlockGasUsed genesisBlock.nonce = Config.getIntValue("GENESIS_BLOCK_NONCE") genesisBlock.bits = Config.getIntValue("GENESIS_BLOCK_DIFFICULTY_BITS", 16) genesisBlock.timestamp = Config.getIntValue("GENESIS_BLOCK_TIMESTAMP") transaction = Transaction(genesisBlockGasLimit, genesisBlockGasPrice) coinbaseData = [] coinbaseData.append(Config.getValue("GENESIS_BLOCK_COINBASE_DATA")) transaction.addCoinbaseInput(coinbaseData) genesisBlockRewards = Config.getDecimalValue("GENESIS_BLOCK_REWARDS") genesisBlockRewards = Units.toUnits(genesisBlockRewards) genesisBlockPublicAddress = Config.getValue("GENESIS_BLOCK_PUBLIC_ADDRESS") genesisBlockPublicAddress = Address.toAddressBytes(genesisBlockPublicAddress) genesisBlockScript = Script.verifySignature() transaction.addOutput(genesisBlockPublicAddress, genesisBlockScript, genesisBlockRewards) transaction.hash() genesisBlock.transactions.append(transaction) genesisBlock.merkleRoot = MerkleTree.getMerkleRoot(genesisBlock.transactions, False) genesisBlock.witnessRoot = MerkleTree.getMerkleRoot(genesisBlock.transactions, True) self.addBlock(genesisBlock)
def add(self, callback = None): account = CoreAccount() Accounts.addAccount(account) address = Address.to0xAddress(account.address) callback( JSONRPC.createResultObject(address, self.id) )
def get(self, callback = None): accounts = [] # Account Types standard = [] multisig = [] atomicswap = [] for account in Accounts.getAccounts(): confirmedBalance = Accounts.getConfirmedBalanceByAddress(account.address) confirmedBalance = Units.toValue(confirmedBalance) confirmedBalance = DataType.asFloat(confirmedBalance) address = Address.to0xAddress(account.address) accountInfo = { 'address': address, 'type': account.type, 'balance': confirmedBalance } if account.type == AccountType.STANDARD: standard.append(accountInfo) elif account.type == AccountType.MULTISIGNATURE: multisig.append(accountInfo) elif account.type == AccountType.ATOMIC_SWAP: atomicswap.append(accountInfo) accounts.extend(standard) accounts.extend(multisig) accounts.extend(atomicswap) callback( JSONRPC.createResultObject(accounts, self.id) )
def serialize(self): return { 'address': Address.toAddressStr(self.address), 'public': DataType.toHex(self.public), 'private': DataType.toHex(self.private), 'type': self.type }
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) if self.validateParameters(): for sidechain in self.params: self.sidechains.append(Address.toAddressBytes(sidechain)) if self.validate(): return True return False
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) if self.validateParameters(): for contract in self.params: self.contracts.append(Address.toAddressBytes(contract)) if self.validate(): return True return False
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) keys = ['address', 'enabled'] if self.validateParameters(keys): self.address = Address.toAddressBytes(self.params['address']) self.enabled = self.params['enabled'] if self.validate(): return True return False
def get(self, callback = None): sidechains = [] for sidechain in Sidechains.getSidechains(): address = Address.to0xAddress(sidechain.address) sidechains.append({ 'address': address }) callback( JSONRPC.createResultObject(sidechains, self.id) )
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) keys = None if self.isMultiSig(): if self.isTxFromMultiSig(): keys = [ 'fromAddress', 'publicKeys', 'signatures', 'threshold', 'toAddress', 'value', 'gasLimit', 'gasPrice' ] else: keys = [ 'fromAddress', 'threshold', 'toAddress', 'value', 'gasLimit', 'gasPrice' ] else: keys = [ 'fromAddress', 'toAddress', 'value', 'gasLimit', 'gasPrice' ] if self.validateParameters(keys): self.fromAddress = Address.toAddressBytes( self.params['fromAddress']) if self.isMultiSig(): if self.isTxFromMultiSig(): self.publicKeys = [] _publicKeys = self.params['publicKeys'] for publicKey in _publicKeys: publicKey = DataType.fromHex(publicKey) self.publicKeys.append(publicKey) self.signatures = [] _signatures = self.params['signatures'] for signature in _signatures: signature = DataType.fromHex(signature) self.signatures.append(signature) self.threshold = self.params['threshold'] self.toAddress = Address.toAddressBytes( self.params['toAddress']) self.value = Units.toUnits(self.params['value']) self.gasLimit = self.params['gasLimit'] self.gasPrice = Units.toUnits(self.params['gasPrice']) if self.validate(): return True return False
def __init__(self, blockchain, account, enabled): Thread.__init__(self, name=Address.to0xAddress(account.address), daemon=True) self.blockchain = blockchain self.account = account self.enabled = enabled self.blkValidator = ValidatorFactory.getInstance(ValidatorType.BLOCK) self.MINING_KEY = 'MINING'
def createNewMultiSigAccountType(self, accountType, privateKey, callback = None): multiSigAddressBytes = bytearray() for publicKey in self.publicKeys: multiSigAddressBytes.extend(publicKey) multiSigAddress = Crypto.generateAddress(multiSigAddressBytes) account = CoreAccount(multiSigAddress, self.publicKeys, privateKey, accountType) Accounts.addAccount(account) multiSigAddress = Address.to0xAddress(multiSigAddress) callback( JSONRPC.createResultObject(multiSigAddress, self.id) )
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) keys = None if self.isSignAtomicSwapTx(): keys = [] else: keys = ['fromAddress', 'threshold', 'toAddress', 'value', 'gasLimit', 'gasPrice'] if self.validateParameters(keys): if self.isSignAtomicSwapTx(): self.unsignedTx = self.params else: self.fromAddress = Address.toAddressBytes(self.params['fromAddress']) self.threshold = self.params['threshold'] self.toAddress = Address.toAddressBytes(self.params['toAddress']) self.value = Units.toUnits(self.params['value']) self.gasLimit = self.params['gasLimit'] self.gasPrice = Units.toUnits(self.params['gasPrice']) if self.validate(): return True return False
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) keys = [ 'fromAddress', 'script', 'parameters', 'value', 'gasLimit', 'gasPrice' ] if self.validateParameters(keys): self.fromAddress = Address.toAddressBytes( self.params['fromAddress']) if self.isDeploy(): self.script = DataType.fromHex(self.params['script']) else: self.script = Address.toAddressBytes(self.params['script']) self.parameters = self.params['parameters'] self.value = Units.toUnits(self.params['value']) self.gasLimit = self.params['gasLimit'] self.gasPrice = Units.toUnits(self.params['gasPrice']) if self.validate(): return True return False
def get(self, callback=None): contracts = [] for contract in Contracts.getContracts(): confirmedBalance = 0 unspentTransactionScript = UXTO.getUnspentTransactionScript( contract.address) if unspentTransactionScript != None: txOut = unspentTransactionScript.output confirmedBalance = txOut.value confirmedBalance = Units.toValue(confirmedBalance) confirmedBalance = DataType.asFloat(confirmedBalance) address = Address.to0xAddress(contract.address) contracts.append({'address': address, 'balance': confirmedBalance}) callback(JSONRPC.createResultObject(contracts, self.id))
def deserialize(self, payload): if self.validatePayload(payload): self.deserializePayload(payload) if self.validateParameters(): if self.isMultiSig(): for publicKey in self.params: publicKey = DataType.fromHex(publicKey) self.publicKeys.append(publicKey) else: for addr in self.params: addr = Address.toAddressBytes(addr) self.address.append(addr) if self.validate(): return True return False
def initialize(self): self.code = self.output.script[1:] parameters = [] if self.deploy: parameters = [] if self.output.extraData == None else self.output.extraData else: if self.output.hasExtScript(): parameters.extend(self.input.witness[3:]) else: self.stack.extend(self.input.witness) methodHash = False if self.deploy else True for parameter in parameters: if not isinstance(parameter, bytes): try: if methodHash: parameter = DataType.fromHex(parameter) parameter = DataType.serialize(parameter) methodHash = False else: parameter = DataType.asInt(parameter) except ValueError: try: parameter = DataType.fromHex(parameter) parameter = DataType.serialize(parameter) except ValueError: parameter = Address.toAddressBytes(parameter) if self.deploy: self.code += DataType.zeroFillArray(parameter, 32) else: if len(self.data) == 0: self.data += parameter else: self.data += DataType.zeroFillArray(parameter, 32) if len(self.data) > 0 and len(self.data) < 32: fillLen = 32 - len(self.data) self.data.extend(fillLen * b'\x00')
def deserialize(self, buffer): self.address = Address.toAddressBytes(buffer['address']) self.public = DataType.fromHex(buffer['public']) self.private = DataType.fromHex(buffer['private']) self.type = buffer['type']
def serialize(self): return {'address': Address.toAddressStr(self.address)}
def deserialize(self, buffer): self.address = Address.toAddressBytes(buffer['address'])