Infura Filecoin API使用教程

Infura以提供免費的以太坊Web3 API而聞名,隨着Filecoin的流行,Infura也適時推出了Filecoin的免費API,方便開發者無需搭建Filecoin節點就可以開發Filecoin應用。在這個教程中,我們將學習如何利用Infura提供的Filecoin API來創建一個簡單的Node.js程序來對接Filecoin區塊鏈網絡,例如驗證Filecoin地址並查詢Filecoin地址餘額等。

如果你的主力開發語言是PHP或Golang,那麼可以使用以下開發包:Filecoin.php | Filecoin.go

1、Infura API

在訪問Infura API之前,首先需要到Infura網站上註冊。這是一個免費的API,只需註冊電子郵件即可:

  • 前往infura.io 並登錄。
  • 在側欄中選擇Filecoin,然後單擊創建新項目。
  • 輸入項目名稱,例如:Filecoin - Get Started,然後單擊創建。
  • 現在,你應該可以看到Project ID和Project Secret。記下這兩個字段,我們稍後再使用。

結果看起來是這樣:

在這裏插入圖片描述

接下來我們創建項目。

2、創建Filecoin對接項目

出於簡化考慮,讓我們爲項目創建一個新目錄,並創建一個包含樣板代碼的文件:

首先創建一個新的項目目錄並進入該目錄:

~$ mkdir ~/Code/filecoin-wallet-checker -p
~$ cd ~/Code/filecoin-wallet-checker

接下來在filecoin-wallet-checker目錄下創建文件index.js並添加以下初始代碼:

let request = require('request')

// Call the Infura API and check that the address is valid.

request(options, (error, response, body) => {
  if (error) {
    console.error('Error: ', error)
  } else {
    console.log('Response: ', body)
  }
})

最後,別忘了將request包添加到該項目,因爲在上面的代碼中我們使用了它:

~/filecoin-wallet-checker$ npm install request

> ...
> + [email protected]
> added 47 packages from 58 contributors and audited 47 packages in 1.594s
> ...

現在我們已經建立了一個項目,可以開始編寫對接Filecoin區塊鏈的JS程序了!

3、Filecoin對接JS代碼編寫

我們已經建立了項目目錄,並準備好了index.js文件。現在先創建一個基本的Filecoin API調用來充實程序。

作爲回顧,這是我們的樣板代碼如下所示:

let request = require('request')

// Call the Infura API and check that the address is valid.

request(options, (error, response, body) => {
  if (error) {
    console.error('Error: ', error)
  } else {
    console.log('Response: ', body)
  }
})

在註釋下方創建一個名爲options的對象,我們將利用這個對象聲明request請求的細節:

// Call the Infura API and check that the address is valid.
let options = {}

在options對象中設置url、method和headers參數:

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  }
}

容易理解上面這些參數的含義,因此不再贅述。我們更感興趣的兩個參數是bodyauth

現在添加auth參數對象:

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {}
}

Infura API將使用auth參數的內容來驗證請求。

將你的Infura項目ID和項目密鑰添加到user和pass字段:

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  }
}

在user字段中輸入Infura項目ID ,然後在pass字段中輸入Infura項目密碼。你可以轉到 infura.io/dashboard/filecoin,選擇具體項目並轉到設置標籤來找到這些信息。

我們需要添加到options對象中的最後一個參數是body。該對象用來聲明API端點要使用的ID、JSON RPC版本以及要調用的方法等。設置id爲0和jsonrpc爲2.0:

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0", 
      "id": 0
 }`
}

最後設置要調用的method爲Filecoin.ChainHead

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0", 
      "id": 0, 
      "method": "Filecoin.ChainHead"
  }`
}

Filecoin.ChainHead方法返回Filecoin區塊鏈的當前鏈頭數據。雖然這不是我們要使用的最終方法,但它是測試我們的js程序是否正常工作的好方法。

4、測試Filecoin對接程序的運行

我們的js程序已經實現了對接Filecoin區塊鏈的基本功能,現在進行測試運行以確保其正常工作!

在項目目錄中,使用node執行對接程序:

node index.js

> Post successful: response:  {"jsonrpc":"2.0","result":{"Cids":[{"/":"bafy2bzaceamdit67mnlyozufeaptmhmti6dv ...

優秀!Infura API收到了我們的請求,並向我們發送了最新的鏈頭信息。但是我們對鏈頭數據並不感興趣,我們想獲取有關地址的信息!

5、驗證Filecoin地址是否有效

現在讓我們如何利用Infura的Filecoin API來驗證指定的字符串是否是有效的Filecoin地址。

在options對象的body參數內,將method改爲Filecoin.WalletValidateAddress

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0",
      "id": 0,
      "method": "Filecoin.WalletValidateAddress"
  }`
}

如果我們現在運行程序,Infura API會返回錯誤,因爲WalletValidateAddress方法需要至少一個字符串作爲參數。

在body對象中添加一個名爲params的數組:

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0",
      "id": 0,
      "method": "Filecoin.WalletValidateAddress",
      "params": [""],
  }`
}

在params數組內,添加要檢查的地址:

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0",
      "id": 0,
      "method": "Filecoin.WalletValidateAddress",
      "params": ["f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi"],
  }`
}

本示例使用f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi作爲要驗證的字符串。

讓我們重新運行js程序以查看得到的響應:

node index.js

> Response:  {"jsonrpc":"2.0","result":"f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi","id":0}

棒極了!在result中出現地址意味着我們的地址有效。如果我們發送了一個無效的地址,我們將得到如下信息:

Response:  {"jsonrpc":"2.0","id":0,"error":{"code":1,"message":"invalid address payload"}}

6、查看Filecoin地址餘額

在前一個示例中,我們的JS程序可以檢查給定的字符串是否是有效的Filecoin地址。現在讓我們實現檢查Filecoin地址餘額的功能。

我們要做的唯一的修改,就是將請求方法改成WalletBalance

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0", 
      "id": 0, 
      "method": "Filecoin.WalletBalance", 
      "params": ["f1ydrwynitbbfs5ckb7c3qna5cu25la2agmapkchi"]}`
}

Infura API將讓我們知道餘額:

node index.js

> ADDRESS:  {"jsonrpc":"2.0","result":"7182015146934547358774","id":0}

Infura API返回結果中的餘額單位爲attoFIL。如果請求的地址沒有餘額,則Infura的響應將爲空白。

7、完整的Filecoin對接JS程序代碼

到目前爲止,我們的對接Filecoin的js程序代碼如下,你可以在此基礎上繼續完善:

let request = require('request')

// Call the Infura API and check that the address is valid.
let options = {
  url: 'https://filecoin.infura.io',
  method: 'post',
  headers: {
    'content-type': 'application/json'
  },
  auth: {
    user: '1nO7B...',
    pass: 'bda4a...'
  },
  body: `{
      "jsonrpc": "2.0",
      "id": 0,
      "method": "Filecoin.WalletBalance",
      "params": ["f3tfhgkbq6h55fqhumadd7wvogx3bbhgm3ifg6mk6hq35ob3fex2uei7hfbo2nwqkzudjzjidmshxpvo3ja4iq"]
  }`
}

request(options, (error, response, body) => {
  if (error) {
    console.error('Error: ', error)
  } else {
    console.log('Response: ', body)
  }
})

原文鏈接:利用Infura對接Filecoin區塊鏈 — 匯智網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章