温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Infura Filecoin API的使用教程

发布时间:2021-06-24 14:12:26 来源:亿速云 阅读:420 作者:chen 栏目:互联网科技

这篇文章主要介绍“Infura Filecoin API的使用教程”,在日常操作中,相信很多人在Infura Filecoin API的使用教程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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。记下这两个字段,我们稍后再使用。

结果看起来是这样:

Infura Filecoin API的使用教程

接下来我们创建项目。

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

> ...
> + request@2.88.2
> 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": ["f3tfhgkbq6h65fqhumadd7wvogx3bbhgm3ifg6mk6hq35ob3fex2uei7hfbo2nwqkzudjzjidmshxpvo3ja4iq"]
  }`
}

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

到此,关于“Infura Filecoin API的使用教程”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI