快速集成
使用要求
Node Version >= 16推荐使用nvm或nvm-windows(windows) 来管理 Node 版本。
提示
- 本章节将介绍如何使用 hymatrix-js 快速的进行安装、引入、创建 、发起转账交易等操作。
安装
使用 yarn 或 npm 进行安装使用,若未安装成功请检查网络再次进行尝试。
- yarn
- npm
yarn add hymatrix-js
npm install hymatrix-js
引入
- ES Modules
- CommonJS
import HyMatrix from 'hymatrix-js'
const hyMatrix = new HyMatrix()
const HyMatrix = require('hymatrix-js')
const hyMatrix = new HyMatrix()
创建 HyMatrix 实例
以太坊 钱包连接方式
- 创建时可选择性注入
signer或privateKey等配置项。 - 请下载
MetaMask或 其他以太坊钱包。
1. 使用包管理器
(1)安装 hymatrix-js
- yarn
- npm
yarn add hymatrix-js
npm install hymatrix-js
(2)引入 hymatrix-js 进行创建。
import HyMatrix, { Web3Provider } from 'hymatrix-js'
const accid = ''
// 浏览器 Ethereum 插件钱包
const provider = new Web3Provider(window.ethereum)
const hyMatrix = new HyMatrix({
debug: true, // 开启 hymatrix dev 环境
accid: accid, // 账户地址
signer: provider // eth signer provider
})
// Ethereum 私钥
const privateKey = '0x...'
const hyMatrix2 = new HyMatrix({
debug: true, // 开启 hymatrix dev 环境
accid: accid, // 账户地址
privateKey: privateKey // 0x + 64位私钥, 共66位
})
提示
- 若在 WEB 环境中多个 ethereum 插件钱包同时存在,例如
MetaMask,OKX Wallet,TokenPocket Wallet,可使用eip6963:announceProvider和eip6963:requestProvider区分。 - 相关文档 https://eips.ethereum.org/EIPS/eip-6963#announce-and-request-events
Arweave 钱包连接方式
- 下载
Wander钱包。 - 创建需要注入
arJWK:- 当使用浏览器 Wander 钱包时,注入
arJWK: 'use_wallet'即可。 - 当使用 Arweave JWKInterface JSON 文件时,需导入文件,参考
arweave Docs - Sample JWK。
- 当使用浏览器 Wander 钱包时,注入
1. 使用包管理器
(1)安装 hymatrix-js
- yarn
- npm
yarn add hymatrix-js
npm install hymatrix-js
(2)引入 hymatrix-js 进行创建。
import HyMatrix from 'hymatrix-js'
// 浏览器 Arweave 插件钱包
const arAddress = await window.arweaveWallet.getActiveAddress()
const hyMatrix = new HyMatrix({
debug: true, // 开启 hymatrix dev 环境
accid: arAddress,
arJWK: 'use_wallet'
})
// JWKInterface KEY JSON // 需导入文件
// import jwkInterface from 'arweave-key-file.json'
const accid = ''
const hyMatrix2 = new HyMatrix({
debug: true, // 开启 hymatrix dev 环境
accid: accid, // 账户地址
arJWK: jwkInterface
})
提示
JWKInterface 方式: 参考 arweave Docs - Sample JWK。arweave 的私钥格式,一个 KEY JSON
成功啦
- 根据上面的步骤,我们已经完成了 HyMatrix 实例的创建了,接下来一起发起一笔转账交易吧!。
转账交易
HyMatrix 实例创建成功后,可通过 sendMessage 方法,对当前 账户(accid) 在 HyMatrix 网络已拥有的 资产(token) 进行转账。
填写发交易所需的 processId 、 tags、 data,调用下面的接口完成交易:
processId: 需要收到通知的processId。tags:Tag字段, 发起一笔转账,最少要包含Action、Recipient、Quantity参数。
data: 签名数据,可选。
// 转账:以 hmAR 为例
// 1. 确认转账的 processId
const processId = 'GuuH1wCOBatG-JoKu42NkJMC7Cx-rjD8F5EEICLTNP8'
// 2. 确认 收款地址和金额
const recipient = '' // ethAddress or arAddress
const amt = '100' // 转账金额
const tags = [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: recipient },
{ name: 'Quantity', value: amt }
]
// 3. 通过 processId 获取到指定的 url 节点,可成功 发起交易,// 若已知 url 可省略,减少请求,节约时间。
const urls = await new HyMatrix().getNodesByProcess(processId)
// 4. 创建 HyMatrix 实例
const hyMatrix = new HyMatrix({
arJWK: 'use_wallet',
url: urls[0]?.URL // 后续 hyMatrix 发起的所有请求统一从该参数的 url 下进行。
})
// 5 创建参数结构
const params = {
tags,
processId,
data: '' // 可选, 默认为空字符串
}
// 6. 发起交易
const result = await hyMatrix.sendMessage(params)
console.log(result.id) // 获取 messageID, 可通过其查具体交易记录
// wait.../ 稍等片刻后...
// 7. 查询交易记录 // tip: 会在指定 url 节点发起请求。
const resultTx = await hyMatrix.getResult(processId, result.id)
console.log(resultTx) // 获取详情交易信息