溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Infura Filecoin API的使用教程

發(fā)布時間:2021-06-24 14:12:26 來源:億速云 閱讀:438 作者:chen 欄目:互聯(lián)網(wǎng)科技

這篇文章主要介紹“Infura Filecoin API的使用教程”,在日常操作中,相信很多人在Infura Filecoin API的使用教程問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Infura Filecoin API的使用教程”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

Infura以提供免費的以太坊Web3 API而聞名,隨著Filecoin的流行,Infura也適時推出了Filecoin的免費API,方便開發(fā)者無需搭建Filecoin節(jié)點就可以開發(fā)Filecoin應(yīng)用。在這個教程中,我們將學習如何利用Infura提供的Filecoin API來創(chuàng)建一個簡單的Node.js程序來對接Filecoin區(qū)塊鏈網(wǎng)絡(luò),例如驗證Filecoin地址并查詢Filecoin地址余額等。

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

1、Infura API

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

  • 前往infura.io 并登錄。

  • 在側(cè)欄中選擇Filecoin,然后單擊創(chuàng)建新項目。

  • 輸入項目名稱,例如:Filecoin - Get Started,然后單擊創(chuàng)建。

  • 現(xiàn)在,你應(yīng)該可以看到Project ID和Project Secret。記下這兩個字段,我們稍后再使用。

結(jié)果看起來是這樣:

Infura Filecoin API的使用教程

接下來我們創(chuàng)建項目。

2、創(chuàng)建Filecoin對接項目

出于簡化考慮,讓我們?yōu)轫椖縿?chuàng)建一個新目錄,并創(chuàng)建一個包含樣板代碼的文件:

首先創(chuàng)建一個新的項目目錄并進入該目錄:

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

接下來在filecoin-wallet-checker目錄下創(chuàng)建文件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

> ...
> + request@2.88.2
> added 47 packages from 58 contributors and audited 47 packages in 1.594s
> ...

現(xiàn)在我們已經(jīng)建立了一個項目,可以開始編寫對接Filecoin區(qū)塊鏈的JS程序了!

3、Filecoin對接JS代碼編寫

我們已經(jīng)建立了項目目錄,并準備好了index.js文件?,F(xiàn)在先創(chuàng)建一個基本的Filecoin API調(diào)用來充實程序。

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

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)
  }
})

在注釋下方創(chuàng)建一個名為options的對象,我們將利用這個對象聲明request請求的細節(jié):

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

在options對象中設(shè)置url、method和headers參數(shù):

// 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'
  }
}

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

現(xiàn)在添加auth參數(shù)對象:

// 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參數(shù)的內(nèi)容來驗證請求。

將你的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項目密碼。你可以轉(zhuǎn)到 infura.io/dashboard/filecoin,選擇具體項目并轉(zhuǎn)到設(shè)置標簽來找到這些信息。

我們需要添加到options對象中的最后一個參數(shù)是body。該對象用來聲明API端點要使用的ID、JSON RPC版本以及要調(diào)用的方法等。設(shè)置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
 }`
}

最后設(shè)置要調(diào)用的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區(qū)塊鏈的當前鏈頭數(shù)據(jù)。雖然這不是我們要使用的最終方法,但它是測試我們的js程序是否正常工作的好方法。

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

我們的js程序已經(jīng)實現(xiàn)了對接Filecoin區(qū)塊鏈的基本功能,現(xiàn)在進行測試運行以確保其正常工作!

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

node index.js

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

優(yōu)秀!Infura API收到了我們的請求,并向我們發(fā)送了最新的鏈頭信息。但是我們對鏈頭數(shù)據(jù)并不感興趣,我們想獲取有關(guān)地址的信息!

5、驗證Filecoin地址是否有效

現(xiàn)在讓我們?nèi)绾卫肐nfura的Filecoin API來驗證指定的字符串是否是有效的Filecoin地址。

在options對象的body參數(shù)內(nèi),將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"
  }`
}

如果我們現(xiàn)在運行程序,Infura API會返回錯誤,因為WalletValidateAddress方法需要至少一個字符串作為參數(shù)。

在body對象中添加一個名為params的數(shù)組:

// 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數(shù)組內(nèi),添加要檢查的地址:

// 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程序以查看得到的響應(yīng):

node index.js

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

棒極了!在result中出現(xiàn)地址意味著我們的地址有效。如果我們發(fā)送了一個無效的地址,我們將得到如下信息:

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

6、查看Filecoin地址余額

在前一個示例中,我們的JS程序可以檢查給定的字符串是否是有效的Filecoin地址?,F(xiàn)在讓我們實現(xiàn)檢查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返回結(jié)果中的余額單位為attoFIL。如果請求的地址沒有余額,則Infura的響應(yīng)將為空白。

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

到目前為止,我們的對接Filecoin的js程序代碼如下,你可以在此基礎(chǔ)上繼續(xù)完善:

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": ["f3tfhgkbq6h65fqhumadd7wvogx3bbhgm3ifg6mk6hq35ob3fex2uei7hfbo2nwqkzudjzjidmshxpvo3ja4iq"]
  }`
}

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

到此,關(guān)于“Infura Filecoin API的使用教程”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI