我正在开发一份有管理员的合同。管理员只有他才能调用一些功能,而我则是从服务器调用它们。现在,我最大的担心是服务器被破坏了,有人得到了该管理员的私钥。
这就是为什么我想要放置一个函数,我可以用它来重置管理员。
该函数如下所示:
address private constant emergency_admin = 0x...;
address private admin = 0x...;
function emergency(address newa, bytes32 h, uint8 v, bytes32 r, bytes32 s,bytes32 h2, uint8 v2, bytes32 r2, bytes32 s2)
public
{
//check if the signed messages match the new addres
require(h==prefixedHash2(newa));
require(h2==prefixedHash2(newa));
//check if admin and emergency_admin signed the messages
require(ecrecover(h, v, r, s)==admin);
require(ecrecover(h2, v2, r2, s2)==emergency_admin);
//set new admin
admin = newa;
}
function prefixedHash2(address message)
private
pure
returns (bytes32)
{
bytes32 h = keccak256(message);
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHashe = keccak256(prefix, h);
return prefixedHashe;
}显然,emergency_admin私钥不会保存在服务器上。更像是在保险箱里的一张纸上。
我想知道这是否可以接受,或者是否有更好的解决方案,我还没有想到呢?
发布于 2018-06-13 22:51:23
这似乎是一种合理的方法,但不需要参数h和h2。我也会考虑你是否想要改变emergency_admin的能力,以防受到损害。
下面是您的代码的一小部分重写,它删除了h和h2,并允许更改两个管理帐户:
address private admin = 0x...;
address private emergencyAdmin = 0x...;
function emergency(
address newAdmin,
address newEmergencyAdmin,
uint8 v,
bytes32 r,
bytes32 s,
uint8 v2,
bytes32 r2,
bytes32 s2
)
public
{
bytes32 h = prefixedHash(abi.encodePacked(newAdmin, newEmergencyAdmin));
require(ecrecover(h, v, r, s) == admin);
require(ecrecover(h, v2, r2, s2) == emergencyAdmin);
admin = newAdmin;
emergencyAdmin = newEmergencyAdmin;
}
function prefixedHash(bytes message) private pure returns (bytes32) {
bytes32 h = keccak256(message);
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", h));
}https://ethereum.stackexchange.com/questions/51161
复制相似问题