Rust区块链安全恢复库pallet-recovery的使用,实现Substrate链上账户多签与资产恢复功能

以下是基于您提供的内容整理的完整示例demo:

完整示例demo

// 1. 首先在runtime中配置pallet-recovery
impl pallet_recovery::Config for Runtime {
    type Event = Event;  // 使用Runtime的事件系统
    type Currency = Balances;  // 使用Balances作为货币类型
    type Call = Call;  // 使用Runtime的Call类型
    type ConfigDepositBase = ConfigDepositBase;  // 基础配置押金
    type FriendDepositFactor = FriendDepositFactor;  // 每个朋友的押金乘数
    type MaxFriends = MaxFriends;  // 最大朋友数量限制
    type RecoveryPeriod = RecoveryPeriod;  // 恢复周期
    type WeightInfo = ();  // 权重信息
}

// 2. 创建恢复配置
let account = AccountId::new([1; 32]);  // 需要恢复配置的账户
let friend1 = AccountId::new([2; 32]);  // 朋友1
let friend2 = AccountId::new([3; 32]);  // 朋友2
let friend3 = AccountId::new([4; 32]);  // 朋友3

let friends = vec![friend1, friend2, friend3];  // 朋友列表
let threshold = 2;  // 需要2个朋友批准
let delay_period = 10;  // 10个区块的延迟期

// 调用create_recovery创建恢复配置
Recovery::create_recovery(
    Origin::signed(account.clone()), 
    friends.clone(), 
    threshold, 
    delay_period
);

// 3. 当账户丢失时,使用新账户发起恢复
let new_account = AccountId::new([5; 32]);  // 新账户
let lost_account = account.clone();  // 丢失的账户

// 调用initiate_recovery启动恢复过程
Recovery::initiate_recovery(
    Origin::signed(new_account.clone()), 
    lost_account.clone()
);

// 4. 朋友为恢复担保
// 朋友1担保
Recovery::vouch_recovery(
    Origin::signed(friend1.clone()), 
    lost_account.clone(), 
    new_account.clone()
);

// 朋友2担保
Recovery::vouch_recovery(
    Origin::signed(friend2.clone()), 
    lost_account.clone(), 
    new_account.clone()
);

// 5. 等待delay_period(10个区块)后...

// 6. 认领恢复
Recovery::claim_recovery(
    Origin::signed(new_account.clone()), 
    lost_account.clone()
);

// 7. 作为已恢复账户执行操作
let call = Box::new(
    Call::Balances(pallet_balances::Call::transfer {
        dest: AccountId::new([6; 32]),  // 转账目标
        value: 100,  // 转账金额
    })
);

// 使用as_recovered代表恢复的账户执行调用
Recovery::as_recovered(
    Origin::signed(new_account.clone()), 
    lost_account.clone(), 
    call
);

// 8. 关闭恢复过程
Recovery::close_recovery(
    Origin::signed(lost_account.clone()), 
    new_account.clone()
);

// 9. 移除恢复配置
Recovery::remove_recovery(
    Origin::signed(lost_account.clone())
);

这个完整示例展示了从配置恢复、发起恢复、朋友担保到最终完成恢复的完整流程。每个步骤都有详细说明:

  1. 首先配置runtime以使用pallet-recovery
  2. 创建恢复配置,指定朋友列表和阈值
  3. 当账户丢失时,使用新账户发起恢复
  4. 朋友为恢复过程提供担保
  5. 等待延迟期结束后认领恢复
  6. 作为已恢复账户执行操作
  7. 最后关闭恢复过程并移除恢复配置

整个过程实现了安全的M-of-N多签恢复机制,确保在丢失私钥时仍能安全恢复账户访问权限。


1 回复

Rust区块链安全恢复库pallet-recovery的使用:实现Substrate链上账户多签与资产恢复功能

介绍

pallet-recovery是Substrate框架中的一个内置模块,用于实现区块链账户的安全恢复机制。它允许用户设置一组"恢复人"(recoverers),当账户私钥丢失时,可以通过这些恢复人的多数同意来重新获得账户控制权或恢复资产。

主要功能包括:

  • 多签账户恢复
  • 资产恢复
  • 社交恢复机制
  • 可配置的恢复阈值

使用方法

1. 在runtime中集成pallet-recovery

首先需要在你的Substrate链的runtime中集成这个pallet:

// runtime/src/lib.rs

impl pallet_recovery::Config for Runtime {
    type RuntimeEvent = RuntimeEvent;
    type RuntimeCall = RuntimeCall;
    type Currency = Balances;
    type ConfigDepositBase = ConstU128<100>;
    type FriendDepositFactor = ConstU128<1>;
    type MaxFriends = ConstU32<9>;
    type RecoveryDeposit = ConstU128<200>;
}

2. 创建恢复配置

账户所有者可以设置恢复配置:

// 设置3个恢复人,需要其中至少2人同意才能恢复账户
let recovery_config = pallet_recovery::RecoveryConfig {
    delay_period: 0,  // 恢复延迟区块数
    friends: vec![
        alice.into(),
        bob.into(),
        charlie.into()
    ],
    threshold: 2,  // 需要的签名数
};

3. 创建恢复请求

当账户丢失时,恢复人可以发起恢复请求:

// 恢复人alice发起对lost_account的恢复请求
let call = pallet_recovery::Call::initiate_recovery {
    account: lost_account.into()
};
RuntimeCall::Recovery(call).dispatch(origin_of(alice));

4. 其他恢复人确认

其他恢复人需要确认恢复请求:

// 恢复人bob确认对lost_account的恢复
let call = pallet_recovery::Call::vouch_recovery {
    lost: lost_account.into(),
    rescuer: alice.into()
};
RuntimeCall::Recovery(call).dispatch(origin_of(bob));

5. 执行恢复

达到阈值后,原始账户或新账户可以声明恢复:

// 新账户claim恢复的账户
let call = pallet_recovery::Call::claim_recovery {
    account: lost_account.into()
};
RuntimeCall::Recovery(call).dispatch(origin_of(new_account));

完整示例

use frame_support::{assert_ok, traits::Currency};
use sp_core::sr25519::Pair;
use sp_runtime::AccountId32;

// 创建测试环境
let mut ext = new_test_ext();
ext.execute_with(|| {
    // 创建账户
    let lost_pair = Pair::from_seed(b"lost_account_seed_________________".as_ref());
    let lost_account: AccountId32 = lost_pair.public().into();
    
    let alice_pair = Pair::from_seed(b"alice_seed_______________________".as_ref());
    let alice: AccountId32 = alice_pair.public().into();
    
    let bob_pair = Pair::from_seed(b"bob_seed_________________________".as_ref());
    let bob: AccountId32 = bob_pair.public().into();
    
    // 给账户分配资金
    let _ = Balances::make_free_balance_be(&lost_account, 1000);
    
    // 设置恢复配置
    assert_ok!(Recovery::create_recovery(
        RuntimeOrigin::signed(lost_account.clone()),
        vec![alice.clone(), bob.clone()],
        2
    ));
    
    // 模拟账户丢失,alice发起恢复
    assert_ok!(Recovery::initiate_recovery(
        RuntimeOrigin::signed(alice.clone()),
        lost_account.clone()
    ));
    
    // bob确认恢复
    assert_ok!(Recovery::vouch_recovery(
        RuntimeOrigin::signed(bob.clone()),
        lost_account.clone(),
        alice.clone()
    ));
    
    // 创建新账户并claim恢复
    let new_pair = Pair::from_seed(b"new_account_seed_________________".as_ref());
    let new_account: AccountId32 = new_pair.public().into();
    
    assert_ok!(Recovery::claim_recovery(
        RuntimeOrigin::signed(new_account.clone()),
        lost_account.clone()
    ));
    
    // 验证新账户现在可以控制原账户资金
    assert_eq!(Balances::free_balance(&new_account), 1000);
});

注意事项

  1. 恢复过程需要支付押金,防止滥用
  2. 恢复配置可以随时更新
  3. 恢复过程有延迟期,防止恶意恢复
  4. 恢复后原账户仍然存在,但控制权转移

pallet-recovery为Substrate链提供了一套完整的账户安全恢复机制,特别适合需要高安全性和容错能力的区块链应用场景。

回到顶部