- 配置环境:
- npm init
- npm install @iota/core @iota/converter
- 生成随机seed(私钥):
cat /dev/urandom |tr -dc A-Z9|head -c${1:-81}
- 私钥可以关联多个地址, 执行下面代码, 生成两个钱包地址:
const Iota = require('@iota/core');
const seed =
'KHSYXDNSEOVFCAERBIPDIXMMXSQKOBPJTAVRGBYXCENMPXOZNHIOKSANOHCTRLGBXBLSINTYIXWZIKMFQ';
const index = 0;
const securityLevel = 3;
const addrCnt = 2;
// Connect to a node
const iota = Iota.composeAPI({
provider: 'https://nodes.devnet.iota.org:443',
});
// generate two address
iota.getNewAddress(seed, {
index: index,
securityLevel: securityLevel,
total: addrCnt,
})
.then((address) => {
console.log('Your address is: ' + address);
})
.catch((err) => {
console.log(err);
});
// output:
// Your address is: RLSEXCEJSSELIHZ9KUXDQN9SDRNAIDPIELWKHGFTORWL9LDZRMFYMRKNFNLODWEKDPEUKCHSC9CH9VRRX,ZWZQGKTJGENPOVVRJNDRNEWQAMDAHFFY9OUVJEEGLCWZHEYU9DADWRFWFVLRRAKWQZWDVNK9LMWQU9UOW
// 第1个地址的`index = 0`, 第2个地址的`index = 1`;
- 在 获取测试Token网站 获取测试Token
- 在 devnet 通过地址查询获取到的测试Token
- 发送
Token
测试:
const Iota = require('@iota/core');
const Converter = require('@iota/converter');
// Connect to a node
const iota = Iota.composeAPI({
provider: 'https://nodes.devnet.iota.org:443',
});
// Define the depth that the node will use for tip selection
const depth = 3;
// Define the minimum weight magnitude for the Devnet
const minimumWeightMagnitude = 9;
// Replace this seed with the one that owns the address you used to receive test tokens
const seed =
'KHSYXDNSEOVFCAERBIPDIXMMXSQKOBPJTAVRGBYXCENMPXOZNHIOKSANOHCTRLGBXBLSINTYIXWZIKMFQ';
// Create a wrapping function so you can use async/await
const main = async () => {
// Define an address to which to send IOTA tokens
const receivingAddress =
'ZWZQGKTJGENPOVVRJNDRNEWQAMDAHFFY9OUVJEEGLCWZHEYU9DADWRFWFVLRRAKWQZWDVNK9LMWQU9UOW';
const message = JSON.stringify({"message": "Heng30 tests api"});
// Convert the message to trytes
const messageInTrytes = Converter.asciiToTrytes(message);
// Define an input transaction object
// that sends 10i to your new address
const transfers = [
{
value: 10,
address: receivingAddress,
message: messageInTrytes,
},
];
console.log('Sending 10i to ' + receivingAddress);
try {
// Construct the bundle and sign your input transactions
const trytes = await iota.prepareTransfers(seed, transfers);
// Send the transactions to the node
const response = await iota.sendTrytes(
trytes,
depth,
minimumWeightMagnitude
);
console.log('Bundle sent');
response.map((tx) => console.log(tx));
} catch (error) {
console.log(error);
}
};
main();
- 在 devnet 通过地址查询获取到的测试Token
参考
如果觉得有帮助, 可以扫描右边的微信打赏码支持一下.