caict-4iot-dev / bif-rfcs

11 stars 5 forks source link

提出一种“星火·链网数字身份钱包异构链的接入协议”的标准建议 #22

Open jiaonliao opened 1 year ago

jiaonliao commented 1 year ago

在星火主链的生态体系下,拥有许多基于星火主链信任基础的骨干节点/子链。 对于这些庞杂的异构链,我们不可能对每个链都进行独立的数字身份钱包开发,因此我们需要一套统一的接入协议,来实现异构链的接入。 使不同骨干节点/子链,都能通过一个入口完成数字身份的认证、消息的签名、交易的发送等操作。

这套协议的完善是十分重要的,它将会是星火链网生态体系的基础设施之一,它可以使星火链网生态体系中,其他异构链完成数字身份管理的去中心化转型。

如果其他异构链没有去中心化的数字身份管理方式,那么在其生产实践中,将会出现以下问题:

  1. 私钥泄露,导致不同应用间的信息安全问题
  2. 应用平台自举数字身份
    1. 无法实现数字身份的跨应用使用
    2. 用户的隐私信息被平台掌控
    3. 用户的地址是不可信的,因为没有经过星火链网的数字身份认证
maruolong commented 1 year ago

感谢建议,请与我联系,详细讨论关于“星火·链网数字身份钱包异构链的接入协议”的内容。 email: maruolong@caict.ac.cn

jiaonliao commented 12 months ago

提出一种“星火·链网数字身份钱包异构链的接入协议”的标准

1.序言


编号:RFC-016

类型:标准

标题:星火·链网数字身份钱包异构链的接入协议

作者:焦竞健,wren.jiaocl@gmail.com

发布时间:2023-10-11

状态:

更新时间:2023-10-11

讨论地址:

依赖RFC: RFC-002、RFC-003、RFC-004

2.摘要


本文规范了异构链向星火·链网数字身份钱包接入的标准协议,其目的为使不同骨干节点/子链,都能通过一个入口完成数字身份的认证、消息的签名、交易的发送等操作。

3.动机


在星火主链的生态体系下,拥有许多基于星火主链信任基础的骨干节点/子链。对于这些庞杂的异构链,我们不可能对每个链都进行独立的数字身份钱包开发,因此我们需要一套统一的接入协议,来实现异构链的接入,使不同骨干节点/子链,都能通过一个入口完成消息的签名、交易的发送等操作。

4.原理


本章节构建于BID规范的前提之下,适用于实现了BID规范的异构节点接入星火·链网数字身份钱包,在实现了BID规范的前提下,确定AC编码,加密算法以及编码模式的私钥在任意链中都可以推导出一致的BID标识。

协议提取了钱包的基础数据结构,它们被定义得极其宽泛以用于兼容不同的链架构,被定义的基础数据有密钥对、资产、交易等。

任意符合BID规范的子链都可以通过实现协议构造该链架构下的专有函数,在其实现中,可以通过提前注入的Channel向钱包基础层发起访问(用于构造交易、用户授权签名等),协议实现的函数会通过映射关联到钱包的应用接口中,一般情况下,建议将同种链架构的子链实现进行合并,以防止钱包过于臃肿。

Untitled

5.规范


5.1 协议实现概览

协议实现者通过指定仓库提交PR,经审核后可并入分支,协议的实现者应为节点供应商、个人开发者、节点运营方,协议实现的粒度为链架构,同种链架构下,不同的子链可以使用同种链架构实现。

用户/Dapp/应用开发者,对于协议实现应是无感知的,协议实现方可向应用开发者提供关于链架构的特定开发文档。

Overview:

Protocal.register({
    chainType: "", // chain type
    version: "", // version
    storage: {
        //data sharing zero
    },
    onSwitch: (node) => {
        //network change hook
    },
    funs:{
        //custom funs
    },
    coin:{
        //native coin setting
    }
});

5.2 自定义函数

通过自定义函数,实现向不同异构链的交易、状态访问,其中,函数的参数、返回值都可以任意定义,钱包会如实的将其映射到应用接口中。

Example:

{
    storage: {
                 //data sharing zero
        //empty when switching network
        provider: undefined
    },
    onSwitch: (node) => {
        //switch network hook
                 this.storage.provider = new ethers.Wallet(node.rpcUrl);
    },
    funs: {
        triggerCall: async (from, to, amount, fun, inputs, outputs) => {
           //build tx for wallet
            const tx = Transactions.build({
                from,
                to,
                amount,
                fee: 0,
                fun,
                status: 0x00,
            });

            //submit tx to wallet and get busId
            //busId is a unique id for this tx
            const busId = this.channel.submit(tx);
            /*
            * Specific Implementation for Heterogeneous Chains
            * example: Ethereum
            */
            // 1. get contract instance 
            const fakeAbi = [{ name: fun, inputs: inputs, outputs: outputs }]
            const contract = new ethers.Contract(to, fakeAbi, this.storage.provider);

            // 2. get call function
            const callFun = contract.interface.getFunction(fun);

            //3. encode function data
            const message = contract.interface.encodeFunctionData(callFun, inputs);

            //sign message with wallet
            //busId is a unique id for this tx
            //message is the data to be signed
            const signature = await this.channel.sign(busId, message);
            //send raw tx to chain
            const ethereumTx = provider.sendRawTransaction(signature);

            //refresh txId
            tx.txId = ethereumTx.hash;
            this.channel.refresh(busId, tx);

            //wait tx to be confirmed
            await ethereumTx.wait();

            //get receipt
            const receipt = provider.getTransactionReceipt(ethereumTx.hash);

            //refresh tx status & fee
            tx.fee = ethereumTx.gasPrice * ethereumTx.gasUsed;
            tx.status = receipt.status == 0x01 ? 0x01 : 0x02;
            this.channel.refresh(busId, tx);

            //return receipt
            return receipt;
        }
        //custom funs
    }
}

以上示例中,我们定义了一个triggerCall 函数,其可以完成Ethereum中任意合约的调用,在自定义函数中,我们可以通过预置的对象向钱包完成通信:

签名时,将在用户界面显示您所提交的交易结构,故而,请设置合理的交易实例。

开发实现时应注意,提交交易后,在任何情况下都应向钱包说明此交易的状态,以防止交易状态长期Pending。

5.3 主链资产定义

钱包中普遍拥有对主链资产进行管理的能力,协议将主链币资产管理功能提取为余额查询、转账两个具名的功能函数。主链币的名称、符号、精度由节点/网络提供,在协议中不做具体展开。

Example:

coin: {
        balanceOf: async (bid) => {
            const provider = this.storage.provider;
            const balance = await provider.getBalance(bid);
            return balance;
        },
        transfer: async (from, to, amount) => {
            const provider = this.storage.provider;
            //build tx for wallet
            const tx = Transactions.build({
                from,
                to,
                amount,
                status: 0x00,
                fun: "transfer"
            });
            //submit tx to wallet and get busId
            const busId = this.channel.submit(tx);
            //sign message with wallet
            const ethereumTx = await provider.sendTransaction({
                from,
                to,
                value: amount
            });
            //refresh txId
            tx.txId = ethereumTx.hash;
            this.channel.refresh(busId, tx);
            //wait tx to be confirmed
            await ethereumTx.wait();
            //get receipt
            const receipt = provider.getTransactionReceipt(ethereumTx.hash);
            //refresh tx status & fee
            tx.fee = ethereumTx.gasPrice * ethereumTx.gasUsed;
            tx.status = receipt.status == 0x01 ? 0x01 : 0x02;
            this.channel.refresh(busId, tx);
            //return receipt
            return [tx, receipt];
        }
        //native coin setting
    },

实现方式基本上与自定义函数一致,但是函数名、函数参数不可以自定义,因为钱包需要具名的函数完成固定的动作,transfer函数返回值为一个元组,元组的第二个元素可作为自定义返回值使用。

5.4 协议实现的使用

协议实现由具体的应用开发者使用,应用开发者可以根据具体链架构下的协议实现自行开发应用。

5.4.1 网络切换

切换网络,同时载入链架构下的协议实现

const cm = bifWallet.switchNetwork([chain_id]);

5.4.2 发起主链币交易/查询主链币余额

bifWallet.transfer(from,to,amount) => Promise<[Tx,Any]>
bifWallet.balanceOf(bid) => Promise<BigNumber>

5.4.3 访问自定义函数

cm.funs["triggerCall"](...params) => Any 
//or
cm.funs.triggerCall(...params) => Any

5.4.4 事件

// network change
bifWallet.networkChange((chain)=>{});
// address change
bifWallet.bidChange((bid)=>{})
maruolong commented 12 months ago

接受关于“星火·链网数字身份钱包异构链的接入协议”的标准建议,现分配编号RFC-016。RFC-016进入接受阶段。

生命周期 提出日期:2023-08-28 演示日期:2023-08-28 接受日期:2023-10-11 采纳日期:

编号:RFC-016 类型:标准 标题:星火·链网数字身份钱包异构链的接入协议 作者:焦竞健,wren.jiaocl@gmail.com 发布时间:2023-10-11 状态:接受 更新时间:2023-10-11 讨论地址:https://github.com/caict-4iot-dev/bif-rfcs/issues/22 依赖RFC: 星火链网RFC-002:星火链网分布式标识BID星火链网RFC-003:星火链网BID生成规则和地址容量标准星火链网RFC-004:星火链网BID标识管理规范