-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3ba62fa
Showing
5 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Base | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module github.com/base-org/eip712sign | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/decred/dcrd/hdkeychain/v3 v3.1.1 | ||
github.com/ethereum/go-ethereum v1.12.0 | ||
github.com/tyler-smith/go-bip39 v1.1.0 | ||
) | ||
|
||
require ( | ||
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect | ||
github.com/decred/base58 v1.0.5 // indirect | ||
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect | ||
github.com/decred/dcrd/crypto/ripemd160 v1.0.2 // indirect | ||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect | ||
github.com/go-stack/stack v1.8.1 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect | ||
github.com/karalabe/usb v0.0.3-0.20230711191512-61db3e06439c // indirect | ||
golang.org/x/crypto v0.1.0 // indirect | ||
golang.org/x/sys v0.7.0 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8= | ||
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8= | ||
github.com/VictoriaMetrics/fastcache v1.6.0 h1:C/3Oi3EiBCqufydp1neRZkqcwmEiuRT9c3fqvvgKm5o= | ||
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7ISrnJIXKzwaspym5BTKGx93EI= | ||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= | ||
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= | ||
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= | ||
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= | ||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= | ||
github.com/cockroachdb/errors v1.9.1 h1:yFVvsI0VxmRShfawbt/laCIDy/mtTqqnvoNgiy5bEV8= | ||
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= | ||
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811 h1:ytcWPaNPhNoGMWEhDvS3zToKcDpRsLuRolQJBVGdozk= | ||
github.com/cockroachdb/redact v1.1.3 h1:AKZds10rFSIj7qADf0g46UixK8NNLwWTNdCIGS5wfSQ= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/dchest/siphash v1.2.3 h1:QXwFc8cFOR2dSa/gE6o/HokBMWtLUaNDVd+22aKHeEA= | ||
github.com/decred/base58 v1.0.5 h1:hwcieUM3pfPnE/6p3J100zoRfGkQxBulZHo7GZfOqic= | ||
github.com/decred/base58 v1.0.5/go.mod h1:s/8lukEHFA6bUQQb/v3rjUySJ2hu+RioCzLukAVkrfw= | ||
github.com/decred/dcrd/chaincfg/chainhash v1.0.4 h1:zRCv6tdncLfLTKYqu7hrXvs7hW+8FO/NvwoFvGsrluU= | ||
github.com/decred/dcrd/chaincfg/v3 v3.2.0 h1:6WxA92AGBkycEuWvxtZMvA76FbzbkDRoK8OGbsR2muk= | ||
github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= | ||
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= | ||
github.com/decred/dcrd/crypto/ripemd160 v1.0.2 h1:TvGTmUBHDU75OHro9ojPLK+Yv7gDl2hnUvRocRCjsys= | ||
github.com/decred/dcrd/crypto/ripemd160 v1.0.2/go.mod h1:uGfjDyePSpa75cSQLzNdVmWlbQMBuiJkvXw/MNKRY4M= | ||
github.com/decred/dcrd/dcrec v1.0.1 h1:gDzlndw0zYxM5BlaV17d7ZJV6vhRe9njPBFeg4Db2UY= | ||
github.com/decred/dcrd/dcrec/edwards/v2 v2.0.3 h1:l/lhv2aJCUignzls81+wvga0TFlyoZx8QxRMQgXpZik= | ||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= | ||
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= | ||
github.com/decred/dcrd/hdkeychain/v3 v3.1.1 h1:4WhyHNBy7ec6qBUC7Fq7JFVGSd7bpuR5H+AJRID8Lyk= | ||
github.com/decred/dcrd/hdkeychain/v3 v3.1.1/go.mod h1:HaabrLc27lnny5/Ph9+6I3szp0op5MCb7smEwlzfD60= | ||
github.com/decred/dcrd/txscript/v4 v4.1.0 h1:uEdcibIOl6BuWj3AqmXZ9xIK/qbo6lHY9aNk29FtkrU= | ||
github.com/decred/dcrd/wire v1.6.0 h1:YOGwPHk4nzGr6OIwUGb8crJYWDiVLpuMxfDBCCF7s/o= | ||
github.com/decred/slog v1.2.0 h1:soHAxV52B54Di3WtKLfPum9OFfWqwtf/ygf9njdfnPM= | ||
github.com/ethereum/go-ethereum v1.12.0 h1:bdnhLPtqETd4m3mS8BGMNvBTf36bO5bx/hxE2zljOa0= | ||
github.com/ethereum/go-ethereum v1.12.0/go.mod h1:/oo2X/dZLJjf2mJ6YT9wcWxa4nNJDBKDBU6sFIpx1Gs= | ||
github.com/getsentry/sentry-go v0.18.0 h1:MtBW5H9QgdcJabtZcuJG80BMOwaBpkRDZkxRkNC1sN0= | ||
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E= | ||
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= | ||
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= | ||
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= | ||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= | ||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= | ||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= | ||
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk= | ||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c h1:DZfsyhDK1hnSS5lH8l+JggqzEleHteTYfutAiVlSUM8= | ||
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw= | ||
github.com/karalabe/usb v0.0.3-0.20230711191512-61db3e06439c h1:AqsttAyEyIEsNz5WLRwuRwjiT5CMDUfLk6cFJDVPebs= | ||
github.com/karalabe/usb v0.0.3-0.20230711191512-61db3e06439c/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU= | ||
github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw= | ||
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= | ||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= | ||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw= | ||
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4= | ||
github.com/prometheus/common v0.39.0 h1:oOyhkDq05hPZKItWVBkJ6g6AtGxi+fy7F4JvUV8uhsI= | ||
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= | ||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= | ||
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU= | ||
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= | ||
github.com/tklauser/go-sysconf v0.3.5 h1:uu3Xl4nkLzQfXNsWn15rPc/HQCJKObbt1dKJeWp3vU4= | ||
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA= | ||
github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8= | ||
github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= | ||
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= | ||
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= | ||
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 h1:xP7rWLUr1e1n2xkK5YB4LI0hPEy3LJC6Wk+D4pGlOJg= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= | ||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df h1:5Pf6pFKu98ODmgnpvkJ3kFUOQGGLIzLIkbzUHp47618= | ||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | ||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= | ||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= | ||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= | ||
lukechampine.com/blake3 v1.2.1 h1:YuqqRuaqsGV71BV/nm9xlI0MKUv4QC54jQnBChWbGnI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"crypto/ecdsa" | ||
"encoding/hex" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/decred/dcrd/hdkeychain/v3" | ||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/accounts/usbwallet" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/tyler-smith/go-bip39" | ||
) | ||
|
||
func main() { | ||
var privateKey string | ||
var ledger bool | ||
var mnemonic string | ||
var hdPath string | ||
var prefix string | ||
var suffix string | ||
var workdir string | ||
flag.StringVar(&privateKey, "private-key", "", "Private key to use for signing") | ||
flag.BoolVar(&ledger, "ledger", false, "Use ledger device for signing") | ||
flag.StringVar(&mnemonic, "mnemonic", "", "Mnemonic to use for signing") | ||
flag.StringVar(&hdPath, "hd-paths", "m/44'/60'/0'/0/0", "Hierarchical deterministic derivation path for mnemonic or ledger") | ||
flag.StringVar(&prefix, "prefix", "vvvvvvvv", "String that prefixes the data to be signed") | ||
flag.StringVar(&suffix, "suffix", "^^^^^^^^", "String that suffixes the data to be signed") | ||
flag.StringVar(&workdir, "workdir", ".", "Directory in which to run the subprocess") | ||
flag.Parse() | ||
|
||
options := 0 | ||
if privateKey != "" { | ||
options++ | ||
} | ||
if ledger { | ||
options++ | ||
} | ||
if mnemonic != "" { | ||
options++ | ||
} | ||
if options != 1 { | ||
log.Fatalf("One (and only one) of --private-key, --ledger, --mnemonic must be set") | ||
} | ||
|
||
var input []byte | ||
var err error | ||
if flag.NArg() == 0 { | ||
input, err = io.ReadAll(os.Stdin) | ||
if err != nil { | ||
log.Fatalf("Error reading from stdin: %v", err) | ||
} | ||
} else { | ||
input, err = run(workdir, flag.Arg(0), flag.Args()[1:]...) | ||
if err != nil { | ||
log.Fatalf("Error running process: %v", err) | ||
} | ||
fmt.Printf("\n%s exited with code 0\n", flag.Arg(0)) | ||
} | ||
|
||
if index := strings.Index(string(input), prefix); prefix != "" && index >= 0 { | ||
input = input[index+len(prefix):] | ||
} | ||
if index := strings.Index(string(input), suffix); suffix != "" && index >= 0 { | ||
input = input[:index] | ||
} | ||
|
||
fmt.Println() | ||
hash := common.FromHex(strings.TrimSpace(string(input))) | ||
if len(hash) != 66 { | ||
log.Fatalf("Expected EIP-712 hex string with 66 bytes, got %d bytes, value: %s", len(input), string(input)) | ||
} | ||
|
||
s, err := createSigner(privateKey, mnemonic, hdPath) | ||
if err != nil { | ||
log.Fatalf("Error creating signer: %v", err) | ||
} | ||
|
||
domainHash := hash[2:34] | ||
messageHash := hash[34:66] | ||
fmt.Printf("Domain hash: 0x%s\n", hex.EncodeToString(domainHash)) | ||
fmt.Printf("Message hash: 0x%s\n", hex.EncodeToString(messageHash)) | ||
|
||
if ledger { | ||
fmt.Printf("Data sent to ledger, awaiting signature...") | ||
} | ||
signature, err := s.sign(hash) | ||
if ledger { | ||
fmt.Println("done") | ||
} | ||
if err != nil { | ||
log.Fatalf("Error signing data: %v", err) | ||
} | ||
|
||
fmt.Printf("\nData: 0x%s\n", hex.EncodeToString(hash)) | ||
fmt.Printf("Signer: %s\n", s.address().String()) | ||
fmt.Printf("Signature: %s\n", hex.EncodeToString(signature)) | ||
} | ||
|
||
func run(workdir, name string, args ...string) ([]byte, error) { | ||
cmd := exec.Command(name, args...) | ||
cmd.Dir = workdir | ||
|
||
var buffer bytes.Buffer | ||
cmd.Stdout = io.MultiWriter(os.Stdout, &buffer) | ||
cmd.Stderr = os.Stderr | ||
|
||
err := cmd.Run() | ||
return buffer.Bytes(), err | ||
} | ||
|
||
func createSigner(privateKey, mnemonic, hdPath string) (signer, error) { | ||
path, err := accounts.ParseDerivationPath(hdPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if privateKey != "" { | ||
key, err := crypto.HexToECDSA(privateKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("error parsing private key: %w", err) | ||
} | ||
return &ecdsaSigner{key}, nil | ||
} | ||
|
||
if mnemonic != "" { | ||
key, err := derivePrivateKey(mnemonic, path) | ||
if err != nil { | ||
return nil, fmt.Errorf("error deriving key from mnemonic: %w", err) | ||
} | ||
return &ecdsaSigner{key}, nil | ||
} | ||
|
||
// assume using a ledger | ||
ledgerHub, err := usbwallet.NewLedgerHub() | ||
if err != nil { | ||
return nil, fmt.Errorf("error starting ledger: %w", err) | ||
} | ||
wallets := ledgerHub.Wallets() | ||
if len(wallets) == 0 { | ||
return nil, fmt.Errorf("no ledgers found, please connect your ledger") | ||
} else if len(wallets) > 1 { | ||
return nil, fmt.Errorf("multiple ledgers found, please use one ledger at a time") | ||
} | ||
wallet := wallets[0] | ||
if err := wallet.Open(""); err != nil { | ||
return nil, fmt.Errorf("error opening ledger: %w", err) | ||
} | ||
account, err := wallet.Derive(path, true) | ||
if err != nil { | ||
return nil, fmt.Errorf("error deriving ledger account (have you unlocked?): %w", err) | ||
} | ||
return &walletSigner{ | ||
wallet: wallet, | ||
account: account, | ||
}, nil | ||
} | ||
|
||
type signer interface { | ||
address() common.Address | ||
sign([]byte) ([]byte, error) | ||
} | ||
|
||
type ecdsaSigner struct { | ||
*ecdsa.PrivateKey | ||
} | ||
|
||
func (s *ecdsaSigner) address() common.Address { | ||
return crypto.PubkeyToAddress(s.PublicKey) | ||
} | ||
|
||
func (s *ecdsaSigner) sign(data []byte) ([]byte, error) { | ||
sig, err := crypto.Sign(crypto.Keccak256(data), s.PrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sig[crypto.RecoveryIDOffset] += 27 | ||
return sig, err | ||
} | ||
|
||
type walletSigner struct { | ||
wallet accounts.Wallet | ||
account accounts.Account | ||
} | ||
|
||
func (s *walletSigner) address() common.Address { | ||
return s.account.Address | ||
} | ||
|
||
func (s *walletSigner) sign(data []byte) ([]byte, error) { | ||
return s.wallet.SignData(s.account, accounts.MimetypeTypedData, data) | ||
} | ||
|
||
func derivePrivateKey(mnemonic string, path accounts.DerivationPath) (*ecdsa.PrivateKey, error) { | ||
// Parse the seed string into the master BIP32 key. | ||
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
privKey, err := hdkeychain.NewMaster(seed, fakeNetworkParams{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, child := range path { | ||
privKey, err = privKey.Child(child) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
rawPrivKey, err := privKey.SerializedPrivKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return crypto.ToECDSA(rawPrivKey) | ||
} | ||
|
||
type fakeNetworkParams struct{} | ||
|
||
func (f fakeNetworkParams) HDPrivKeyVersion() [4]byte { | ||
return [4]byte{} | ||
} | ||
|
||
func (f fakeNetworkParams) HDPubKeyVersion() [4]byte { | ||
return [4]byte{} | ||
} |