filecoin-saturn / contracts

contracts
6 stars 0 forks source link

feat: RPC calls for actor ID extraction from f1-f3 addr #20

Closed alexander-camuto closed 1 year ago

alexander-camuto commented 1 year ago

Many users will be accustomed to handling f1 type addresses; which don't have a direct method of conversion to 0x type addresses.

Here if the conversion function stumbles upon a f1, f2, f3 type addresses it makes a call to a lotus RPC endpoint to convert that address to an actor ID / f0 address such that that address can be converted to an 0x address via masking.

// call to get f0 type address
let lotus_call = json!({
    "jsonrpc": "2.0",
    "method": "Filecoin.StateLookupID",
    "params": [address, []],
    "id": 1
});

let response = reqwest::Client::new()
    .post(rpc_url)
    .json(&lotus_call)
    .send()
    .await;
let response = response.map_err(|_| AddressError::RPCFailure)?;

// f0 type address
let lookup_resp: StateLookupIDResp = response
    .json()
    .await
    .map_err(|_| AddressError::RPCFailure)?;

check_address_string(&lookup_resp.result, "").await?

To validate the full loop

// test SECP256K1 addresses
let addr = "t1ypi542zmmgaltijzw4byonei5c267ev5iif2liy";  
let addr_id = "t01004";
assert_eq!(filecoin_to_eth_address(addr, "https://api.hyperspace.node.glif.io/rpc/v1").await.unwrap(),
filecoin_to_eth_address(addr_id, "").await.unwrap());