diff options
author | obscuren <geffobscura@gmail.com> | 2014-10-08 18:01:36 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-10-08 18:01:36 +0800 |
commit | f3196c915a6f8ddde1d2e178ad419a114b608b91 (patch) | |
tree | 7c24facd291b1b15f153800d61cbc1cede56c26b /ethvm/address.go | |
parent | 9b60cf267a021027f47a588095e23d4c510658bd (diff) | |
download | dexon-f3196c915a6f8ddde1d2e178ad419a114b608b91.tar.gz dexon-f3196c915a6f8ddde1d2e178ad419a114b608b91.tar.zst dexon-f3196c915a6f8ddde1d2e178ad419a114b608b91.zip |
Precompiled crypto contracts
Diffstat (limited to 'ethvm/address.go')
-rw-r--r-- | ethvm/address.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/ethvm/address.go b/ethvm/address.go new file mode 100644 index 000000000..51e0d1cd7 --- /dev/null +++ b/ethvm/address.go @@ -0,0 +1,44 @@ +package ethvm + +import ( + "math/big" + + "github.com/ethereum/eth-go/ethcrypto" + "github.com/ethereum/eth-go/ethutil" +) + +type Address interface { + Call(in []byte) []byte +} + +type PrecompiledAddress struct { + Gas *big.Int + fn func(in []byte) []byte +} + +func (self PrecompiledAddress) Call(in []byte) []byte { + return self.fn(in) +} + +var Precompiled = map[uint64]*PrecompiledAddress{ + 1: &PrecompiledAddress{big.NewInt(500), ecrecoverFunc}, + 2: &PrecompiledAddress{big.NewInt(100), sha256Func}, + 3: &PrecompiledAddress{big.NewInt(100), ripemd160Func}, +} + +var NoAddr = PrecompiledAddress{} + +func sha256Func(in []byte) []byte { + return ethcrypto.Sha256(in) +} + +func ripemd160Func(in []byte) []byte { + return ethutil.RightPadBytes(ethcrypto.Ripemd160(in), 32) +} + +func ecrecoverFunc(in []byte) []byte { + // In case of an invalid sig. Defaults to return nil + defer func() { recover() }() + + return ethcrypto.Ecrecover(in) +} |