mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
Integrate IPA directly without submodule, fix address types
This commit is contained in:
@@ -199,6 +199,18 @@ func Keccak512(data ...[]byte) []byte {
|
||||
return d.Sum(nil)
|
||||
}
|
||||
|
||||
// HexToAddress returns Address with byte values of s.
|
||||
func HexToAddress(s string) Address {
|
||||
if len(s) >= 2 && (s[0:2] == "0x" || s[0:2] == "0X") {
|
||||
s = s[2:]
|
||||
}
|
||||
if len(s)%2 == 1 {
|
||||
s = "0" + s
|
||||
}
|
||||
b, _ := hex.DecodeString(s)
|
||||
return BytesToAddress(b)
|
||||
}
|
||||
|
||||
// CreateAddress creates an ethereum address given the bytes and the nonce
|
||||
func CreateAddress(b Address, nonce uint64) Address {
|
||||
data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
|
||||
|
||||
+7
-7
@@ -94,7 +94,7 @@ func TestUnmarshalPubkey(t *testing.T) {
|
||||
|
||||
func TestSign(t *testing.T) {
|
||||
key, _ := HexToECDSA(testPrivHex)
|
||||
addr := common.HexToAddress(testAddrHex)
|
||||
addr := HexToAddress(testAddrHex)
|
||||
|
||||
msg := Keccak256([]byte("foo"))
|
||||
sig, err := Sign(msg, key)
|
||||
@@ -133,7 +133,7 @@ func TestInvalidSign(t *testing.T) {
|
||||
|
||||
func TestNewContractAddress(t *testing.T) {
|
||||
key, _ := HexToECDSA(testPrivHex)
|
||||
addr := common.HexToAddress(testAddrHex)
|
||||
addr := HexToAddress(testAddrHex)
|
||||
genAddr := PubkeyToAddress(key.PublicKey)
|
||||
// sanity check before using addr to create contract address
|
||||
checkAddr(t, genAddr, addr)
|
||||
@@ -141,9 +141,9 @@ func TestNewContractAddress(t *testing.T) {
|
||||
caddr0 := CreateAddress(addr, 0)
|
||||
caddr1 := CreateAddress(addr, 1)
|
||||
caddr2 := CreateAddress(addr, 2)
|
||||
checkAddr(t, common.HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0)
|
||||
checkAddr(t, common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1)
|
||||
checkAddr(t, common.HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2)
|
||||
checkAddr(t, HexToAddress("333c3310824b7c685133f2bedb2ca4b8b4df633d"), caddr0)
|
||||
checkAddr(t, HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), caddr1)
|
||||
checkAddr(t, HexToAddress("c9ddedf451bc62ce88bf9292afb13df35b670699"), caddr2)
|
||||
}
|
||||
|
||||
func TestLoadECDSA(t *testing.T) {
|
||||
@@ -277,9 +277,9 @@ func checkhash(t *testing.T, name string, f func([]byte) []byte, msg, exp []byte
|
||||
}
|
||||
}
|
||||
|
||||
func checkAddr(t *testing.T, addr0, addr1 common.Address) {
|
||||
func checkAddr(t *testing.T, addr0, addr1 Address) {
|
||||
if addr0 != addr1 {
|
||||
t.Fatalf("address mismatch: want: %x have: %x", addr0, addr1)
|
||||
t.Fatalf("address mismatch: want: %s have: %s", addr0.Hex(), addr1.Hex())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,10 +10,12 @@ require (
|
||||
github.com/ethereum/c-kzg-4844/v2 v2.1.1
|
||||
github.com/google/gofuzz v1.2.0
|
||||
github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267
|
||||
github.com/leanovate/gopter v0.2.9
|
||||
github.com/luxfi/ids v1.0.1
|
||||
github.com/mr-tron/base58 v1.2.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
golang.org/x/crypto v0.40.0
|
||||
golang.org/x/sync v0.16.0
|
||||
golang.org/x/sys v0.34.0
|
||||
)
|
||||
|
||||
@@ -24,7 +26,6 @@ require (
|
||||
github.com/mmcloughlin/addchain v0.4.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/supranational/blst v0.3.15 // indirect
|
||||
golang.org/x/sync v0.16.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
rsc.io/tmplfunc v0.0.3 // indirect
|
||||
)
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
This directory contains code from the go-ipa project (https://github.com/crate-crypto/go-ipa)
|
||||
which is dual licensed under Apache 2.0 and MIT licenses.
|
||||
|
||||
The original license files are preserved as:
|
||||
- LICENSE-APACHE
|
||||
- LICENSE-MIT
|
||||
|
||||
================================================================================
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
See LICENSE-APACHE for full Apache 2.0 license text.
|
||||
|
||||
================================================================================
|
||||
|
||||
MIT License
|
||||
|
||||
See LICENSE-MIT for full MIT license text.
|
||||
|
||||
================================================================================
|
||||
|
||||
This code has been modified for compatibility with gnark-crypto v0.12.1
|
||||
and integrated into the luxfi/crypto package.
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
module github.com/luxfi/crypto/ipa
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/consensys/gnark-crypto v0.12.1
|
||||
github.com/leanovate/gopter v0.2.9
|
||||
golang.org/x/sync v0.1.0
|
||||
golang.org/x/sys v0.15.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bits-and-blooms/bitset v1.7.0 // indirect
|
||||
github.com/consensys/bavard v0.1.13 // indirect
|
||||
github.com/mmcloughlin/addchain v0.4.0 // indirect
|
||||
rsc.io/tmplfunc v0.0.3 // indirect
|
||||
)
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
github.com/bits-and-blooms/bitset v1.7.0 h1:YjAGVd3XmtK9ktAbX8Zg2g2PwLIMjGREZJHlV4j7NEo=
|
||||
github.com/bits-and-blooms/bitset v1.7.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
|
||||
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
|
||||
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
|
||||
github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M=
|
||||
github.com/consensys/gnark-crypto v0.12.1/go.mod h1:v2Gy7L/4ZRosZ7Ivs+9SfUDr0f5UlG+EM5t7MPHiLuY=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
|
||||
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
|
||||
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
|
||||
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
|
||||
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
|
||||
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
|
||||
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=
|
||||
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/luxfi/crypto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
key, _ := crypto.HexToECDSA("289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032")
|
||||
addr := crypto.HexToAddress("970e8128ab834e8eac17ab8e3812f010678cf791")
|
||||
genAddr := crypto.PubkeyToAddress(key.PublicKey)
|
||||
|
||||
fmt.Printf("addr: %s\n", addr.Hex())
|
||||
fmt.Printf("genAddr: %s\n", genAddr.Hex())
|
||||
fmt.Printf("Equal: %v\n", addr == genAddr)
|
||||
|
||||
caddr0 := crypto.CreateAddress(addr, 0)
|
||||
fmt.Printf("Contract addr 0: %s\n", caddr0.Hex())
|
||||
fmt.Printf("Expected: 0x333c3310824b7c685133f2bedb2ca4b8b4df633d\n")
|
||||
}
|
||||
Reference in New Issue
Block a user