ICP nodejs 接入

作者: 博尔克斯 | 来源:发表于2021-10-03 11:20 被阅读0次

    ICP 接入

    对ICP币节点扫描,生成unsignedTx,本地签名发布上链

    ICP文档及其介绍

    开发包:https://sdk.dfinity.org/
    文档: https://smartcontracts.org/docs/quickstart/quickstart-intro.html
    官网:https://dfinity.org/
    区块浏览器:https://dashboard.internetcomputer.org/

    其他:
    论坛:https://forum.dfinity.org/
    第三方区块浏览器:https://www.dfinityexplorer.org/#/
    官方教程:https://github.com/DFINITY-Education
    grant申请:https://dfinity.org/grants/

    sdk接入

    本文主要以nodejs sdk的方式接入,nodejs参考文档:https://github.com/dfinity/rosetta-client

    生成地址,公钥私钥

    let { randomBytes } = require("crypto");
    const mnemonicToKeyPair = require('mnemonic-to-key-pair')
    const tweetnacl = require('tweetnacl');
    
    let { 
        key_new, 
        key_to_pub_key, 
        seed_from_pem,
        pub_key_to_address,
    } = require("@dfinity/rosetta-client");
    
    // 从用户指定的 32 字节随机数种子生成一个 ED25519 私钥。
    let seed = randomBytes(32);
    
    // 生成私钥 公钥 地址
    let privateKey = key_new(seed);
    let publicKey = key_to_pub_key(privateKey);
    let address = pub_key_to_address(publicKey);
    

    另外一种私钥生成和管理方式,通过助记词来生成

    const {privateKey, publicKey} = mnemonicToKeyPair(
        'maze birth my runway you pulp vast universe era panda hello access',
        "m/44'/0'/0'/0/0",  // 改变这里的值,映射多个公私钥管理
        "12345671",
    );
    let key = tweetnacl.sign.keyPair.fromSeed(privateKey);
    let priKey = key.secretKey;
    let pubKey = key_to_pub_key(priKey);
    let address = pub_key_to_address(publicKey);
    

    签名认证上链

    let { Session, transfer_combine, address_from_hex} = require("@dfinity/rosetta-client");
    let session = new Session({baseUrl: "https://rosetta-api.internetcomputer.org"});
    
    // 由上面生成了相应地址和公私钥(由上面的助记词随机生成的)
    let priKey = "1e7f57d002109581348519cd7b2478edf1c99703ca2ae0d35be1724ae7241c9f2280216b8b286290bfffe841a7e19c846867504a522d49f1dbcdb6c29f37649c";
    let pubKey = "2280216b8b286290bfffe841a7e19c846867504a522d49f1dbcdb6c29f37649c";
    let address= "210b3e285f05ae41c8b5a94952ba01f5e551ab6aab9f3d6e6ec1273a";
    
    // 打包交易
    let payloadsResult = await session.transfer_pre_combine(
        Buffer.from(pubKey), // 源地址
        address_from_hex(address), // 目的地址
        BigInt(10000),  // amount decimals=9
    );
    
    // 签名,可以离线处理
    let combineResult = transfer_combine(Buffer.from(priKey), payloadsResult);
    
    // 上链发布
    let sumbitResult = await session.transfer_post_combine(combineResult);
    

    相关文章

      网友评论

        本文标题:ICP nodejs 接入

        本文链接:https://www.haomeiwen.com/subject/wvvvnltx.html