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 createAtomicSwapTx(self, callback): fromAccount = Accounts.getAccountByAddress(self.fromAddress) toMultiSigAccount = Accounts.getAccountByAddress(self.toAddress) ''' A picks a random number x ''' hashPrivKey = Crypto.generateHash(toMultiSigAccount.private) ''' A creates TX1: "Pay w BTC to <B's public key> if (x for H(x) known and signed by B) or (signed by A & B)" ''' transactionA1 = MemoryPool.createUnconfirmedAtomicSwapTransaction(fromAccount, toMultiSigAccount.address, self.value, self.gasLimit, self.gasPrice, Script.verifyAtomicSwapSignature(), self.threshold) if transactionA1 != None: ''' A creates TX2: "Pay w BTC from TX1 to <A's public key>, locked 48 hours in the future, signed by A" ''' transactionA2 = MemoryPool.createUnconfirmedAtomicSwapTransaction(fromAccount, toMultiSigAccount.address, self.value, self.gasLimit, self.gasPrice, Script.verifyAtomicSwapLock()) if transactionA2 != None: tx = transactionA1.serialize() unsignedTx = transactionA2.serialize() result = { 'tx': DataType.toHex(tx), 'unsignedTx': DataType.toHex(unsignedTx) } callback( JSONRPC.createResultObject(result, self.id) ) else: self.onFailure(callback) else: self.onFailure(callback)
def addUnspentTransactionCoin(outpoint, coin): txOut = coin.output if txOut.hasExtScript(): UXTO.index.set(txOut.address, outpoint.serialize()) Contracts.addContract(Contract(txOut.address)) elif Accounts.hasAddress(txOut.address): Accounts.addConfirmedBalanceByAddress(txOut.address, txOut.value) Accounts.addUnspentTransactionCoin(outpoint, coin) UXTO.uxto.set(outpoint.serialize(), coin.serialize())
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 removeUnspentTransactionCoin(outpoint): coin = getUnspentTransactionCoin(outpoint) if coin != None: txOut = coin.output if txOut.hasExtScript(): if outpoint.removal: UXTO.index.remove(txOut.address) Contracts.removeContract(txOut.address) else: return elif Accounts.hasAddress(txOut.address): Accounts.subtractConfirmedBalanceByAddress(txOut.address, txOut.value) Accounts.removeUnspentTransactionCoin(outpoint) UXTO.uxto.remove(outpoint.serialize())
def deploy(self, isLocal, callback): result = None fromAccount = Accounts.getAccountByAddress(self.fromAddress) if fromAccount != None: if isLocal: toAddress = Crypto.generateAddress(fromAccount.address) _script = bytearray() _script.append(Config.getIntValue("EXTENSION_SCRIPT_VERSION")) _script.extend(self.script) _script = DataType.serialize(_script) extraData = None if len(self.parameters) > 0: extraData = self.parameters extraData.append(fromAccount.address) output = Output(toAddress, _script, self.value, extraData) result = self.handleLocalScript(fromAccount, output, True) if result != None: Contracts.addContract(Contract(toAddress), False) else: result = MemoryPool.addUnconfirmedTransaction( fromAccount, None, self.value, self.gasLimit, self.gasPrice, self.script, self.parameters) try: result = DataType.toHex(result) except: pass if result != None: callback(JSONRPC.createResultObject(result, self.id)) else: self.onFailure(callback)
def call(self, isLocal, callback): result = None fromAccount = Accounts.getAccountByAddress(self.fromAddress) if fromAccount != None: if isLocal: _output = PersistentStorage.get(self.script, True) if _output == None: unspentTransactionScript = UXTO.getUnspentTransactionScript( self.script) if unspentTransactionScript != None: _output = unspentTransactionScript.output if _output != None: result = self.handleLocalScript(fromAccount, _output, False) else: result = MemoryPool.addUnconfirmedTransaction( fromAccount, self.script, self.value, self.gasLimit, self.gasPrice, None, self.parameters) try: result = DataType.toHex(result) except: pass if result != None: callback(JSONRPC.createResultObject(result, self.id)) else: self.onFailure(callback)
def onSuccess(self, callback = None): isMiningSupported = Config.getBoolValue("MINING_SUPPORTED") if isMiningSupported: account = Accounts.getAccountByAddress(self.address) if account != None: if not self.chain.exitsMiningWorker(account): self.chain.addMiningWorker(account, self.enabled) callback( JSONRPC.createResultObject('added mining worker', self.id) ) else: if self.enabled: callback( JSONRPC.createResultObject('worker is mining', self.id) ) else: self.chain.stopMiningWorker(account) callback( JSONRPC.createResultObject('worker is stopped mining', self.id) ) else: callback( JSONRPC.createErrorObject(-32005, 'not found', 'account not found', self.id) ) else: callback( JSONRPC.createErrorObject(-32006, 'not supported', 'node does not support mining', self.id) )
def sign(self): signatures = {} for txIn in self.inputs: unspentTransactionCoin = UXTO.getUnspentTransactionCoin( txIn.outpoint) unspentTransactionOutput = unspentTransactionCoin.output outputHash = self.hashOutput(txIn, unspentTransactionOutput) account = None if unspentTransactionOutput.hasExtScript(): account = Accounts.getAccountByAddress(txIn.witness[0]) else: account = Accounts.getAccountByAddress( unspentTransactionOutput.address) if account.isMultiSig(): _signatures = [] for public in account.public: _account = Accounts.getAccountByPublic(public) signature = _account.sign(outputHash) _signatures.append(signature) signatures[txIn.outpoint] = _signatures else: signature = account.sign(outputHash) signatures[txIn.outpoint] = signature for txIn in self.inputs: unspentTransactionCoin = UXTO.getUnspentTransactionCoin( txIn.outpoint) unspentTransactionOutput = unspentTransactionCoin.output account = None if unspentTransactionOutput.hasExtScript(): account = Accounts.getAccountByAddress(txIn.witness[0]) else: account = Accounts.getAccountByAddress( unspentTransactionOutput.address) if account.isMultiSig(): signatures = signatures[txIn.outpoint] for signature, public in zip(signatures, account.public): txIn.initWitness(signature, public) else: signature = signatures[txIn.outpoint] txIn.initWitness(signature, account.public)
def addUnconfirmedMultiSigTransaction(self, callback=None): fromAccount = Accounts.getAccountByAddress(self.fromAddress) txId = None if fromAccount != None: txId = MemoryPool.addUnconfirmedMultiSigTransaction( fromAccount, self.publicKeys, self.signatures, self.toAddress, self.value, self.gasLimit, self.gasPrice, self.threshold) if txId != None: callback(JSONRPC.createResultObject(DataType.toHex(txId), self.id)) else: self.onFailure(callback)
def remove(self, callback = None): removed = False for addr in self.address: removed = Accounts.removeAccount(addr) if not removed: break if removed: callback( JSONRPC.createResultObject('accounts removed', self.id) ) else: callback( JSONRPC.createErrorObject(-32003, 'delete failed', 'failed to remove accounts', self.id) )
def signMultiSigOutput(self, callback=None): signatures = [] multiSigAccount = Accounts.getAccountByAddress(self.fromAddress) if multiSigAccount != None: transaction = MemoryPool.createUnconfirmedMultiSigTransaction( multiSigAccount, self.toAddress, self.value, self.gasLimit, self.gasPrice, self.threshold) if transaction != None: for txIn in transaction.inputs: multiSigAddress = DataType.toHex(multiSigAccount.address) _publicKeys = txIn.witness[::2] _signatures = txIn.witness[1::2] for publicKey, signature in zip(_publicKeys, _signatures): publicKey = DataType.toHex(publicKey) signature = DataType.toHex(signature) signatures.append({ 'address': multiSigAddress, 'public': publicKey, 'signature': signature }) callback(JSONRPC.createResultObject(signatures, self.id))