본문 바로가기
Hack/Cryptocurrency

FileCoin(FIL) JSON-RPC로 통신하기

by Becoming a Hacker 2023. 3. 13.
반응형

기본 구문

import requests
import json
import cbor

# Filecoin 노드 RPC endpoint URL
node_url = 'https://api.node.glif.io'

def json_request(method,cid):
    # JSON RPC 요청 메시지 구성
    rpc_request = {
        "jsonrpc": "2.0",
        "id": 0,
        "method": f"Filecoin.{method}",
        "params": [{"/":cid},]
    }
    return rpc_request

def get_response(method,cid):
    # JSON RPC 요청 보내기
    response = requests.post(node_url, json=json_request(method,cid))

    # JSON RPC 응답 처리
    if response.status_code == 200:
        rpc_response = json.loads(response.content)
        return rpc_response
    else:
        print("Error: HTTP status code", response.status_code)
        return None

 

  • 지정된 Block의 Parent Message 내 존재하는 method 및 amount 수집
cid = "bafy2bzacechvfkkjvjy3xfxntec2u4qgwr4ldcbedbpzw4w3d7tkdruwrqorw"

message_result = get_response("ChainGetParentMessages",cid)
print(message_result)
try:
    if(message_result):
        for message in message_result["result"]:
            amount = message["Message"]["Value"]
            method = message["Message"]["Method"]
            print(message["Message"]["CID"],method, amount)
except:
    pass

 

  • 메시지 내 데이터 확인
cid = "bafy2bzacedusu7ogm7roemoyuqdcqol3b7mn6txndcc27plvx7tb5knwxytou" # propose
# cid = "bafy2bzaceatjsbldoavngt5bb2coducplofg4e7xrtcrj33ufdzodrbufa3ce" # approve
message_result = get_response("ChainGetMessage",cid)
if(message_result):
    message = message_result["result"]
    method = message["Method"]
    if(method==0): # Method is Send
        print(message)
    elif(method==2): # Method is Propose (Multisig Wallet)
        cbor_data = cbor.loads(base64.b64decode(message['Params']))
        print(message)
        to = cbor_data[0]
        value = int("0x"+cbor_data[1].hex(),16)
        p_method = cbor_data[2]
        p_params = cbor_data[3]
        print(f"Params : To : {to}, Value : {value}, method : {p_method}, params : {p_params}")
    elif(method==3): # Method is Approve
        print(message)
        cbor_data = cbor.loads(base64.b64decode(message['Params']))
        print(f"params : {cbor_data}")

 

 

참고 문서

 

Chain

The Chain method group contains methods for interacting with the blockchain, but that do not require any form of state computation. ChainDeleteObj # ChainDeleteObj deletes node referenced by the given CID Perms: admin Inputs: [ { "/": "bafy2bzacea3wsdh6y3a

docs.filecoin.io

 

 

Home

 

spec.filecoin.io

 

댓글