Skip to content

Commit

Permalink
增加文档说明,主要说明实现合约来实现充提的方式
Browse files Browse the repository at this point in the history
  • Loading branch information
lmxdawn committed Jun 27, 2022
1 parent 5c4b3f6 commit bc0364f
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,112 @@ sendTransaction(string, string, *big.Int) (string, error)
# 一个简单的示例

github地址: [golang 实现加密货币的充值/提现/归集服务](https://github.com/lmxdawn/wallet)

# 特别说明

> 创建钱包的方式可以用 create2 创建合约,这样可以实现不用批量管理私钥,防止私钥丢失或者被盗。
- solidity

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./ERC20.sol";
contract Wallet {
address internal token = 0xDA0bab807633f07f013f94DD0E6A4F96F8742B53;
address internal hotWallet = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;
constructor() {
// send all tokens from this contract to hotwallet
IERC20(token).transfer(
hotWallet,
IERC20(token).balanceOf(address(this))
);
// selfdestruct to receive gas refund and reset nonce to 0
selfdestruct(payable(hotWallet));
}
}
contract Fabric {
function createContract(uint256 salt) public returns (address newAddr){
// get wallet init_code
bytes memory bytecode = type(Wallet).creationCode;
assembly {
let codeSize := mload(bytecode) // get size of init_bytecode
newAddr := create2(
0, // 0 wei
add(bytecode, 32), // the bytecode itself starts at the second slot. The first slot contains array length
codeSize, // size of init_code
salt // salt from function arguments
)
}
}
function getAddress(uint _salt)
public
view
returns (address)
{
bytes memory bytecode = type(Wallet).creationCode;
bytes32 hash = keccak256(
abi.encodePacked(bytes1(0xff), address(this), _salt, keccak256(bytecode))
);
// NOTE: cast last 20 bytes of hash to address
return address(uint160(uint(hash)));
}
function getBytecode() public pure returns (bytes memory) {
bytes memory bytecode = type(Wallet).creationCode;
return bytecode;
}
function getBytecode1() public pure returns (bytes1) {
return bytes1(0xff);
}
function getBytecode3(uint256 s) public pure returns (bytes memory) {
return abi.encodePacked(s);
}
function getBytecode2() public pure returns (bytes32) {
bytes memory bytecode = type(Wallet).creationCode;
return keccak256(bytecode);
}
}
```

- go

```go
code := "6080604052600080546001600160a01b031990811673da0bab807633f07f013f94dd0e6a4f96f8742b53179091556001805490911673ab8483f64d9c6d1ecf9b849ae677dd3315835cb217905534801561005857600080fd5b506000546001546040516370a0823160e01b81523060048201526001600160a01b039283169263a9059cbb92169083906370a0823190602401602060405180830381865afa1580156100ae573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d29190610150565b6040516001600160e01b031960e085901b1681526001600160a01b03909216600483015260248201526044016020604051808303816000875af115801561011d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101419190610169565b506001546001600160a01b0316ff5b60006020828403121561016257600080fd5b5051919050565b60006020828403121561017b57600080fd5b8151801515811461018b57600080fd5b939250505056fe"
codeB := common.Hex2Bytes(code)
codeHash := crypto.Keccak256Hash(codeB)
fmt.Println(codeHash)

address := common.HexToAddress("0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47")
fmt.Println(address)

fmt.Println(common.LeftPadBytes(big.NewInt(1).Bytes(), 32))
var buffer bytes.Buffer
buffer.Write(common.FromHex("0xff"))
buffer.Write(address.Bytes())
buffer.Write(common.Hex2Bytes("0x30"))
buffer.Write(codeHash.Bytes())

hash := crypto.Keccak256Hash([]byte{0xff}, address.Bytes(), common.LeftPadBytes(big.NewInt(1).Bytes(), 32), codeHash.Bytes())

//salt := common.LeftPadBytes(big.NewInt(1).Bytes(), 32)
//crypto.CreateAddress2(address, salt, codeHash.Bytes())

fmt.Println(common.BytesToAddress(hash[12:]))
```

0 comments on commit bc0364f

Please sign in to comment.