예제 #1
0
def broadcastTransaction(id, params):

    requestSchema = utils.getRequestMethodSchema(BROADCAST_TRANSACTION)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    RPCConnector.request(RPC_CORE_ENDPOINT, id, SEND_RAW_TRANSACTION_METHOD, [params[RAW_TRANSACTION]])
    
    return {}
예제 #2
0
def getBlockByNumber(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_BLOCK_BY_NUMBER)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)


    if not params[BLOCK_NUMBER].startswith('0x'):
        blockNumber = hex(int(params[BLOCK_NUMBER]))
    else:
        blockNumber = params[BLOCK_NUMBER]

    block = RPCConnector.request(RPC_ENDPOINT, id, GET_BLOCK_BY_NUMBER_METHOD, [blockNumber, True])
    
    response = {
        TRANSACTIONS: block[TRANSACTIONS]
    }
        
    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #3
0
def getAddressUnspent(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_ADDRESS_UNSPENT)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    connResponse = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_ADDRESS_UNSPENT_METHOD, [params[ADDRESS]])

    response = []

    for tx in connResponse:

        response.append(
            {
                TX_HASH: tx[TX_HASH_SNAKE_CASE], 
                VOUT: str(tx[TX_POS_SNAKE_CASE]),
                STATUS: 
                    {
                        CONFIRMED: tx[HEIGHT] != 0,
                        BLOCK_HEIGHT: str(tx[HEIGHT])
                    },
                VALUE: str(tx[VALUE])
            }
        )
    
    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #4
0
def getTransaction(id, params):
    
    requestSchema, responseSchema = utils.getMethodSchemas(GET_TRANSACTION)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)


    transactionRaw = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_TRANSACTION_METHOD, [params[TX_HASH]])
    transaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, DECODE_RAW_TRANSACTION_METHOD, [transactionRaw])
    
    err = rpcutils.validateJSONRPCSchema(transaction, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return transaction
예제 #5
0
def getAddressBalance(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_ADDRESS_BALANCE)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    connLatest = RPCConnector.request(RPC_ENDPOINT, id, GET_BALANCE_METHOD, [utils.ensureHash(params[ADDRESS]), LATEST])
    connPending = RPCConnector.request(RPC_ENDPOINT, id, GET_BALANCE_METHOD, [utils.ensureHash(params[ADDRESS]), PENDING])

    response = {
        CONFIRMED: connPending,
        UNCONFIRMED: hex(int(connPending, 16) - int(connLatest, 16))
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #6
0
def getHeight(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_HEIGHT)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    latestBlockHeight = int(RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_BLOCK_COUNT_METHOD, []))
    latestBlockHash = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_BLOCK_HASH_METHOD, [latestBlockHeight])

    response = {
        LATEST_BLOCK_INDEX: latestBlockHeight, 
        LATEST_BLOCK_HASH: latestBlockHash
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response
예제 #7
0
def getBlockByHash(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_BLOCK_BY_HASH)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    block = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_BLOCK_METHOD, [params[BLOCK_HASH], VERBOSITY_MORE_MODE])

    err = rpcutils.validateJSONRPCSchema(block, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return block
예제 #8
0
def getTransactionReceipt(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_TRANSACTION_RECEIPT)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    response = RPCConnector.request(RPC_ENDPOINT, id, GET_TRANSACTION_RECEIPT_METHOD, [params[TX_HASH]])

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response
예제 #9
0
def broadcastTransaction(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(BROADCAST_TRANSACTION)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    transactionHash = RPCConnector.request(RPC_ENDPOINT, id, SEND_RAW_TRANSACTION_METHOD, [params[RAW_TRANSACTION]])
    response = {
        BROADCASTED: transactionHash
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #10
0
def notify(id, params):
    
    requestSchema, responseSchema = utils.getMethodSchemas(NOTIFY)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    payload = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, NOTIFY_METHOD, [params[ADDRESS], params[CALLBACK_ENDPOINT]])

    response = {
        SUCCESS: payload
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #11
0
def getTransactionCount(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_TRANSACTION_COUNT)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    count = RPCConnector.request(RPC_ENDPOINT, id, GET_TRANSACTION_COUNT_METHOD, [utils.ensureHash(params[ADDRESS]), PENDING if params[PENDING] else LATEST])

    response = {
        TRANSACTION_COUNT: count
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #12
0
def getTransactionHex(id, params):
    
    requestSchema, responseSchema = utils.getMethodSchemas(GET_TRANSACTION_HEX)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    rawTransaction = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_TRANSACTION_METHOD, [params[TX_HASH]])

    response = {
        RAW_TRANSACTION: rawTransaction
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #13
0
def getBlockByHash(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_BLOCK_BY_HASH)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    block = RPCConnector.request(RPC_ENDPOINT, id, GET_BLOCK_BY_HASH_METHOD, [params[BLOCK_HASH], True])

    response = {
        TRANSACTIONS: block[TRANSACTIONS]
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response
예제 #14
0
def getGasPrice(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_GAS_PRICE)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    gas = RPCConnector.request(RPC_ENDPOINT, id, GET_GAS_PRICE_METHOD, None)

    response = {
        GAS_PRICE: gas
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #15
0
def estimateGas(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(ESTIMATE_GAS)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    gas = RPCConnector.request(RPC_ENDPOINT, id, ESTIMATE_GAS_METHOD, [params[TX]])

    response = {
        ESTIMATED_GAS: gas
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response
예제 #16
0
def getAddressBalance(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_ADDRESS_BALANCE)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    connResponse = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_ADDRESS_BALANCE_METHOD, [params[ADDRESS]])

    response = {
        CONFIRMED: utils.convertToSatoshi(connResponse[CONFIRMED]),
        UNCONFIRMED: utils.convertToSatoshi(connResponse[UNCONFIRMED])
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #17
0
def getHeight(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_HEIGHT)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    latestHash = RPCConnector.request(RPC_ENDPOINT, id, GET_BLOCK_BY_NUMBER_METHOD, [LATEST, True])

    response = {
        LATEST_BLOCK_INDEX: latestHash[NUMBER],
        LATEST_BLOCK_HASH: latestHash[HASH]
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response
예제 #18
0
def getBlockByNumber(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_BLOCK_BY_NUMBER)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    try:
        blockNumber = int(params[BLOCK_NUMBER], base=10)
    except Exception as err:
        raise rpcerrorhandler.BadRequestError(str(err))

    blockHash = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_BLOCK_HASH_METHOD,[blockNumber])

    return getBlockByHash(
        id,
        {
            BLOCK_HASH: blockHash
        }
    )
예제 #19
0
def getTransaction(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_TRANSACTION)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    transaction = RPCConnector.request(RPC_ENDPOINT, id, GET_TRANSACTION_BY_HASH_METHOD, [params[TX_HASH]])

    if transaction is None:
        raise rpcerrorhandler.BadRequestError("Could not get transaction from node")
    
    inputs = []
    outputs = []

    inputs.append(
        {
            ADDRESS: transaction[FROM], 
            AMOUNT: transaction[VALUE]
        }
    )
    outputs.append(
        {
            ADDRESS: transaction[TO], 
            AMOUNT: transaction[VALUE]
        }
    )

    response = {
        TRANSACTION: transaction, 
        INPUTS: inputs, 
        OUTPUTS: outputs
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #20
0
def getAddressHistory(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_ADDRESS_HISTORY)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    addrHistory = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_ADDRESS_HISTORY_METHOD, [params[ADDRESS]])

    response = {
        TX_HASHES: []
    }

    for item in addrHistory:
        response[TX_HASHES].append(item[TX_HASH_SNAKE_CASE])

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response
예제 #21
0
def getFeePerByte(id, params):

    requestSchema, responseSchema = utils.getMethodSchemas(GET_FEE_PER_BYTE)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    try:
        confirmations = int(params[CONFIRMATIONS], base=10)
    except ValueError as err:
        raise rpcerrorhandler.BadRequestError(str(err))

    feePerByte = RPCConnector.request(RPC_CORE_ENDPOINT, id, ESTIMATE_SMART_FEE_METHOD, [confirmations])

    response = {
        FEE_PER_BYTE: utils.convertToSatoshi(feePerByte[FEE_RATE])
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    return response
예제 #22
0
def getTransactionCount(id, params):
    
    requestSchema, responseSchema = utils.getMethodSchemas(GET_TRANSACTION_COUNT)

    err = rpcutils.validateJSONRPCSchema(params, requestSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)

    txs = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_ADDRESS_HISTORY_METHOD, [params[ADDRESS]])

    pending = 0
    for tx in txs:
        if tx[HEIGHT] == 0:
            pending += 1

    response = {
        TRANSACTION_COUNT: str(pending) if params[PENDING] else str(len(txs) - pending)
    }

    err = rpcutils.validateJSONRPCSchema(response, responseSchema)
    if err is not None:
        raise rpcerrorhandler.BadRequestError(err.message)
    
    return response