mirror of https://github.com/ethereum/go-ethereum
parent
f53ff0ff4a
commit
2c097007a6
@ -1,5 +0,0 @@ |
||||
**/*_test.go |
||||
|
||||
build/_workspace |
||||
build/_bin |
||||
tests/testdata |
@ -1,3 +0,0 @@ |
||||
# Auto detect text files and perform LF normalization |
||||
* text=auto |
||||
*.sol linguist-language=Solidity |
@ -1,24 +0,0 @@ |
||||
# Lines starting with '#' are comments. |
||||
# Each line is a file pattern followed by one or more owners. |
||||
|
||||
accounts/usbwallet @karalabe |
||||
accounts/scwallet @gballet |
||||
accounts/abi @gballet @MariusVanDerWijden |
||||
cmd/clef @holiman |
||||
cmd/puppeth @karalabe |
||||
consensus @karalabe |
||||
core/ @karalabe @holiman @rjl493456442 |
||||
eth/ @karalabe @holiman @rjl493456442 |
||||
eth/catalyst/ @gballet |
||||
eth/tracers/ @s1na |
||||
graphql/ @gballet @s1na |
||||
les/ @zsfelfoldi @rjl493456442 |
||||
light/ @zsfelfoldi @rjl493456442 |
||||
mobile/ @karalabe @ligi |
||||
node/ @fjl |
||||
p2p/ @fjl @zsfelfoldi |
||||
rpc/ @fjl @holiman |
||||
p2p/simulations @fjl |
||||
p2p/protocols @fjl |
||||
p2p/testing @fjl |
||||
signer/ @holiman |
@ -1,40 +0,0 @@ |
||||
# Contributing |
||||
|
||||
Thank you for considering to help out with the source code! We welcome |
||||
contributions from anyone on the internet, and are grateful for even the |
||||
smallest of fixes! |
||||
|
||||
If you'd like to contribute to go-ethereum, please fork, fix, commit and send a |
||||
pull request for the maintainers to review and merge into the main code base. If |
||||
you wish to submit more complex changes though, please check up with the core |
||||
devs first on [our gitter channel](https://gitter.im/ethereum/go-ethereum) to |
||||
ensure those changes are in line with the general philosophy of the project |
||||
and/or get some early feedback which can make both your efforts much lighter as |
||||
well as our review and merge procedures quick and simple. |
||||
|
||||
## Coding guidelines |
||||
|
||||
Please make sure your contributions adhere to our coding guidelines: |
||||
|
||||
* Code must adhere to the official Go |
||||
[formatting](https://golang.org/doc/effective_go.html#formatting) guidelines |
||||
(i.e. uses [gofmt](https://golang.org/cmd/gofmt/)). |
||||
* Code must be documented adhering to the official Go |
||||
[commentary](https://golang.org/doc/effective_go.html#commentary) guidelines. |
||||
* Pull requests need to be based on and opened against the `master` branch. |
||||
* Commit messages should be prefixed with the package(s) they modify. |
||||
* E.g. "eth, rpc: make trace configs optional" |
||||
|
||||
## Can I have feature X |
||||
|
||||
Before you submit a feature request, please check and make sure that it isn't |
||||
possible through some other means. The JavaScript-enabled console is a powerful |
||||
feature in the right hands. Please check our |
||||
[Geth documentation page](https://geth.ethereum.org/docs/) for more info |
||||
and help. |
||||
|
||||
## Configuration, dependencies, and tests |
||||
|
||||
Please see the [Developers' Guide](https://geth.ethereum.org/docs/developers/devguide) |
||||
for more details on configuring your environment, managing project dependencies |
||||
and testing procedures. |
@ -1,31 +0,0 @@ |
||||
--- |
||||
name: Report a bug |
||||
about: Something with go-ethereum is not working as expected |
||||
title: '' |
||||
labels: 'type:bug' |
||||
assignees: '' |
||||
--- |
||||
|
||||
#### System information |
||||
|
||||
Geth version: `geth version` |
||||
CL client & version: e.g. lighthouse/nimbus/prysm@v1.0.0 |
||||
OS & Version: Windows/Linux/OSX |
||||
Commit hash : (if `develop`) |
||||
|
||||
#### Expected behaviour |
||||
|
||||
|
||||
#### Actual behaviour |
||||
|
||||
|
||||
#### Steps to reproduce the behaviour |
||||
|
||||
|
||||
#### Backtrace |
||||
|
||||
```` |
||||
[backtrace] |
||||
```` |
||||
|
||||
When submitting logs: please submit them as text and not screenshots. |
@ -1,17 +0,0 @@ |
||||
--- |
||||
name: Request a feature |
||||
about: Report a missing feature - e.g. as a step before submitting a PR |
||||
title: '' |
||||
labels: 'type:feature' |
||||
assignees: '' |
||||
--- |
||||
|
||||
# Rationale |
||||
|
||||
Why should this feature exist? |
||||
What are the use-cases? |
||||
|
||||
# Implementation |
||||
|
||||
Do you have ideas regarding the implementation of this feature? |
||||
Are you willing to implement this feature? |
@ -1,9 +0,0 @@ |
||||
--- |
||||
name: Ask a question |
||||
about: Something is unclear |
||||
title: '' |
||||
labels: 'type:docs' |
||||
assignees: '' |
||||
--- |
||||
|
||||
This should only be used in very rare cases e.g. if you are not 100% sure if something is a bug or asking a question that leads to improving the documentation. For general questions please use [discord](https://discord.gg/nthXNEv) or the Ethereum stack exchange at https://ethereum.stackexchange.com. |
@ -1,11 +0,0 @@ |
||||
# Number of days of inactivity before an Issue is closed for lack of response |
||||
daysUntilClose: 30 |
||||
# Label requiring a response |
||||
responseRequiredLabel: "need:more-information" |
||||
# Comment to post when closing an Issue for lack of response. Set to `false` to disable |
||||
closeComment: > |
||||
This issue has been automatically closed because there has been no response |
||||
to our request for more information from the original author. With only the |
||||
information that is currently in the issue, we don't have enough information |
||||
to take action. Please reach out if you have more relevant information or |
||||
answers to our questions so that we can investigate further. |
@ -1,17 +0,0 @@ |
||||
# Number of days of inactivity before an issue becomes stale |
||||
daysUntilStale: 366 |
||||
# Number of days of inactivity before a stale issue is closed |
||||
daysUntilClose: 42 |
||||
# Issues with these labels will never be considered stale |
||||
exemptLabels: |
||||
- pinned |
||||
- security |
||||
# Label to use when marking an issue as stale |
||||
staleLabel: "status:inactive" |
||||
# Comment to post when marking an issue as stale. Set to `false` to disable |
||||
markComment: > |
||||
This issue has been automatically marked as stale because it has not had |
||||
recent activity. It will be closed if no further activity occurs. Thank you |
||||
for your contributions. |
||||
# Comment to post when closing a stale issue. Set to `false` to disable |
||||
closeComment: false |
@ -1,49 +0,0 @@ |
||||
# See http://help.github.com/ignore-files/ for more about ignoring files. |
||||
# |
||||
# If you find yourself ignoring temporary files generated by your text editor |
||||
# or operating system, you probably want to add a global ignore instead: |
||||
# git config --global core.excludesfile ~/.gitignore_global |
||||
|
||||
/tmp |
||||
*/**/*un~ |
||||
*/**/*.test |
||||
*un~ |
||||
.DS_Store |
||||
*/**/.DS_Store |
||||
.ethtest |
||||
*/**/*tx_database* |
||||
*/**/*dapps* |
||||
build/_vendor/pkg |
||||
|
||||
#* |
||||
.#* |
||||
*# |
||||
*~ |
||||
.project |
||||
.settings |
||||
|
||||
# used by the Makefile |
||||
/build/_workspace/ |
||||
/build/cache/ |
||||
/build/bin/ |
||||
/geth*.zip |
||||
|
||||
# travis |
||||
profile.tmp |
||||
profile.cov |
||||
|
||||
# IdeaIDE |
||||
.idea |
||||
|
||||
# VS Code |
||||
.vscode |
||||
|
||||
# dashboard |
||||
/dashboard/assets/flow-typed |
||||
/dashboard/assets/node_modules |
||||
/dashboard/assets/stats.json |
||||
/dashboard/assets/bundle.js |
||||
/dashboard/assets/bundle.js.map |
||||
/dashboard/assets/package-lock.json |
||||
|
||||
**/yarn-error.log |
@ -1,8 +0,0 @@ |
||||
[submodule "tests"] |
||||
path = tests/testdata |
||||
url = https://github.com/ethereum/tests |
||||
shallow = true |
||||
[submodule "evm-benchmarks"] |
||||
path = tests/evm-benchmarks |
||||
url = https://github.com/ipsilon/evm-benchmarks |
||||
shallow = true |
@ -1,64 +0,0 @@ |
||||
# This file configures github.com/golangci/golangci-lint. |
||||
|
||||
run: |
||||
timeout: 20m |
||||
tests: true |
||||
# default is true. Enables skipping of directories: |
||||
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ |
||||
skip-dirs-use-default: true |
||||
skip-files: |
||||
- core/genesis_alloc.go |
||||
|
||||
linters: |
||||
disable-all: true |
||||
enable: |
||||
- goconst |
||||
- goimports |
||||
- gosimple |
||||
- govet |
||||
- ineffassign |
||||
- misspell |
||||
- unconvert |
||||
- typecheck |
||||
- unused |
||||
- staticcheck |
||||
- bidichk |
||||
- durationcheck |
||||
- exportloopref |
||||
- whitespace |
||||
|
||||
# - structcheck # lots of false positives |
||||
# - errcheck #lot of false positives |
||||
# - contextcheck |
||||
# - errchkjson # lots of false positives |
||||
# - errorlint # this check crashes |
||||
# - exhaustive # silly check |
||||
# - makezero # false positives |
||||
# - nilerr # several intentional |
||||
|
||||
linters-settings: |
||||
gofmt: |
||||
simplify: true |
||||
goconst: |
||||
min-len: 3 # minimum length of string constant |
||||
min-occurrences: 6 # minimum number of occurrences |
||||
|
||||
issues: |
||||
exclude-rules: |
||||
- path: crypto/bn256/cloudflare/optate.go |
||||
linters: |
||||
- deadcode |
||||
- staticcheck |
||||
- path: internal/build/pgp.go |
||||
text: 'SA1019: "golang.org/x/crypto/openpgp" is deprecated: this package is unmaintained except for security fixes.' |
||||
- path: core/vm/contracts.go |
||||
text: 'SA1019: "golang.org/x/crypto/ripemd160" is deprecated: RIPEMD-160 is a legacy hash and should not be used for new applications.' |
||||
- path: accounts/usbwallet/trezor.go |
||||
text: 'SA1019: "github.com/golang/protobuf/proto" is deprecated: Use the "google.golang.org/protobuf/proto" package instead.' |
||||
- path: accounts/usbwallet/trezor/ |
||||
text: 'SA1019: "github.com/golang/protobuf/proto" is deprecated: Use the "google.golang.org/protobuf/proto" package instead.' |
||||
exclude: |
||||
- 'SA1019: event.TypeMux is deprecated: use Feed' |
||||
- 'SA1019: strings.Title is deprecated' |
||||
- 'SA1019: strings.Title has been deprecated since Go 1.18 and an alternative has been available since Go 1.0: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.' |
||||
- 'SA1029: should not use built-in type string as key for value' |
@ -1,247 +0,0 @@ |
||||
language: go |
||||
go_import_path: github.com/ethereum/go-ethereum |
||||
sudo: false |
||||
jobs: |
||||
allow_failures: |
||||
- stage: build |
||||
os: osx |
||||
go: 1.17.x |
||||
env: |
||||
- azure-osx |
||||
- azure-ios |
||||
- cocoapods-ios |
||||
|
||||
include: |
||||
# This builder only tests code linters on latest version of Go |
||||
- stage: lint |
||||
os: linux |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- lint |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
script: |
||||
- go run build/ci.go lint |
||||
|
||||
# These builders create the Docker sub-images for multi-arch push and each |
||||
# will attempt to push the multi-arch image if they are the last builder |
||||
- stage: build |
||||
if: type = push |
||||
os: linux |
||||
arch: amd64 |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- docker |
||||
services: |
||||
- docker |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
before_install: |
||||
- export DOCKER_CLI_EXPERIMENTAL=enabled |
||||
script: |
||||
- go run build/ci.go docker -image -manifest amd64,arm64 -upload ethereum/client-go |
||||
|
||||
- stage: build |
||||
if: type = push |
||||
os: linux |
||||
arch: arm64 |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- docker |
||||
services: |
||||
- docker |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
before_install: |
||||
- export DOCKER_CLI_EXPERIMENTAL=enabled |
||||
script: |
||||
- go run build/ci.go docker -image -manifest amd64,arm64 -upload ethereum/client-go |
||||
|
||||
# This builder does the Ubuntu PPA upload |
||||
- stage: build |
||||
if: type = push |
||||
os: linux |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- ubuntu-ppa |
||||
- GO111MODULE=on |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
addons: |
||||
apt: |
||||
packages: |
||||
- devscripts |
||||
- debhelper |
||||
- dput |
||||
- fakeroot |
||||
- python-bzrlib |
||||
- python-paramiko |
||||
script: |
||||
- echo '|1|7SiYPr9xl3uctzovOTj4gMwAC1M=|t6ReES75Bo/PxlOPJ6/GsGbTrM0= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0aKz5UTUndYgIGG7dQBV+HaeuEZJ2xPHo2DS2iSKvUL4xNMSAY4UguNW+pX56nAQmZKIZZ8MaEvSj6zMEDiq6HFfn5JcTlM80UwlnyKe8B8p7Nk06PPQLrnmQt5fh0HmEcZx+JU9TZsfCHPnX7MNz4ELfZE6cFsclClrKim3BHUIGq//t93DllB+h4O9LHjEUsQ1Sr63irDLSutkLJD6RXchjROXkNirlcNVHH/jwLWR5RcYilNX7S5bIkK8NlWPjsn/8Ua5O7I9/YoE97PpO6i73DTGLh5H9JN/SITwCKBkgSDWUt61uPK3Y11Gty7o2lWsBjhBUm2Y38CBsoGmBw==' >> ~/.ssh/known_hosts |
||||
- go run build/ci.go debsrc -upload ethereum/ethereum -sftp-user geth-ci -signer "Go Ethereum Linux Builder <geth-ci@ethereum.org>" |
||||
|
||||
# This builder does the Linux Azure uploads |
||||
- stage: build |
||||
if: type = push |
||||
os: linux |
||||
dist: bionic |
||||
sudo: required |
||||
go: 1.19.x |
||||
env: |
||||
- azure-linux |
||||
- GO111MODULE=on |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
addons: |
||||
apt: |
||||
packages: |
||||
- gcc-multilib |
||||
script: |
||||
# Build for the primary platforms that Trusty can manage |
||||
- go run build/ci.go install -dlgo |
||||
- go run build/ci.go archive -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
- go run build/ci.go install -dlgo -arch 386 |
||||
- go run build/ci.go archive -arch 386 -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
|
||||
# Switch over GCC to cross compilation (breaks 386, hence why do it here only) |
||||
- sudo -E apt-get -yq --no-install-suggests --no-install-recommends --force-yes install gcc-arm-linux-gnueabi libc6-dev-armel-cross gcc-arm-linux-gnueabihf libc6-dev-armhf-cross gcc-aarch64-linux-gnu libc6-dev-arm64-cross |
||||
- sudo ln -s /usr/include/asm-generic /usr/include/asm |
||||
|
||||
- GOARM=5 go run build/ci.go install -dlgo -arch arm -cc arm-linux-gnueabi-gcc |
||||
- GOARM=5 go run build/ci.go archive -arch arm -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
- GOARM=6 go run build/ci.go install -dlgo -arch arm -cc arm-linux-gnueabi-gcc |
||||
- GOARM=6 go run build/ci.go archive -arch arm -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
- GOARM=7 go run build/ci.go install -dlgo -arch arm -cc arm-linux-gnueabihf-gcc |
||||
- GOARM=7 go run build/ci.go archive -arch arm -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
- go run build/ci.go install -dlgo -arch arm64 -cc aarch64-linux-gnu-gcc |
||||
- go run build/ci.go archive -arch arm64 -type tar -signer LINUX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
|
||||
# This builder does the Android Maven and Azure uploads |
||||
- stage: build |
||||
if: type = push |
||||
os: linux |
||||
dist: bionic |
||||
addons: |
||||
apt: |
||||
packages: |
||||
- openjdk-8-jdk |
||||
env: |
||||
- azure-android |
||||
- maven-android |
||||
- GO111MODULE=on |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
before_install: |
||||
# Install Android and it's dependencies manually, Travis is stale |
||||
- export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 |
||||
- curl https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip -o android.zip |
||||
- unzip -q android.zip -d $HOME/sdk && rm android.zip |
||||
- mv $HOME/sdk/cmdline-tools $HOME/sdk/latest && mkdir $HOME/sdk/cmdline-tools && mv $HOME/sdk/latest $HOME/sdk/cmdline-tools |
||||
- export PATH=$PATH:$HOME/sdk/cmdline-tools/latest/bin |
||||
- export ANDROID_HOME=$HOME/sdk |
||||
|
||||
- yes | sdkmanager --licenses >/dev/null |
||||
- sdkmanager "platform-tools" "platforms;android-15" "platforms;android-19" "platforms;android-24" "ndk-bundle" |
||||
|
||||
# Install Go to allow building with |
||||
- curl https://dl.google.com/go/go1.18.linux-amd64.tar.gz | tar -xz |
||||
- export PATH=`pwd`/go/bin:$PATH |
||||
- export GOROOT=`pwd`/go |
||||
- export GOPATH=$HOME/go |
||||
script: |
||||
# Build the Android archive and upload it to Maven Central and Azure |
||||
- mkdir -p $GOPATH/src/github.com/ethereum |
||||
- ln -s `pwd` $GOPATH/src/github.com/ethereum/go-ethereum |
||||
- go run build/ci.go aar -signer ANDROID_SIGNING_KEY -signify SIGNIFY_KEY -deploy https://oss.sonatype.org -upload gethstore/builds |
||||
|
||||
# This builder does the OSX Azure, iOS CocoaPods and iOS Azure uploads |
||||
- stage: build |
||||
if: type = push |
||||
os: osx |
||||
go: 1.19.x |
||||
env: |
||||
- azure-osx |
||||
- azure-ios |
||||
- cocoapods-ios |
||||
- GO111MODULE=on |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
script: |
||||
- go run build/ci.go install -dlgo |
||||
- go run build/ci.go archive -type tar -signer OSX_SIGNING_KEY -signify SIGNIFY_KEY -upload gethstore/builds |
||||
|
||||
# Build the iOS framework and upload it to CocoaPods and Azure |
||||
- gem uninstall cocoapods -a -x |
||||
- gem install cocoapods |
||||
|
||||
- mv ~/.cocoapods/repos/master ~/.cocoapods/repos/master.bak |
||||
- sed -i '.bak' 's/repo.join/!repo.join/g' $(dirname `gem which cocoapods`)/cocoapods/sources_manager.rb |
||||
- if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then git clone --depth=1 https://github.com/CocoaPods/Specs.git ~/.cocoapods/repos/master && pod setup --verbose; fi |
||||
|
||||
- xctool -version |
||||
- xcrun simctl list |
||||
|
||||
# Workaround for https://github.com/golang/go/issues/23749 |
||||
- export CGO_CFLAGS_ALLOW='-fmodules|-fblocks|-fobjc-arc' |
||||
- go run build/ci.go xcode -signer IOS_SIGNING_KEY -signify SIGNIFY_KEY -deploy trunk -upload gethstore/builds |
||||
|
||||
# These builders run the tests |
||||
- stage: build |
||||
os: linux |
||||
arch: amd64 |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- GO111MODULE=on |
||||
script: |
||||
- go run build/ci.go test -coverage $TEST_PACKAGES |
||||
|
||||
- stage: build |
||||
if: type = pull_request |
||||
os: linux |
||||
arch: arm64 |
||||
dist: bionic |
||||
go: 1.18.x |
||||
env: |
||||
- GO111MODULE=on |
||||
script: |
||||
- go run build/ci.go test -coverage $TEST_PACKAGES |
||||
|
||||
- stage: build |
||||
os: linux |
||||
dist: bionic |
||||
go: 1.18.x |
||||
env: |
||||
- GO111MODULE=on |
||||
script: |
||||
- go run build/ci.go test -coverage $TEST_PACKAGES |
||||
|
||||
# This builder does the Azure archive purges to avoid accumulating junk |
||||
- stage: build |
||||
if: type = cron |
||||
os: linux |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- azure-purge |
||||
- GO111MODULE=on |
||||
git: |
||||
submodules: false # avoid cloning ethereum/tests |
||||
script: |
||||
- go run build/ci.go purge -store gethstore/builds -days 14 |
||||
|
||||
# This builder executes race tests |
||||
- stage: build |
||||
if: type = cron |
||||
os: linux |
||||
dist: bionic |
||||
go: 1.19.x |
||||
env: |
||||
- GO111MODULE=on |
||||
script: |
||||
- go run build/ci.go test -race -coverage $TEST_PACKAGES |
||||
|
@ -1,674 +0,0 @@ |
||||
GNU GENERAL PUBLIC LICENSE |
||||
Version 3, 29 June 2007 |
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> |
||||
Everyone is permitted to copy and distribute verbatim copies |
||||
of this license document, but changing it is not allowed. |
||||
|
||||
Preamble |
||||
|
||||
The GNU General Public License is a free, copyleft license for |
||||
software and other kinds of works. |
||||
|
||||
The licenses for most software and other practical works are designed |
||||
to take away your freedom to share and change the works. By contrast, |
||||
the GNU General Public License is intended to guarantee your freedom to |
||||
share and change all versions of a program--to make sure it remains free |
||||
software for all its users. We, the Free Software Foundation, use the |
||||
GNU General Public License for most of our software; it applies also to |
||||
any other work released this way by its authors. You can apply it to |
||||
your programs, too. |
||||
|
||||
When we speak of free software, we are referring to freedom, not |
||||
price. Our General Public Licenses are designed to make sure that you |
||||
have the freedom to distribute copies of free software (and charge for |
||||
them if you wish), that you receive source code or can get it if you |
||||
want it, that you can change the software or use pieces of it in new |
||||
free programs, and that you know you can do these things. |
||||
|
||||
To protect your rights, we need to prevent others from denying you |
||||
these rights or asking you to surrender the rights. Therefore, you have |
||||
certain responsibilities if you distribute copies of the software, or if |
||||
you modify it: responsibilities to respect the freedom of others. |
||||
|
||||
For example, if you distribute copies of such a program, whether |
||||
gratis or for a fee, you must pass on to the recipients the same |
||||
freedoms that you received. You must make sure that they, too, receive |
||||
or can get the source code. And you must show them these terms so they |
||||
know their rights. |
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps: |
||||
(1) assert copyright on the software, and (2) offer you this License |
||||
giving you legal permission to copy, distribute and/or modify it. |
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains |
||||
that there is no warranty for this free software. For both users' and |
||||
authors' sake, the GPL requires that modified versions be marked as |
||||
changed, so that their problems will not be attributed erroneously to |
||||
authors of previous versions. |
||||
|
||||
Some devices are designed to deny users access to install or run |
||||
modified versions of the software inside them, although the manufacturer |
||||
can do so. This is fundamentally incompatible with the aim of |
||||
protecting users' freedom to change the software. The systematic |
||||
pattern of such abuse occurs in the area of products for individuals to |
||||
use, which is precisely where it is most unacceptable. Therefore, we |
||||
have designed this version of the GPL to prohibit the practice for those |
||||
products. If such problems arise substantially in other domains, we |
||||
stand ready to extend this provision to those domains in future versions |
||||
of the GPL, as needed to protect the freedom of users. |
||||
|
||||
Finally, every program is threatened constantly by software patents. |
||||
States should not allow patents to restrict development and use of |
||||
software on general-purpose computers, but in those that do, we wish to |
||||
avoid the special danger that patents applied to a free program could |
||||
make it effectively proprietary. To prevent this, the GPL assures that |
||||
patents cannot be used to render the program non-free. |
||||
|
||||
The precise terms and conditions for copying, distribution and |
||||
modification follow. |
||||
|
||||
TERMS AND CONDITIONS |
||||
|
||||
0. Definitions. |
||||
|
||||
"This License" refers to version 3 of the GNU General Public License. |
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of |
||||
works, such as semiconductor masks. |
||||
|
||||
"The Program" refers to any copyrightable work licensed under this |
||||
License. Each licensee is addressed as "you". "Licensees" and |
||||
"recipients" may be individuals or organizations. |
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work |
||||
in a fashion requiring copyright permission, other than the making of an |
||||
exact copy. The resulting work is called a "modified version" of the |
||||
earlier work or a work "based on" the earlier work. |
||||
|
||||
A "covered work" means either the unmodified Program or a work based |
||||
on the Program. |
||||
|
||||
To "propagate" a work means to do anything with it that, without |
||||
permission, would make you directly or secondarily liable for |
||||
infringement under applicable copyright law, except executing it on a |
||||
computer or modifying a private copy. Propagation includes copying, |
||||
distribution (with or without modification), making available to the |
||||
public, and in some countries other activities as well. |
||||
|
||||
To "convey" a work means any kind of propagation that enables other |
||||
parties to make or receive copies. Mere interaction with a user through |
||||
a computer network, with no transfer of a copy, is not conveying. |
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices" |
||||
to the extent that it includes a convenient and prominently visible |
||||
feature that (1) displays an appropriate copyright notice, and (2) |
||||
tells the user that there is no warranty for the work (except to the |
||||
extent that warranties are provided), that licensees may convey the |
||||
work under this License, and how to view a copy of this License. If |
||||
the interface presents a list of user commands or options, such as a |
||||
menu, a prominent item in the list meets this criterion. |
||||
|
||||
1. Source Code. |
||||
|
||||
The "source code" for a work means the preferred form of the work |
||||
for making modifications to it. "Object code" means any non-source |
||||
form of a work. |
||||
|
||||
A "Standard Interface" means an interface that either is an official |
||||
standard defined by a recognized standards body, or, in the case of |
||||
interfaces specified for a particular programming language, one that |
||||
is widely used among developers working in that language. |
||||
|
||||
The "System Libraries" of an executable work include anything, other |
||||
than the work as a whole, that (a) is included in the normal form of |
||||
packaging a Major Component, but which is not part of that Major |
||||
Component, and (b) serves only to enable use of the work with that |
||||
Major Component, or to implement a Standard Interface for which an |
||||
implementation is available to the public in source code form. A |
||||
"Major Component", in this context, means a major essential component |
||||
(kernel, window system, and so on) of the specific operating system |
||||
(if any) on which the executable work runs, or a compiler used to |
||||
produce the work, or an object code interpreter used to run it. |
||||
|
||||
The "Corresponding Source" for a work in object code form means all |
||||
the source code needed to generate, install, and (for an executable |
||||
work) run the object code and to modify the work, including scripts to |
||||
control those activities. However, it does not include the work's |
||||
System Libraries, or general-purpose tools or generally available free |
||||
programs which are used unmodified in performing those activities but |
||||
which are not part of the work. For example, Corresponding Source |
||||
includes interface definition files associated with source files for |
||||
the work, and the source code for shared libraries and dynamically |
||||
linked subprograms that the work is specifically designed to require, |
||||
such as by intimate data communication or control flow between those |
||||
subprograms and other parts of the work. |
||||
|
||||
The Corresponding Source need not include anything that users |
||||
can regenerate automatically from other parts of the Corresponding |
||||
Source. |
||||
|
||||
The Corresponding Source for a work in source code form is that |
||||
same work. |
||||
|
||||
2. Basic Permissions. |
||||
|
||||
All rights granted under this License are granted for the term of |
||||
copyright on the Program, and are irrevocable provided the stated |
||||
conditions are met. This License explicitly affirms your unlimited |
||||
permission to run the unmodified Program. The output from running a |
||||
covered work is covered by this License only if the output, given its |
||||
content, constitutes a covered work. This License acknowledges your |
||||
rights of fair use or other equivalent, as provided by copyright law. |
||||
|
||||
You may make, run and propagate covered works that you do not |
||||
convey, without conditions so long as your license otherwise remains |
||||
in force. You may convey covered works to others for the sole purpose |
||||
of having them make modifications exclusively for you, or provide you |
||||
with facilities for running those works, provided that you comply with |
||||
the terms of this License in conveying all material for which you do |
||||
not control copyright. Those thus making or running the covered works |
||||
for you must do so exclusively on your behalf, under your direction |
||||
and control, on terms that prohibit them from making any copies of |
||||
your copyrighted material outside their relationship with you. |
||||
|
||||
Conveying under any other circumstances is permitted solely under |
||||
the conditions stated below. Sublicensing is not allowed; section 10 |
||||
makes it unnecessary. |
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law. |
||||
|
||||
No covered work shall be deemed part of an effective technological |
||||
measure under any applicable law fulfilling obligations under article |
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or |
||||
similar laws prohibiting or restricting circumvention of such |
||||
measures. |
||||
|
||||
When you convey a covered work, you waive any legal power to forbid |
||||
circumvention of technological measures to the extent such circumvention |
||||
is effected by exercising rights under this License with respect to |
||||
the covered work, and you disclaim any intention to limit operation or |
||||
modification of the work as a means of enforcing, against the work's |
||||
users, your or third parties' legal rights to forbid circumvention of |
||||
technological measures. |
||||
|
||||
4. Conveying Verbatim Copies. |
||||
|
||||
You may convey verbatim copies of the Program's source code as you |
||||
receive it, in any medium, provided that you conspicuously and |
||||
appropriately publish on each copy an appropriate copyright notice; |
||||
keep intact all notices stating that this License and any |
||||
non-permissive terms added in accord with section 7 apply to the code; |
||||
keep intact all notices of the absence of any warranty; and give all |
||||
recipients a copy of this License along with the Program. |
||||
|
||||
You may charge any price or no price for each copy that you convey, |
||||
and you may offer support or warranty protection for a fee. |
||||
|
||||
5. Conveying Modified Source Versions. |
||||
|
||||
You may convey a work based on the Program, or the modifications to |
||||
produce it from the Program, in the form of source code under the |
||||
terms of section 4, provided that you also meet all of these conditions: |
||||
|
||||
a) The work must carry prominent notices stating that you modified |
||||
it, and giving a relevant date. |
||||
|
||||
b) The work must carry prominent notices stating that it is |
||||
released under this License and any conditions added under section |
||||
7. This requirement modifies the requirement in section 4 to |
||||
"keep intact all notices". |
||||
|
||||
c) You must license the entire work, as a whole, under this |
||||
License to anyone who comes into possession of a copy. This |
||||
License will therefore apply, along with any applicable section 7 |
||||
additional terms, to the whole of the work, and all its parts, |
||||
regardless of how they are packaged. This License gives no |
||||
permission to license the work in any other way, but it does not |
||||
invalidate such permission if you have separately received it. |
||||
|
||||
d) If the work has interactive user interfaces, each must display |
||||
Appropriate Legal Notices; however, if the Program has interactive |
||||
interfaces that do not display Appropriate Legal Notices, your |
||||
work need not make them do so. |
||||
|
||||
A compilation of a covered work with other separate and independent |
||||
works, which are not by their nature extensions of the covered work, |
||||
and which are not combined with it such as to form a larger program, |
||||
in or on a volume of a storage or distribution medium, is called an |
||||
"aggregate" if the compilation and its resulting copyright are not |
||||
used to limit the access or legal rights of the compilation's users |
||||
beyond what the individual works permit. Inclusion of a covered work |
||||
in an aggregate does not cause this License to apply to the other |
||||
parts of the aggregate. |
||||
|
||||
6. Conveying Non-Source Forms. |
||||
|
||||
You may convey a covered work in object code form under the terms |
||||
of sections 4 and 5, provided that you also convey the |
||||
machine-readable Corresponding Source under the terms of this License, |
||||
in one of these ways: |
||||
|
||||
a) Convey the object code in, or embodied in, a physical product |
||||
(including a physical distribution medium), accompanied by the |
||||
Corresponding Source fixed on a durable physical medium |
||||
customarily used for software interchange. |
||||
|
||||
b) Convey the object code in, or embodied in, a physical product |
||||
(including a physical distribution medium), accompanied by a |
||||
written offer, valid for at least three years and valid for as |
||||
long as you offer spare parts or customer support for that product |
||||
model, to give anyone who possesses the object code either (1) a |
||||
copy of the Corresponding Source for all the software in the |
||||
product that is covered by this License, on a durable physical |
||||
medium customarily used for software interchange, for a price no |
||||
more than your reasonable cost of physically performing this |
||||
conveying of source, or (2) access to copy the |
||||
Corresponding Source from a network server at no charge. |
||||
|
||||
c) Convey individual copies of the object code with a copy of the |
||||
written offer to provide the Corresponding Source. This |
||||
alternative is allowed only occasionally and noncommercially, and |
||||
only if you received the object code with such an offer, in accord |
||||
with subsection 6b. |
||||
|
||||
d) Convey the object code by offering access from a designated |
||||
place (gratis or for a charge), and offer equivalent access to the |
||||
Corresponding Source in the same way through the same place at no |
||||
further charge. You need not require recipients to copy the |
||||
Corresponding Source along with the object code. If the place to |
||||
copy the object code is a network server, the Corresponding Source |
||||
may be on a different server (operated by you or a third party) |
||||
that supports equivalent copying facilities, provided you maintain |
||||
clear directions next to the object code saying where to find the |
||||
Corresponding Source. Regardless of what server hosts the |
||||
Corresponding Source, you remain obligated to ensure that it is |
||||
available for as long as needed to satisfy these requirements. |
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided |
||||
you inform other peers where the object code and Corresponding |
||||
Source of the work are being offered to the general public at no |
||||
charge under subsection 6d. |
||||
|
||||
A separable portion of the object code, whose source code is excluded |
||||
from the Corresponding Source as a System Library, need not be |
||||
included in conveying the object code work. |
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any |
||||
tangible personal property which is normally used for personal, family, |
||||
or household purposes, or (2) anything designed or sold for incorporation |
||||
into a dwelling. In determining whether a product is a consumer product, |
||||
doubtful cases shall be resolved in favor of coverage. For a particular |
||||
product received by a particular user, "normally used" refers to a |
||||
typical or common use of that class of product, regardless of the status |
||||
of the particular user or of the way in which the particular user |
||||
actually uses, or expects or is expected to use, the product. A product |
||||
is a consumer product regardless of whether the product has substantial |
||||
commercial, industrial or non-consumer uses, unless such uses represent |
||||
the only significant mode of use of the product. |
||||
|
||||
"Installation Information" for a User Product means any methods, |
||||
procedures, authorization keys, or other information required to install |
||||
and execute modified versions of a covered work in that User Product from |
||||
a modified version of its Corresponding Source. The information must |
||||
suffice to ensure that the continued functioning of the modified object |
||||
code is in no case prevented or interfered with solely because |
||||
modification has been made. |
||||
|
||||
If you convey an object code work under this section in, or with, or |
||||
specifically for use in, a User Product, and the conveying occurs as |
||||
part of a transaction in which the right of possession and use of the |
||||
User Product is transferred to the recipient in perpetuity or for a |
||||
fixed term (regardless of how the transaction is characterized), the |
||||
Corresponding Source conveyed under this section must be accompanied |
||||
by the Installation Information. But this requirement does not apply |
||||
if neither you nor any third party retains the ability to install |
||||
modified object code on the User Product (for example, the work has |
||||
been installed in ROM). |
||||
|
||||
The requirement to provide Installation Information does not include a |
||||
requirement to continue to provide support service, warranty, or updates |
||||
for a work that has been modified or installed by the recipient, or for |
||||
the User Product in which it has been modified or installed. Access to a |
||||
network may be denied when the modification itself materially and |
||||
adversely affects the operation of the network or violates the rules and |
||||
protocols for communication across the network. |
||||
|
||||
Corresponding Source conveyed, and Installation Information provided, |
||||
in accord with this section must be in a format that is publicly |
||||
documented (and with an implementation available to the public in |
||||
source code form), and must require no special password or key for |
||||
unpacking, reading or copying. |
||||
|
||||
7. Additional Terms. |
||||
|
||||
"Additional permissions" are terms that supplement the terms of this |
||||
License by making exceptions from one or more of its conditions. |
||||
Additional permissions that are applicable to the entire Program shall |
||||
be treated as though they were included in this License, to the extent |
||||
that they are valid under applicable law. If additional permissions |
||||
apply only to part of the Program, that part may be used separately |
||||
under those permissions, but the entire Program remains governed by |
||||
this License without regard to the additional permissions. |
||||
|
||||
When you convey a copy of a covered work, you may at your option |
||||
remove any additional permissions from that copy, or from any part of |
||||
it. (Additional permissions may be written to require their own |
||||
removal in certain cases when you modify the work.) You may place |
||||
additional permissions on material, added by you to a covered work, |
||||
for which you have or can give appropriate copyright permission. |
||||
|
||||
Notwithstanding any other provision of this License, for material you |
||||
add to a covered work, you may (if authorized by the copyright holders of |
||||
that material) supplement the terms of this License with terms: |
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the |
||||
terms of sections 15 and 16 of this License; or |
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or |
||||
author attributions in that material or in the Appropriate Legal |
||||
Notices displayed by works containing it; or |
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or |
||||
requiring that modified versions of such material be marked in |
||||
reasonable ways as different from the original version; or |
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or |
||||
authors of the material; or |
||||
|
||||
e) Declining to grant rights under trademark law for use of some |
||||
trade names, trademarks, or service marks; or |
||||
|
||||
f) Requiring indemnification of licensors and authors of that |
||||
material by anyone who conveys the material (or modified versions of |
||||
it) with contractual assumptions of liability to the recipient, for |
||||
any liability that these contractual assumptions directly impose on |
||||
those licensors and authors. |
||||
|
||||
All other non-permissive additional terms are considered "further |
||||
restrictions" within the meaning of section 10. If the Program as you |
||||
received it, or any part of it, contains a notice stating that it is |
||||
governed by this License along with a term that is a further |
||||
restriction, you may remove that term. If a license document contains |
||||
a further restriction but permits relicensing or conveying under this |
||||
License, you may add to a covered work material governed by the terms |
||||
of that license document, provided that the further restriction does |
||||
not survive such relicensing or conveying. |
||||
|
||||
If you add terms to a covered work in accord with this section, you |
||||
must place, in the relevant source files, a statement of the |
||||
additional terms that apply to those files, or a notice indicating |
||||
where to find the applicable terms. |
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the |
||||
form of a separately written license, or stated as exceptions; |
||||
the above requirements apply either way. |
||||
|
||||
8. Termination. |
||||
|
||||
You may not propagate or modify a covered work except as expressly |
||||
provided under this License. Any attempt otherwise to propagate or |
||||
modify it is void, and will automatically terminate your rights under |
||||
this License (including any patent licenses granted under the third |
||||
paragraph of section 11). |
||||
|
||||
However, if you cease all violation of this License, then your |
||||
license from a particular copyright holder is reinstated (a) |
||||
provisionally, unless and until the copyright holder explicitly and |
||||
finally terminates your license, and (b) permanently, if the copyright |
||||
holder fails to notify you of the violation by some reasonable means |
||||
prior to 60 days after the cessation. |
||||
|
||||
Moreover, your license from a particular copyright holder is |
||||
reinstated permanently if the copyright holder notifies you of the |
||||
violation by some reasonable means, this is the first time you have |
||||
received notice of violation of this License (for any work) from that |
||||
copyright holder, and you cure the violation prior to 30 days after |
||||
your receipt of the notice. |
||||
|
||||
Termination of your rights under this section does not terminate the |
||||
licenses of parties who have received copies or rights from you under |
||||
this License. If your rights have been terminated and not permanently |
||||
reinstated, you do not qualify to receive new licenses for the same |
||||
material under section 10. |
||||
|
||||
9. Acceptance Not Required for Having Copies. |
||||
|
||||
You are not required to accept this License in order to receive or |
||||
run a copy of the Program. Ancillary propagation of a covered work |
||||
occurring solely as a consequence of using peer-to-peer transmission |
||||
to receive a copy likewise does not require acceptance. However, |
||||
nothing other than this License grants you permission to propagate or |
||||
modify any covered work. These actions infringe copyright if you do |
||||
not accept this License. Therefore, by modifying or propagating a |
||||
covered work, you indicate your acceptance of this License to do so. |
||||
|
||||
10. Automatic Licensing of Downstream Recipients. |
||||
|
||||
Each time you convey a covered work, the recipient automatically |
||||
receives a license from the original licensors, to run, modify and |
||||
propagate that work, subject to this License. You are not responsible |
||||
for enforcing compliance by third parties with this License. |
||||
|
||||
An "entity transaction" is a transaction transferring control of an |
||||
organization, or substantially all assets of one, or subdividing an |
||||
organization, or merging organizations. If propagation of a covered |
||||
work results from an entity transaction, each party to that |
||||
transaction who receives a copy of the work also receives whatever |
||||
licenses to the work the party's predecessor in interest had or could |
||||
give under the previous paragraph, plus a right to possession of the |
||||
Corresponding Source of the work from the predecessor in interest, if |
||||
the predecessor has it or can get it with reasonable efforts. |
||||
|
||||
You may not impose any further restrictions on the exercise of the |
||||
rights granted or affirmed under this License. For example, you may |
||||
not impose a license fee, royalty, or other charge for exercise of |
||||
rights granted under this License, and you may not initiate litigation |
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that |
||||
any patent claim is infringed by making, using, selling, offering for |
||||
sale, or importing the Program or any portion of it. |
||||
|
||||
11. Patents. |
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this |
||||
License of the Program or a work on which the Program is based. The |
||||
work thus licensed is called the contributor's "contributor version". |
||||
|
||||
A contributor's "essential patent claims" are all patent claims |
||||
owned or controlled by the contributor, whether already acquired or |
||||
hereafter acquired, that would be infringed by some manner, permitted |
||||
by this License, of making, using, or selling its contributor version, |
||||
but do not include claims that would be infringed only as a |
||||
consequence of further modification of the contributor version. For |
||||
purposes of this definition, "control" includes the right to grant |
||||
patent sublicenses in a manner consistent with the requirements of |
||||
this License. |
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free |
||||
patent license under the contributor's essential patent claims, to |
||||
make, use, sell, offer for sale, import and otherwise run, modify and |
||||
propagate the contents of its contributor version. |
||||
|
||||
In the following three paragraphs, a "patent license" is any express |
||||
agreement or commitment, however denominated, not to enforce a patent |
||||
(such as an express permission to practice a patent or covenant not to |
||||
sue for patent infringement). To "grant" such a patent license to a |
||||
party means to make such an agreement or commitment not to enforce a |
||||
patent against the party. |
||||
|
||||
If you convey a covered work, knowingly relying on a patent license, |
||||
and the Corresponding Source of the work is not available for anyone |
||||
to copy, free of charge and under the terms of this License, through a |
||||
publicly available network server or other readily accessible means, |
||||
then you must either (1) cause the Corresponding Source to be so |
||||
available, or (2) arrange to deprive yourself of the benefit of the |
||||
patent license for this particular work, or (3) arrange, in a manner |
||||
consistent with the requirements of this License, to extend the patent |
||||
license to downstream recipients. "Knowingly relying" means you have |
||||
actual knowledge that, but for the patent license, your conveying the |
||||
covered work in a country, or your recipient's use of the covered work |
||||
in a country, would infringe one or more identifiable patents in that |
||||
country that you have reason to believe are valid. |
||||
|
||||
If, pursuant to or in connection with a single transaction or |
||||
arrangement, you convey, or propagate by procuring conveyance of, a |
||||
covered work, and grant a patent license to some of the parties |
||||
receiving the covered work authorizing them to use, propagate, modify |
||||
or convey a specific copy of the covered work, then the patent license |
||||
you grant is automatically extended to all recipients of the covered |
||||
work and works based on it. |
||||
|
||||
A patent license is "discriminatory" if it does not include within |
||||
the scope of its coverage, prohibits the exercise of, or is |
||||
conditioned on the non-exercise of one or more of the rights that are |
||||
specifically granted under this License. You may not convey a covered |
||||
work if you are a party to an arrangement with a third party that is |
||||
in the business of distributing software, under which you make payment |
||||
to the third party based on the extent of your activity of conveying |
||||
the work, and under which the third party grants, to any of the |
||||
parties who would receive the covered work from you, a discriminatory |
||||
patent license (a) in connection with copies of the covered work |
||||
conveyed by you (or copies made from those copies), or (b) primarily |
||||
for and in connection with specific products or compilations that |
||||
contain the covered work, unless you entered into that arrangement, |
||||
or that patent license was granted, prior to 28 March 2007. |
||||
|
||||
Nothing in this License shall be construed as excluding or limiting |
||||
any implied license or other defenses to infringement that may |
||||
otherwise be available to you under applicable patent law. |
||||
|
||||
12. No Surrender of Others' Freedom. |
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or |
||||
otherwise) that contradict the conditions of this License, they do not |
||||
excuse you from the conditions of this License. If you cannot convey a |
||||
covered work so as to satisfy simultaneously your obligations under this |
||||
License and any other pertinent obligations, then as a consequence you may |
||||
not convey it at all. For example, if you agree to terms that obligate you |
||||
to collect a royalty for further conveying from those to whom you convey |
||||
the Program, the only way you could satisfy both those terms and this |
||||
License would be to refrain entirely from conveying the Program. |
||||
|
||||
13. Use with the GNU Affero General Public License. |
||||
|
||||
Notwithstanding any other provision of this License, you have |
||||
permission to link or combine any covered work with a work licensed |
||||
under version 3 of the GNU Affero General Public License into a single |
||||
combined work, and to convey the resulting work. The terms of this |
||||
License will continue to apply to the part which is the covered work, |
||||
but the special requirements of the GNU Affero General Public License, |
||||
section 13, concerning interaction through a network will apply to the |
||||
combination as such. |
||||
|
||||
14. Revised Versions of this License. |
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of |
||||
the GNU General Public License from time to time. Such new versions will |
||||
be similar in spirit to the present version, but may differ in detail to |
||||
address new problems or concerns. |
||||
|
||||
Each version is given a distinguishing version number. If the |
||||
Program specifies that a certain numbered version of the GNU General |
||||
Public License "or any later version" applies to it, you have the |
||||
option of following the terms and conditions either of that numbered |
||||
version or of any later version published by the Free Software |
||||
Foundation. If the Program does not specify a version number of the |
||||
GNU General Public License, you may choose any version ever published |
||||
by the Free Software Foundation. |
||||
|
||||
If the Program specifies that a proxy can decide which future |
||||
versions of the GNU General Public License can be used, that proxy's |
||||
public statement of acceptance of a version permanently authorizes you |
||||
to choose that version for the Program. |
||||
|
||||
Later license versions may give you additional or different |
||||
permissions. However, no additional obligations are imposed on any |
||||
author or copyright holder as a result of your choosing to follow a |
||||
later version. |
||||
|
||||
15. Disclaimer of Warranty. |
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY |
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT |
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY |
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, |
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM |
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF |
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION. |
||||
|
||||
16. Limitation of Liability. |
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS |
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY |
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE |
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF |
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD |
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), |
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
||||
SUCH DAMAGES. |
||||
|
||||
17. Interpretation of Sections 15 and 16. |
||||
|
||||
If the disclaimer of warranty and limitation of liability provided |
||||
above cannot be given local legal effect according to their terms, |
||||
reviewing courts shall apply local law that most closely approximates |
||||
an absolute waiver of all civil liability in connection with the |
||||
Program, unless a warranty or assumption of liability accompanies a |
||||
copy of the Program in return for a fee. |
||||
|
||||
END OF TERMS AND CONDITIONS |
||||
|
||||
How to Apply These Terms to Your New Programs |
||||
|
||||
If you develop a new program, and you want it to be of the greatest |
||||
possible use to the public, the best way to achieve this is to make it |
||||
free software which everyone can redistribute and change under these terms. |
||||
|
||||
To do so, attach the following notices to the program. It is safest |
||||
to attach them to the start of each source file to most effectively |
||||
state the exclusion of warranty; and each file should have at least |
||||
the "copyright" line and a pointer to where the full notice is found. |
||||
|
||||
<one line to give the program's name and a brief idea of what it does.> |
||||
Copyright (C) <year> <name of author> |
||||
|
||||
This program is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
This program is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with this program. If not, see <https://www.gnu.org/licenses/>. |
||||
|
||||
Also add information on how to contact you by electronic and paper mail. |
||||
|
||||
If the program does terminal interaction, make it output a short |
||||
notice like this when it starts in an interactive mode: |
||||
|
||||
<program> Copyright (C) <year> <name of author> |
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. |
||||
This is free software, and you are welcome to redistribute it |
||||
under certain conditions; type `show c' for details. |
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate |
||||
parts of the General Public License. Of course, your program's commands |
||||
might be different; for a GUI interface, you would use an "about box". |
||||
|
||||
You should also get your employer (if you work as a programmer) or school, |
||||
if any, to sign a "copyright disclaimer" for the program, if necessary. |
||||
For more information on this, and how to apply and follow the GNU GPL, see |
||||
<https://www.gnu.org/licenses/>. |
||||
|
||||
The GNU General Public License does not permit incorporating your program |
||||
into proprietary programs. If your program is a subroutine library, you |
||||
may consider it more useful to permit linking proprietary applications with |
||||
the library. If this is what you want to do, use the GNU Lesser General |
||||
Public License instead of this License. But first, please read |
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>. |
@ -1,165 +0,0 @@ |
||||
GNU LESSER GENERAL PUBLIC LICENSE |
||||
Version 3, 29 June 2007 |
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> |
||||
Everyone is permitted to copy and distribute verbatim copies |
||||
of this license document, but changing it is not allowed. |
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates |
||||
the terms and conditions of version 3 of the GNU General Public |
||||
License, supplemented by the additional permissions listed below. |
||||
|
||||
0. Additional Definitions. |
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser |
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU |
||||
General Public License. |
||||
|
||||
"The Library" refers to a covered work governed by this License, |
||||
other than an Application or a Combined Work as defined below. |
||||
|
||||
An "Application" is any work that makes use of an interface provided |
||||
by the Library, but which is not otherwise based on the Library. |
||||
Defining a subclass of a class defined by the Library is deemed a mode |
||||
of using an interface provided by the Library. |
||||
|
||||
A "Combined Work" is a work produced by combining or linking an |
||||
Application with the Library. The particular version of the Library |
||||
with which the Combined Work was made is also called the "Linked |
||||
Version". |
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the |
||||
Corresponding Source for the Combined Work, excluding any source code |
||||
for portions of the Combined Work that, considered in isolation, are |
||||
based on the Application, and not on the Linked Version. |
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the |
||||
object code and/or source code for the Application, including any data |
||||
and utility programs needed for reproducing the Combined Work from the |
||||
Application, but excluding the System Libraries of the Combined Work. |
||||
|
||||
1. Exception to Section 3 of the GNU GPL. |
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License |
||||
without being bound by section 3 of the GNU GPL. |
||||
|
||||
2. Conveying Modified Versions. |
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a |
||||
facility refers to a function or data to be supplied by an Application |
||||
that uses the facility (other than as an argument passed when the |
||||
facility is invoked), then you may convey a copy of the modified |
||||
version: |
||||
|
||||
a) under this License, provided that you make a good faith effort to |
||||
ensure that, in the event an Application does not supply the |
||||
function or data, the facility still operates, and performs |
||||
whatever part of its purpose remains meaningful, or |
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of |
||||
this License applicable to that copy. |
||||
|
||||
3. Object Code Incorporating Material from Library Header Files. |
||||
|
||||
The object code form of an Application may incorporate material from |
||||
a header file that is part of the Library. You may convey such object |
||||
code under terms of your choice, provided that, if the incorporated |
||||
material is not limited to numerical parameters, data structure |
||||
layouts and accessors, or small macros, inline functions and templates |
||||
(ten or fewer lines in length), you do both of the following: |
||||
|
||||
a) Give prominent notice with each copy of the object code that the |
||||
Library is used in it and that the Library and its use are |
||||
covered by this License. |
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license |
||||
document. |
||||
|
||||
4. Combined Works. |
||||
|
||||
You may convey a Combined Work under terms of your choice that, |
||||
taken together, effectively do not restrict modification of the |
||||
portions of the Library contained in the Combined Work and reverse |
||||
engineering for debugging such modifications, if you also do each of |
||||
the following: |
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that |
||||
the Library is used in it and that the Library and its use are |
||||
covered by this License. |
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license |
||||
document. |
||||
|
||||
c) For a Combined Work that displays copyright notices during |
||||
execution, include the copyright notice for the Library among |
||||
these notices, as well as a reference directing the user to the |
||||
copies of the GNU GPL and this license document. |
||||
|
||||
d) Do one of the following: |
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this |
||||
License, and the Corresponding Application Code in a form |
||||
suitable for, and under terms that permit, the user to |
||||
recombine or relink the Application with a modified version of |
||||
the Linked Version to produce a modified Combined Work, in the |
||||
manner specified by section 6 of the GNU GPL for conveying |
||||
Corresponding Source. |
||||
|
||||
1) Use a suitable shared library mechanism for linking with the |
||||
Library. A suitable mechanism is one that (a) uses at run time |
||||
a copy of the Library already present on the user's computer |
||||
system, and (b) will operate properly with a modified version |
||||
of the Library that is interface-compatible with the Linked |
||||
Version. |
||||
|
||||
e) Provide Installation Information, but only if you would otherwise |
||||
be required to provide such information under section 6 of the |
||||
GNU GPL, and only to the extent that such information is |
||||
necessary to install and execute a modified version of the |
||||
Combined Work produced by recombining or relinking the |
||||
Application with a modified version of the Linked Version. (If |
||||
you use option 4d0, the Installation Information must accompany |
||||
the Minimal Corresponding Source and Corresponding Application |
||||
Code. If you use option 4d1, you must provide the Installation |
||||
Information in the manner specified by section 6 of the GNU GPL |
||||
for conveying Corresponding Source.) |
||||
|
||||
5. Combined Libraries. |
||||
|
||||
You may place library facilities that are a work based on the |
||||
Library side by side in a single library together with other library |
||||
facilities that are not Applications and are not covered by this |
||||
License, and convey such a combined library under terms of your |
||||
choice, if you do both of the following: |
||||
|
||||
a) Accompany the combined library with a copy of the same work based |
||||
on the Library, uncombined with any other library facilities, |
||||
conveyed under the terms of this License. |
||||
|
||||
b) Give prominent notice with the combined library that part of it |
||||
is a work based on the Library, and explaining where to find the |
||||
accompanying uncombined form of the same work. |
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License. |
||||
|
||||
The Free Software Foundation may publish revised and/or new versions |
||||
of the GNU Lesser General Public License from time to time. Such new |
||||
versions will be similar in spirit to the present version, but may |
||||
differ in detail to address new problems or concerns. |
||||
|
||||
Each version is given a distinguishing version number. If the |
||||
Library as you received it specifies that a certain numbered version |
||||
of the GNU Lesser General Public License "or any later version" |
||||
applies to it, you have the option of following the terms and |
||||
conditions either of that published version or of any later version |
||||
published by the Free Software Foundation. If the Library as you |
||||
received it does not specify a version number of the GNU Lesser |
||||
General Public License, you may choose any version of the GNU Lesser |
||||
General Public License ever published by the Free Software Foundation. |
||||
|
||||
If the Library as you received it specifies that a proxy can decide |
||||
whether future versions of the GNU Lesser General Public License shall |
||||
apply, that proxy's public statement of acceptance of any version is |
||||
permanent authorization for you to choose that version for the |
||||
Library. |
@ -1,33 +0,0 @@ |
||||
# Support setting various labels on the final image |
||||
ARG COMMIT="" |
||||
ARG VERSION="" |
||||
ARG BUILDNUM="" |
||||
|
||||
# Build Geth in a stock Go builder container |
||||
FROM golang:1.19-alpine as builder |
||||
|
||||
RUN apk add --no-cache gcc musl-dev linux-headers git |
||||
|
||||
# Get dependencies - will also be cached if we won't change go.mod/go.sum |
||||
COPY go.mod /go-ethereum/ |
||||
COPY go.sum /go-ethereum/ |
||||
RUN cd /go-ethereum && go mod download |
||||
|
||||
ADD . /go-ethereum |
||||
RUN cd /go-ethereum && go run build/ci.go install -static ./cmd/geth |
||||
|
||||
# Pull Geth into a second stage deploy alpine container |
||||
FROM alpine:latest |
||||
|
||||
RUN apk add --no-cache ca-certificates |
||||
COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ |
||||
|
||||
EXPOSE 8545 8546 30303 30303/udp |
||||
ENTRYPOINT ["geth"] |
||||
|
||||
# Add some metadata labels to help programatic image consumption |
||||
ARG COMMIT="" |
||||
ARG VERSION="" |
||||
ARG BUILDNUM="" |
||||
|
||||
LABEL commit="$COMMIT" version="$VERSION" buildnum="$BUILDNUM" |
@ -1,32 +0,0 @@ |
||||
# Support setting various labels on the final image |
||||
ARG COMMIT="" |
||||
ARG VERSION="" |
||||
ARG BUILDNUM="" |
||||
|
||||
# Build Geth in a stock Go builder container |
||||
FROM golang:1.19-alpine as builder |
||||
|
||||
RUN apk add --no-cache gcc musl-dev linux-headers git |
||||
|
||||
# Get dependencies - will also be cached if we won't change go.mod/go.sum |
||||
COPY go.mod /go-ethereum/ |
||||
COPY go.sum /go-ethereum/ |
||||
RUN cd /go-ethereum && go mod download |
||||
|
||||
ADD . /go-ethereum |
||||
RUN cd /go-ethereum && go run build/ci.go install -static |
||||
|
||||
# Pull all binaries into a second stage deploy alpine container |
||||
FROM alpine:latest |
||||
|
||||
RUN apk add --no-cache ca-certificates |
||||
COPY --from=builder /go-ethereum/build/bin/* /usr/local/bin/ |
||||
|
||||
EXPOSE 8545 8546 30303 30303/udp |
||||
|
||||
# Add some metadata labels to help programatic image consumption |
||||
ARG COMMIT="" |
||||
ARG VERSION="" |
||||
ARG BUILDNUM="" |
||||
|
||||
LABEL commit="$COMMIT" version="$VERSION" buildnum="$BUILDNUM" |
@ -1,50 +0,0 @@ |
||||
# This Makefile is meant to be used by people that do not usually work
|
||||
# with Go source code. If you know what GOPATH is then you probably
|
||||
# don't need to bother with make.
|
||||
|
||||
.PHONY: geth android ios evm all test clean |
||||
|
||||
GOBIN = ./build/bin
|
||||
GO ?= latest
|
||||
GORUN = env GO111MODULE=on go run
|
||||
|
||||
geth: |
||||
$(GORUN) build/ci.go install ./cmd/geth
|
||||
@echo "Done building."
|
||||
@echo "Run \"$(GOBIN)/geth\" to launch geth."
|
||||
|
||||
all: |
||||
$(GORUN) build/ci.go install
|
||||
|
||||
android: |
||||
$(GORUN) build/ci.go aar --local
|
||||
@echo "Done building."
|
||||
@echo "Import \"$(GOBIN)/geth.aar\" to use the library."
|
||||
@echo "Import \"$(GOBIN)/geth-sources.jar\" to add javadocs"
|
||||
@echo "For more info see https://stackoverflow.com/questions/20994336/android-studio-how-to-attach-javadoc"
|
||||
|
||||
ios: |
||||
$(GORUN) build/ci.go xcode --local
|
||||
@echo "Done building."
|
||||
@echo "Import \"$(GOBIN)/Geth.framework\" to use the library."
|
||||
|
||||
test: all |
||||
$(GORUN) build/ci.go test
|
||||
|
||||
lint: ## Run linters.
|
||||
$(GORUN) build/ci.go lint
|
||||
|
||||
clean: |
||||
env GO111MODULE=on go clean -cache
|
||||
rm -fr build/_workspace/pkg/ $(GOBIN)/*
|
||||
|
||||
# The devtools target installs tools required for 'go generate'.
|
||||
# You need to put $GOBIN (or $GOPATH/bin) in your PATH to use 'go generate'.
|
||||
|
||||
devtools: |
||||
env GOBIN= go install golang.org/x/tools/cmd/stringer@latest
|
||||
env GOBIN= go install github.com/fjl/gencodec@latest
|
||||
env GOBIN= go install github.com/golang/protobuf/protoc-gen-go@latest
|
||||
env GOBIN= go install ./cmd/abigen
|
||||
@type "solc" 2> /dev/null || echo 'Please install solc'
|
||||
@type "protoc" 2> /dev/null || echo 'Please install protoc'
|
@ -1,379 +0,0 @@ |
||||
## Go Ethereum |
||||
|
||||
Official Golang execution layer implementation of the Ethereum protocol. |
||||
|
||||
[![API Reference]( |
||||
https://camo.githubusercontent.com/915b7be44ada53c290eb157634330494ebe3e30a/68747470733a2f2f676f646f632e6f72672f6769746875622e636f6d2f676f6c616e672f6764646f3f7374617475732e737667 |
||||
)](https://pkg.go.dev/github.com/ethereum/go-ethereum?tab=doc) |
||||
[![Go Report Card](https://goreportcard.com/badge/github.com/ethereum/go-ethereum)](https://goreportcard.com/report/github.com/ethereum/go-ethereum) |
||||
[![Travis](https://travis-ci.com/ethereum/go-ethereum.svg?branch=master)](https://travis-ci.com/ethereum/go-ethereum) |
||||
[![Discord](https://img.shields.io/badge/discord-join%20chat-blue.svg)](https://discord.gg/nthXNEv) |
||||
|
||||
Automated builds are available for stable releases and the unstable master branch. Binary |
||||
archives are published at https://geth.ethereum.org/downloads/. |
||||
|
||||
## Building the source |
||||
|
||||
For prerequisites and detailed build instructions please read the [Installation Instructions](https://geth.ethereum.org/docs/install-and-build/installing-geth). |
||||
|
||||
Building `geth` requires both a Go (version 1.18 or later) and a C compiler. You can install |
||||
them using your favourite package manager. Once the dependencies are installed, run |
||||
|
||||
```shell |
||||
make geth |
||||
``` |
||||
|
||||
or, to build the full suite of utilities: |
||||
|
||||
```shell |
||||
make all |
||||
``` |
||||
|
||||
## Executables |
||||
|
||||
The go-ethereum project comes with several wrappers/executables found in the `cmd` |
||||
directory. |
||||
|
||||
| Command | Description | |
||||
| :-----------: | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
||||
| **`geth`** | Our main Ethereum CLI client. It is the entry point into the Ethereum network (main-, test- or private net), capable of running as a full node (default), archive node (retaining all historical state) or a light node (retrieving data live). It can be used by other processes as a gateway into the Ethereum network via JSON RPC endpoints exposed on top of HTTP, WebSocket and/or IPC transports. `geth --help` and the [CLI page](https://geth.ethereum.org/docs/interface/command-line-options) for command line options. | |
||||
| `clef` | Stand-alone signing tool, which can be used as a backend signer for `geth`. | |
||||
| `devp2p` | Utilities to interact with nodes on the networking layer, without running a full blockchain. | |
||||
| `abigen` | Source code generator to convert Ethereum contract definitions into easy-to-use, compile-time type-safe Go packages. It operates on plain [Ethereum contract ABIs](https://docs.soliditylang.org/en/develop/abi-spec.html) with expanded functionality if the contract bytecode is also available. However, it also accepts Solidity source files, making development much more streamlined. Please see our [Native DApps](https://geth.ethereum.org/docs/dapp/native-bindings) page for details. | |
||||
| `bootnode` | Stripped down version of our Ethereum client implementation that only takes part in the network node discovery protocol, but does not run any of the higher level application protocols. It can be used as a lightweight bootstrap node to aid in finding peers in private networks. | |
||||
| `evm` | Developer utility version of the EVM (Ethereum Virtual Machine) that is capable of running bytecode snippets within a configurable environment and execution mode. Its purpose is to allow isolated, fine-grained debugging of EVM opcodes (e.g. `evm --code 60ff60ff --debug run`). | |
||||
| `rlpdump` | Developer utility tool to convert binary RLP ([Recursive Length Prefix](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp)) dumps (data encoding used by the Ethereum protocol both network as well as consensus wise) to user-friendlier hierarchical representation (e.g. `rlpdump --hex CE0183FFFFFFC4C304050583616263`). | |
||||
| `puppeth` | a CLI wizard that aids in creating a new Ethereum network. | |
||||
|
||||
## Running `geth` |
||||
|
||||
Going through all the possible command line flags is out of scope here (please consult our |
||||
[CLI Wiki page](https://geth.ethereum.org/docs/interface/command-line-options)), |
||||
but we've enumerated a few common parameter combos to get you up to speed quickly |
||||
on how you can run your own `geth` instance. |
||||
|
||||
### Hardware Requirements |
||||
|
||||
Minimum: |
||||
|
||||
* CPU with 2+ cores |
||||
* 4GB RAM |
||||
* 1TB free storage space to sync the Mainnet |
||||
* 8 MBit/sec download Internet service |
||||
|
||||
Recommended: |
||||
|
||||
* Fast CPU with 4+ cores |
||||
* 16GB+ RAM |
||||
* High-performance SSD with at least 1TB of free space |
||||
* 25+ MBit/sec download Internet service |
||||
|
||||
### Full node on the main Ethereum network |
||||
|
||||
By far the most common scenario is people wanting to simply interact with the Ethereum |
||||
network: create accounts; transfer funds; deploy and interact with contracts. For this |
||||
particular use case, the user doesn't care about years-old historical data, so we can |
||||
sync quickly to the current state of the network. To do so: |
||||
|
||||
```shell |
||||
$ geth console |
||||
``` |
||||
|
||||
This command will: |
||||
* Start `geth` in snap sync mode (default, can be changed with the `--syncmode` flag), |
||||
causing it to download more data in exchange for avoiding processing the entire history |
||||
of the Ethereum network, which is very CPU intensive. |
||||
* Start the built-in interactive [JavaScript console](https://geth.ethereum.org/docs/interface/javascript-console), |
||||
(via the trailing `console` subcommand) through which you can interact using [`web3` methods](https://github.com/ChainSafe/web3.js/blob/0.20.7/DOCUMENTATION.md) |
||||
(note: the `web3` version bundled within `geth` is very old, and not up to date with official docs), |
||||
as well as `geth`'s own [management APIs](https://geth.ethereum.org/docs/rpc/server). |
||||
This tool is optional and if you leave it out you can always attach it to an already running |
||||
`geth` instance with `geth attach`. |
||||
|
||||
### A Full node on the Görli test network |
||||
|
||||
Transitioning towards developers, if you'd like to play around with creating Ethereum |
||||
contracts, you almost certainly would like to do that without any real money involved until |
||||
you get the hang of the entire system. In other words, instead of attaching to the main |
||||
network, you want to join the **test** network with your node, which is fully equivalent to |
||||
the main network, but with play-Ether only. |
||||
|
||||
```shell |
||||
$ geth --goerli console |
||||
``` |
||||
|
||||
The `console` subcommand has the same meaning as above and is equally |
||||
useful on the testnet too. |
||||
|
||||
Specifying the `--goerli` flag, however, will reconfigure your `geth` instance a bit: |
||||
|
||||
* Instead of connecting to the main Ethereum network, the client will connect to the Görli |
||||
test network, which uses different P2P bootnodes, different network IDs and genesis |
||||
states. |
||||
* Instead of using the default data directory (`~/.ethereum` on Linux for example), `geth` |
||||
will nest itself one level deeper into a `goerli` subfolder (`~/.ethereum/goerli` on |
||||
Linux). Note, on OSX and Linux this also means that attaching to a running testnet node |
||||
requires the use of a custom endpoint since `geth attach` will try to attach to a |
||||
production node endpoint by default, e.g., |
||||
`geth attach <datadir>/goerli/geth.ipc`. Windows users are not affected by |
||||
this. |
||||
|
||||
*Note: Although some internal protective measures prevent transactions from |
||||
crossing over between the main network and test network, you should always |
||||
use separate accounts for play and real money. Unless you manually move |
||||
accounts, `geth` will by default correctly separate the two networks and will not make any |
||||
accounts available between them.* |
||||
|
||||
### Full node on the Rinkeby test network |
||||
|
||||
Go Ethereum also supports connecting to the older proof-of-authority based test network |
||||
called [*Rinkeby*](https://www.rinkeby.io) which is operated by members of the community. |
||||
|
||||
```shell |
||||
$ geth --rinkeby console |
||||
``` |
||||
|
||||
### Full node on the Ropsten test network |
||||
|
||||
In addition to Görli and Rinkeby, Geth also supports the ancient Ropsten testnet. The |
||||
Ropsten test network is based on the Ethash proof-of-work consensus algorithm. As such, |
||||
it has certain extra overhead and is more susceptible to reorganization attacks due to the |
||||
network's low difficulty/security. |
||||
|
||||
```shell |
||||
$ geth --ropsten console |
||||
``` |
||||
|
||||
*Note: Older Geth configurations store the Ropsten database in the `testnet` subdirectory.* |
||||
|
||||
### Configuration |
||||
|
||||
As an alternative to passing the numerous flags to the `geth` binary, you can also pass a |
||||
configuration file via: |
||||
|
||||
```shell |
||||
$ geth --config /path/to/your_config.toml |
||||
``` |
||||
|
||||
To get an idea of how the file should look like you can use the `dumpconfig` subcommand to |
||||
export your existing configuration: |
||||
|
||||
```shell |
||||
$ geth --your-favourite-flags dumpconfig |
||||
``` |
||||
|
||||
*Note: This works only with `geth` v1.6.0 and above.* |
||||
|
||||
#### Docker quick start |
||||
|
||||
One of the quickest ways to get Ethereum up and running on your machine is by using |
||||
Docker: |
||||
|
||||
```shell |
||||
docker run -d --name ethereum-node -v /Users/alice/ethereum:/root \ |
||||
-p 8545:8545 -p 30303:30303 \ |
||||
ethereum/client-go |
||||
``` |
||||
|
||||
This will start `geth` in snap-sync mode with a DB memory allowance of 1GB, as the |
||||
above command does. It will also create a persistent volume in your home directory for |
||||
saving your blockchain as well as map the default ports. There is also an `alpine` tag |
||||
available for a slim version of the image. |
||||
|
||||
Do not forget `--http.addr 0.0.0.0`, if you want to access RPC from other containers |
||||
and/or hosts. By default, `geth` binds to the local interface and RPC endpoints are not |
||||
accessible from the outside. |
||||
|
||||
### Programmatically interfacing `geth` nodes |
||||
|
||||
As a developer, sooner rather than later you'll want to start interacting with `geth` and the |
||||
Ethereum network via your own programs and not manually through the console. To aid |
||||
this, `geth` has built-in support for a JSON-RPC based APIs ([standard APIs](https://ethereum.github.io/execution-apis/api-documentation/) |
||||
and [`geth` specific APIs](https://geth.ethereum.org/docs/rpc/server)). |
||||
These can be exposed via HTTP, WebSockets and IPC (UNIX sockets on UNIX based |
||||
platforms, and named pipes on Windows). |
||||
|
||||
The IPC interface is enabled by default and exposes all the APIs supported by `geth`, |
||||
whereas the HTTP and WS interfaces need to manually be enabled and only expose a |
||||
subset of APIs due to security reasons. These can be turned on/off and configured as |
||||
you'd expect. |
||||
|
||||
HTTP based JSON-RPC API options: |
||||
|
||||
* `--http` Enable the HTTP-RPC server |
||||
* `--http.addr` HTTP-RPC server listening interface (default: `localhost`) |
||||
* `--http.port` HTTP-RPC server listening port (default: `8545`) |
||||
* `--http.api` API's offered over the HTTP-RPC interface (default: `eth,net,web3`) |
||||
* `--http.corsdomain` Comma separated list of domains from which to accept cross origin requests (browser enforced) |
||||
* `--ws` Enable the WS-RPC server |
||||
* `--ws.addr` WS-RPC server listening interface (default: `localhost`) |
||||
* `--ws.port` WS-RPC server listening port (default: `8546`) |
||||
* `--ws.api` API's offered over the WS-RPC interface (default: `eth,net,web3`) |
||||
* `--ws.origins` Origins from which to accept WebSocket requests |
||||
* `--ipcdisable` Disable the IPC-RPC server |
||||
* `--ipcapi` API's offered over the IPC-RPC interface (default: `admin,debug,eth,miner,net,personal,txpool,web3`) |
||||
* `--ipcpath` Filename for IPC socket/pipe within the datadir (explicit paths escape it) |
||||
|
||||
You'll need to use your own programming environments' capabilities (libraries, tools, etc) to |
||||
connect via HTTP, WS or IPC to a `geth` node configured with the above flags and you'll |
||||
need to speak [JSON-RPC](https://www.jsonrpc.org/specification) on all transports. You |
||||
can reuse the same connection for multiple requests! |
||||
|
||||
**Note: Please understand the security implications of opening up an HTTP/WS based |
||||
transport before doing so! Hackers on the internet are actively trying to subvert |
||||
Ethereum nodes with exposed APIs! Further, all browser tabs can access locally |
||||
running web servers, so malicious web pages could try to subvert locally available |
||||
APIs!** |
||||
|
||||
### Operating a private network |
||||
|
||||
Maintaining your own private network is more involved as a lot of configurations taken for |
||||
granted in the official networks need to be manually set up. |
||||
|
||||
#### Defining the private genesis state |
||||
|
||||
First, you'll need to create the genesis state of your networks, which all nodes need to be |
||||
aware of and agree upon. This consists of a small JSON file (e.g. call it `genesis.json`): |
||||
|
||||
```json |
||||
{ |
||||
"config": { |
||||
"chainId": <arbitrary positive integer>, |
||||
"homesteadBlock": 0, |
||||
"eip150Block": 0, |
||||
"eip155Block": 0, |
||||
"eip158Block": 0, |
||||
"byzantiumBlock": 0, |
||||
"constantinopleBlock": 0, |
||||
"petersburgBlock": 0, |
||||
"istanbulBlock": 0, |
||||
"berlinBlock": 0, |
||||
"londonBlock": 0 |
||||
}, |
||||
"alloc": {}, |
||||
"coinbase": "0x0000000000000000000000000000000000000000", |
||||
"difficulty": "0x20000", |
||||
"extraData": "", |
||||
"gasLimit": "0x2fefd8", |
||||
"nonce": "0x0000000000000042", |
||||
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", |
||||
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", |
||||
"timestamp": "0x00" |
||||
} |
||||
``` |
||||
|
||||
The above fields should be fine for most purposes, although we'd recommend changing |
||||
the `nonce` to some random value so you prevent unknown remote nodes from being able |
||||
to connect to you. If you'd like to pre-fund some accounts for easier testing, create |
||||
the accounts and populate the `alloc` field with their addresses. |
||||
|
||||
```json |
||||
"alloc": { |
||||
"0x0000000000000000000000000000000000000001": { |
||||
"balance": "111111111" |
||||
}, |
||||
"0x0000000000000000000000000000000000000002": { |
||||
"balance": "222222222" |
||||
} |
||||
} |
||||
``` |
||||
|
||||
With the genesis state defined in the above JSON file, you'll need to initialize **every** |
||||
`geth` node with it prior to starting it up to ensure all blockchain parameters are correctly |
||||
set: |
||||
|
||||
```shell |
||||
$ geth init path/to/genesis.json |
||||
``` |
||||
|
||||
#### Creating the rendezvous point |
||||
|
||||
With all nodes that you want to run initialized to the desired genesis state, you'll need to |
||||
start a bootstrap node that others can use to find each other in your network and/or over |
||||
the internet. The clean way is to configure and run a dedicated bootnode: |
||||
|
||||
```shell |
||||
$ bootnode --genkey=boot.key |
||||
$ bootnode --nodekey=boot.key |
||||
``` |
||||
|
||||
With the bootnode online, it will display an [`enode` URL](https://ethereum.org/en/developers/docs/networking-layer/network-addresses/#enode) |
||||
that other nodes can use to connect to it and exchange peer information. Make sure to |
||||
replace the displayed IP address information (most probably `[::]`) with your externally |
||||
accessible IP to get the actual `enode` URL. |
||||
|
||||
*Note: You could also use a full-fledged `geth` node as a bootnode, but it's the less |
||||
recommended way.* |
||||
|
||||
#### Starting up your member nodes |
||||
|
||||
With the bootnode operational and externally reachable (you can try |
||||
`telnet <ip> <port>` to ensure it's indeed reachable), start every subsequent `geth` |
||||
node pointed to the bootnode for peer discovery via the `--bootnodes` flag. It will |
||||
probably also be desirable to keep the data directory of your private network separated, so |
||||
do also specify a custom `--datadir` flag. |
||||
|
||||
```shell |
||||
$ geth --datadir=path/to/custom/data/folder --bootnodes=<bootnode-enode-url-from-above> |
||||
``` |
||||
|
||||
*Note: Since your network will be completely cut off from the main and test networks, you'll |
||||
also need to configure a miner to process transactions and create new blocks for you.* |
||||
|
||||
#### Running a private miner |
||||
|
||||
Mining on the public Ethereum network is a complex task as it's only feasible using GPUs, |
||||
requiring an OpenCL or CUDA enabled `ethminer` instance. For information on such a |
||||
setup, please consult the [EtherMining subreddit](https://www.reddit.com/r/EtherMining/) |
||||
and the [ethminer](https://github.com/ethereum-mining/ethminer) repository. |
||||
|
||||
In a private network setting, however, a single CPU miner instance is more than enough for |
||||
practical purposes as it can produce a stable stream of blocks at the correct intervals |
||||
without needing heavy resources (consider running on a single thread, no need for multiple |
||||
ones either). To start a `geth` instance for mining, run it with all your usual flags, extended |
||||
by: |
||||
|
||||
```shell |
||||
$ geth <usual-flags> --mine --miner.threads=1 --miner.etherbase=0x0000000000000000000000000000000000000000 |
||||
``` |
||||
|
||||
Which will start mining blocks and transactions on a single CPU thread, crediting all |
||||
proceedings to the account specified by `--miner.etherbase`. You can further tune the mining |
||||
by changing the default gas limit blocks converge to (`--miner.targetgaslimit`) and the price |
||||
transactions are accepted at (`--miner.gasprice`). |
||||
|
||||
## Contribution |
||||
|
||||
Thank you for considering helping out with the source code! We welcome contributions |
||||
from anyone on the internet, and are grateful for even the smallest of fixes! |
||||
|
||||
If you'd like to contribute to go-ethereum, please fork, fix, commit and send a pull request |
||||
for the maintainers to review and merge into the main code base. If you wish to submit |
||||
more complex changes though, please check up with the core devs first on [our Discord Server](https://discord.gg/invite/nthXNEv) |
||||
to ensure those changes are in line with the general philosophy of the project and/or get |
||||
some early feedback which can make both your efforts much lighter as well as our review |
||||
and merge procedures quick and simple. |
||||
|
||||
Please make sure your contributions adhere to our coding guidelines: |
||||
|
||||
* Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) |
||||
guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)). |
||||
* Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) |
||||
guidelines. |
||||
* Pull requests need to be based on and opened against the `master` branch. |
||||
* Commit messages should be prefixed with the package(s) they modify. |
||||
* E.g. "eth, rpc: make trace configs optional" |
||||
|
||||
Please see the [Developers' Guide](https://geth.ethereum.org/docs/developers/devguide) |
||||
for more details on configuring your environment, managing project dependencies, and |
||||
testing procedures. |
||||
|
||||
## License |
||||
|
||||
The go-ethereum library (i.e. all code outside of the `cmd` directory) is licensed under the |
||||
[GNU Lesser General Public License v3.0](https://www.gnu.org/licenses/lgpl-3.0.en.html), |
||||
also included in our repository in the `COPYING.LESSER` file. |
||||
|
||||
The go-ethereum binaries (i.e. all code inside of the `cmd` directory) are licensed under the |
||||
[GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.en.html), also |
||||
included in our repository in the `COPYING` file. |
@ -1,175 +0,0 @@ |
||||
# Security Policy |
||||
|
||||
## Supported Versions |
||||
|
||||
Please see [Releases](https://github.com/ethereum/go-ethereum/releases). We recommend using the [most recently released version](https://github.com/ethereum/go-ethereum/releases/latest). |
||||
|
||||
## Audit reports |
||||
|
||||
Audit reports are published in the `docs` folder: https://github.com/ethereum/go-ethereum/tree/master/docs/audits |
||||
|
||||
| Scope | Date | Report Link | |
||||
| ------- | ------- | ----------- | |
||||
| `geth` | 20170425 | [pdf](https://github.com/ethereum/go-ethereum/blob/master/docs/audits/2017-04-25_Geth-audit_Truesec.pdf) | |
||||
| `clef` | 20180914 | [pdf](https://github.com/ethereum/go-ethereum/blob/master/docs/audits/2018-09-14_Clef-audit_NCC.pdf) | |
||||
| `Discv5` | 20191015 | [pdf](https://github.com/ethereum/go-ethereum/blob/master/docs/audits/2019-10-15_Discv5_audit_LeastAuthority.pdf) | |
||||
| `Discv5` | 20200124 | [pdf](https://github.com/ethereum/go-ethereum/blob/master/docs/audits/2020-01-24_DiscV5_audit_Cure53.pdf) | |
||||
|
||||
## Reporting a Vulnerability |
||||
|
||||
**Please do not file a public ticket** mentioning the vulnerability. |
||||
|
||||
To find out how to disclose a vulnerability in Ethereum visit [https://bounty.ethereum.org](https://bounty.ethereum.org) or email bounty@ethereum.org. Please read the [disclosure page](https://github.com/ethereum/go-ethereum/security/advisories?state=published) for more information about publicly disclosed security vulnerabilities. |
||||
|
||||
Use the built-in `geth version-check` feature to check whether the software is affected by any known vulnerability. This command will fetch the latest [`vulnerabilities.json`](https://geth.ethereum.org/docs/vulnerabilities/vulnerabilities.json) file which contains known security vulnerabilities concerning `geth`, and cross-check the data against its own version number. |
||||
|
||||
The following key may be used to communicate sensitive information to developers. |
||||
|
||||
Fingerprint: `AE96 ED96 9E47 9B00 84F3 E17F E88D 3334 FA5F 6A0A` |
||||
|
||||
``` |
||||
-----BEGIN PGP PUBLIC KEY BLOCK----- |
||||
Version: SKS 1.1.6 |
||||
Comment: Hostname: pgp.mit.edu |
||||
|
||||
mQINBFgl3tgBEAC8A1tUBkD9YV+eLrOmtgy+/JS/H9RoZvkg3K1WZ8IYfj6iIRaYneAk3Bp1 |
||||
82GUPVz/zhKr2g0tMXIScDR3EnaDsY+Qg+JqQl8NOG+Cikr1nnkG2on9L8c8yiqry1ZTCmYM |
||||
qCa2acTFqnyuXJ482aZNtB4QG2BpzfhW4k8YThpegk/EoRUim+y7buJDtoNf7YILlhDQXN8q |
||||
lHB02DWOVUihph9tUIFsPK6BvTr9SIr/eG6j6k0bfUo9pexOn7LS4SojoJmsm/5dp6AoKlac |
||||
48cZU5zwR9AYcq/nvkrfmf2WkObg/xRdEvKZzn05jRopmAIwmoC3CiLmqCHPmT5a29vEob/y |
||||
PFE335k+ujjZCPOu7OwjzDk7M0zMSfnNfDq8bXh16nn+ueBxJ0NzgD1oC6c2PhM+XRQCXCho |
||||
yI8vbfp4dGvCvYqvQAE1bWjqnumZ/7vUPgZN6gDfiAzG2mUxC2SeFBhacgzDvtQls+uuvm+F |
||||
nQOUgg2Hh8x2zgoZ7kqV29wjaUPFREuew7e+Th5BxielnzOfVycVXeSuvvIn6cd3g/s8mX1c |
||||
2kLSXJR7+KdWDrIrR5Az0kwAqFZt6B6QTlDrPswu3mxsm5TzMbny0PsbL/HBM+GZEZCjMXxB |
||||
8bqV2eSaktjnSlUNX1VXxyOxXA+ZG2jwpr51egi57riVRXokrQARAQABtDRFdGhlcmV1bSBG |
||||
b3VuZGF0aW9uIEJ1ZyBCb3VudHkgPGJvdW50eUBldGhlcmV1bS5vcmc+iQIcBBEBCAAGBQJa |
||||
FCY6AAoJEHoMA3Q0/nfveH8P+gJBPo9BXZL8isUfbUWjwLi81Yi70hZqIJUnz64SWTqBzg5b |
||||
mCZ69Ji5637THsxQetS2ARabz0DybQ779FhD/IWnqV9T3KuBM/9RzJtuhLzKCyMrAINPMo28 |
||||
rKWdunHHarpuR4m3tL2zWJkle5QVYb+vkZXJJE98PJw+N4IYeKKeCs2ubeqZu636GA0sMzzB |
||||
Jn3m/dRRA2va+/zzbr6F6b51ynzbMxWKTsJnstjC8gs8EeI+Zcd6otSyelLtCUkk3h5sTvpV |
||||
Wv67BNSU0BYsMkxyFi9PUyy07Wixgeas89K5jG1oOtDva/FkpRHrTE/WA5OXDRcLrHJM+SwD |
||||
CwqcLQqJd09NxwUW1iKeBmPptTiOGu1Gv2o7aEyoaWrHRBO7JuYrQrj6q2B3H1Je0zjAd2qt |
||||
09ni2bLwLn4LA+VDpprNTO+eZDprv09s2oFSU6NwziHybovu0y7X4pADGkK2evOM7c86PohX |
||||
QRQ1M1T16xLj6wP8/Ykwl6v/LUk7iDPXP3GPILnh4YOkwBR3DsCOPn8098xy7FxEELmupRzt |
||||
Cj9oC7YAoweeShgUjBPzb+nGY1m6OcFfbUPBgFyMMfwF6joHbiVIO+39+Ut2g2ysZa7KF+yp |
||||
XqVDqyEkYXsOLb25OC7brt8IJEPgBPwcHK5GNag6RfLxnQV+iVZ9KNH1yQgSiQI+BBMBAgAo |
||||
AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAUCWglh+gUJBaNgWAAKCRDojTM0+l9qCgQ2 |
||||
D/4udJpV4zGIZW1yNaVvtd3vfKsTLi7GIRJLUBqVb2Yx/uhnN8jTl/tAhCVosCQ1pzvi9kMl |
||||
s8qO1vu2kw5EWFFkwK96roI8pTql3VIjwhRVQrCkR7oAk/eUd1U/nt2q6J4UTYeVgqbq4dsI |
||||
ZZTRyPJMD667YpuAIcaah+w9j/E5xksYQdMeprnDrQkkBCb4FIMqfDzBPKvEa8DcQr949K85 |
||||
kxhr6LDq9i5l4Egxt2JdH8DaR4GLca6+oHy0MyPs/bZOsfmZUObfM2oZgPpqYM96JanhzO1j |
||||
dpnItyBii2pc+kNx5nMOf4eikE/MBv+WUJ0TttWzApGGmFUzDhtuEvRH9NBjtJ/pMrYspIGu |
||||
O/QNY5KKOKQTvVIlwGcm8dTsSkqtBDSUwZyWbfKfKOI1/RhM9dC3gj5/BOY57DYYV4rdTK01 |
||||
ZtYjuhdfs2bhuP1uF/cgnSSZlv8azvf7Egh7tHPnYxvLjfq1bJAhCIX0hNg0a81/ndPAEFky |
||||
fSko+JPKvdSvsUcSi2QQ4U2HX//jNBjXRfG4F0utgbJnhXzEckz6gqt7wSDZH2oddVuO8Ssc |
||||
T7sK+CdXthSKnRyuI+sGUpG+6glpKWIfYkWFKNZWuQ+YUatY3QEDHXTIioycSmV8p4d/g/0S |
||||
V6TegidLxY8bXMkbqz+3n6FArRffv5MH7qt3cYkCPgQTAQIAKAUCWCXhOwIbAwUJAeEzgAYL |
||||
CQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQ6I0zNPpfagrN/w/+Igp3vtYdNunikw3yHnYf |
||||
Jkm0MmaMDUM9mtsaXVN6xb9n25N3Xa3GWCpmdsbYZ8334tI/oQ4/NHq/bEI5WFH5F1aFkMkm |
||||
5AJVLuUkipCtmCZ5NkbRPJA9l0uNUUE6uuFXBhf4ddu7jb0jMetRF/kifJHVCCo5fISUNhLp |
||||
7bwcWq9qgDQNZNYMOo4s9WX5Tl+5x4gTZdd2/cAYt49h/wnkw+huM+Jm0GojpLqIQ1jZiffm |
||||
otf5rF4L+JhIIdW0W4IIh1v9BhHVllXw+z9oj0PALstT5h8/DuKoIiirFJ4DejU85GR1KKAS |
||||
DeO19G/lSpWj1rSgFv2N2gAOxq0X+BbQTua2jdcY6JpHR4H1JJ2wzfHsHPgDQcgY1rGlmjVF |
||||
aqU73WV4/hzXc/HshK/k4Zd8uD4zypv6rFsZ3UemK0aL2zXLVpV8SPWQ61nS03x675SmDlYr |
||||
A80ENfdqvsn00JQuBVIv4Tv0Ub7NfDraDGJCst8rObjBT/0vnBWTBCebb2EsnS2iStIFkWdz |
||||
/WXs4L4Yzre1iJwqRjiuqahZR5jHsjAUf2a0O29HVHE7zlFtCFmLPClml2lGQfQOpm5klGZF |
||||
rmvus+qZ9rt35UgWHPZezykkwtWrFOwspwuCWaPDto6tgbRJZ4ftitpdYYM3dKW9IGJXBwrt |
||||
BQrMsu+lp0vDF+yJAlUEEwEIAD8CGwMGCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAFiEErpbt |
||||
lp5HmwCE8+F/6I0zNPpfagoFAmEAEJwFCQycmLgACgkQ6I0zNPpfagpWoBAAhOcbMAUw6Zt0 |
||||
GYzT3sR5/c0iatezPzXEXJf9ebzR8M5uPElXcxcnMx1dvXZmGPXPJKCPa99WCu1NZYy8F+Wj |
||||
GTOY9tfIkvSxhys1p/giPAmvid6uQmD+bz7ivktnyzCkDWfMA+l8lsCSEqVlaq6y5T+a6SWB |
||||
6TzC2S0MPb/RrC/7DpwyrNYWumvyVJh09adm1Mw/UGgst/sZ8eMaRYEd3X0yyT1CBpX4zp2E |
||||
qQj9IEOTizvzv1x2jkHe5ZUeU3+nTBNlhSA+WFHUi0pfBdo2qog3Mv2EC1P2qMKoSdD5tPbA |
||||
zql1yKoHHnXOMsqdftGwbiv2sYXWvrYvmaCd3Ys/viOyt3HOy9uV2ZEtBd9Yqo9x/NZj8QMA |
||||
nY5k8jjrIXbUC89MqrJsQ6xxWQIg5ikMT7DvY0Ln89ev4oJyVvwIQAwCm4jUzFNm9bZLYDOP |
||||
5lGJCV7tF5NYVU7NxNM8vescKc40mVNK/pygS5mxhK9QYOUjZsIv8gddrl1TkqrFMuxFnTyN |
||||
WvzE29wFu/n4N1DkF+ZBqS70SlRvB+Hjz5LrDgEzF1Wf1eA/wq1dZbvMjjDVIc2VGlYp8Cp2 |
||||
8ob23c1seTtYXTNYgSR5go4EpH+xi+bIWv01bQQ9xGwBbT5sm4WUeWOcmX4QewzLZ3T/wK9+ |
||||
N4Ye/hmU9O34FwWJOY58EIe0OUV0aGVyZXVtIEZvdW5kYXRpb24gU2VjdXJpdHkgVGVhbSA8 |
||||
c2VjdXJpdHlAZXRoZXJldW0ub3JnPokCHAQRAQgABgUCWhQmOgAKCRB6DAN0NP5372LSEACT |
||||
wZk1TASWZj5QF7rmkIM1GEyBxLE+PundNcMgM9Ktj1315ED8SmiukNI4knVS1MY99OIgXhQl |
||||
D1foF2GKdTomrwwC4012zTNyUYCY60LnPZ6Z511HG+rZgZtZrbkz0IiUpwAlhGQND77lBqem |
||||
J3K+CFX2XpDA/ojui/kqrY4cwMT5P8xPJkwgpRgw/jgdcZyJTsXdHblV9IGU4H1Vd1SgcfAf |
||||
Db3YxDUlBtzlp0NkZqxen8irLIXUQvsfuIfRUbUSkWoK/n3U/gOCajAe8ZNF07iX4OWjH4Sw |
||||
NDA841WhFWcGE+d8+pfMVfPASU3UPKH72uw86b2VgR46Av6voyMFd1pj+yCA+YAhJuOpV4yL |
||||
QaGg2Z0kVOjuNWK/kBzp1F58DWGh4YBatbhE/UyQOqAAtR7lNf0M3QF9AdrHTxX8oZeqVW3V |
||||
Fmi2mk0NwCIUv8SSrZr1dTchp04OtyXe5gZBXSfzncCSRQIUDC8OgNWaOzAaUmK299v4bvye |
||||
uSCxOysxC7Q1hZtjzFPKdljS81mRlYeUL4fHlJU9R57bg8mriSXLmn7eKrSEDm/EG5T8nRx7 |
||||
TgX2MqJs8sWFxD2+bboVEu75yuFmZ//nmCBApAit9Hr2/sCshGIEpa9MQ6xJCYUxyqeJH+Cc |
||||
Aja0UfXhnK2uvPClpJLIl4RE3gm4OXeE1IkCPgQTAQIAKAIbAwYLCQgHAwIGFQgCCQoLBBYC |
||||
AwECHgECF4AFAloJYfoFCQWjYFgACgkQ6I0zNPpfagr4MQ//cfp3GSbSG8dkqgctW67Fy7cQ |
||||
diiTmx3cwxY+tlI3yrNmdjtrIQMzGdqtY6LNz7aN87F8mXNf+DyVHX9+wd1Y8U+E+hVCTzKC |
||||
sefUfxTz6unD9TTcGqaoelgIPMn4IiKz1RZE6eKpfDWe6q78W1Y6x1bE0qGNSjqT/QSxpezF |
||||
E/OAm/t8RRxVxDtqz8LfH2zLea5zaC+ADj8EqgY9vX9TQa4DyVV8MgOyECCCadJQCD5O5hIA |
||||
B2gVDWwrAUw+KBwskXZ7Iq4reJTKLEmt5z9zgtJ/fABwaCFt66ojwg0/RjbO9cNA3ZwHLGwU |
||||
C6hkb6bRzIoZoMfYxVS84opiqf/Teq+t/XkBYCxbSXTJDA5MKjcVuw3N6YKWbkGP/EfQThe7 |
||||
BfAKFwwIw5YmsWjHK8IQj6R6hBxzTz9rz8y1Lu8EAAFfA7OJKaboI2qbOlauH98OuOUmVtr1 |
||||
TczHO+pTcgWVN0ytq2/pX5KBf4vbmULNbg3HFRq+gHx8CW+jyXGkcqjbgU/5FwtDxeqRTdGJ |
||||
SyBGNBEU6pBNolyynyaKaaJjJ/biY27pvjymL5rlz95BH3Dn16Z4RRmqwlT6eq/wFYginujg |
||||
CCE1icqOSE+Vjl7V8tV8AcgANkXKdbBE+Q8wlKsGI/kS1w4XFAYcaNHFT8qNeS8TSFXFhvU8 |
||||
HylYxO79t56JAj4EEwECACgFAlgl3tgCGwMFCQHhM4AGCwkIBwMCBhUIAgkKCwQWAgMBAh4B |
||||
AheAAAoJEOiNMzT6X2oKmUMP/0hnaL6bVyepAq2LIdvIUbHfagt/Oo/KVfZs4bkM+xJOitJR |
||||
0kwZV9PTihXFdzhL/YNWc2+LtEBtKItqkJZKmWC0E6OPXGVuU6hfFPebuzVccYJfm0Q3Ej19 |
||||
VJI9Uomf59Bpak8HYyEED7WVQjoYn7XVPsonwus/9+LDX+c5vutbrUdbjga3KjHbewD93X4O |
||||
wVVoXyHEmU2Plyg8qvzFbNDylCWO7N2McO6SN6+7DitGZGr2+jO+P2R4RT1cnl2V3IRVcWZ0 |
||||
OTspPSnRGVr2fFiHN/+v8G/wHPLQcJZFvYPfUGNdcYbTmhWdiY0bEYXFiNrgzCCsyad7eKUR |
||||
WN9QmxqmyqLDjUEDJCAh19ES6Vg3tqGwXk+uNUCoF30ga0TxQt6UXZJDEQFAGeASQ/RqE/q1 |
||||
EAuLv8IGM8o7IqKO2pWfLuqsY6dTbKBwDzz9YOJt7EOGuPPQbHxaYStTushZmJnm7hi8lhVG |
||||
jT7qsEJdE95Il+I/mHWnXsCevaXjZugBiyV9yvOq4Hwwe2s1zKfrnQ4u0cadvGAh2eIqum7M |
||||
Y3o6nD47aJ3YmEPX/WnhI56bACa2GmWvUwjI4c0/er3esSPYnuHnM9L8Am4qQwMVSmyU80tC |
||||
MI7A9e13Mvv+RRkYFLJ7PVPdNpbW5jqX1doklFpKf6/XM+B+ngYneU+zgCUBiQJVBBMBCAA/ |
||||
AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgBYhBK6W7ZaeR5sAhPPhf+iNMzT6X2oKBQJh |
||||
ABCQBQkMnJi4AAoJEOiNMzT6X2oKAv0P+gJ3twBp5efNWyVLcIg4h4cOo9uD0NPvz8/fm2gX |
||||
FoOJL3MeigtPuSVfE9kuTaTuRbArzuFtdvH6G/kcRQvOlO4zyiIRHCk1gDHoIvvtn6RbRhVm |
||||
/Xo4uGIsFHst7n4A7BjicwEK5Op6Ih5Hoq19xz83YSBgBVk2fYEJIRyJiKFbyPjH0eSYe8v+ |
||||
Ra5/F85ugLx1P6mMVkW+WPzULns89riW7BGTnZmXFHZp8nO2pkUlcI7F3KRG7l4kmlC50ox6 |
||||
DiG/6AJCVulbAClky9C68TmJ/R1RazQxU/9IqVywsydq66tbJQbm5Z7GEti0C5jjbSRJL2oT |
||||
1xC7Rilr85PMREkPL3vegJdgj5PKlffZ/MocD/0EohiQ7wFpejFD4iTljeh0exRUwCRb6655 |
||||
9ib34JSQgU8Hl4JJu+mEgd9v0ZHD0/1mMD6fnAR84zca+O3cdASbnQmzTOKcGzLIrkE8TEnU |
||||
+2UZ8Ol7SAAqmBgzY1gKOilUho6dkyCAwNL+QDpvrITDPLEFPsjyB/M2KudZSVEn+Rletju1 |
||||
qkMW31qFMNlsbwzMZw+0USeGcs31Cs0B2/WQsro99CExlhS9auUFkmoVjJmYVTIYOM0zuPa4 |
||||
OyGspqPhRu5hEsmMDPDWD7Aad5k4GTqogQNnuKyRliZjXXrDZqFD5nfsJSL8Ky/sJGEMuQIN |
||||
BFgl3tgBEACbgq6HTN5gEBi0lkD/MafInmNi+59U5gRGYqk46WlfRjhHudXjDpgD0lolGb4h |
||||
YontkMaKRlCg2Rvgjvk3Zve0PKWjKw7gr8YBa9fMFY8BhAXI32OdyI9rFhxEZFfWAfwKVmT1 |
||||
9BdeAQRFvcfd+8w8f1XVc+zddULMJFBTr+xKDlIRWwTkdLPQeWbjo0eHl/g4tuLiLrTxVbnj |
||||
26bf+2+1DbM/w5VavzPrkviHqvKe/QP/gay4QDViWvFgLb90idfAHIdsPgflp0VDS5rVHFL6 |
||||
D73rSRdIRo3I8c8mYoNjSR4XDuvgOkAKW9LR3pvouFHHjp6Fr0GesRbrbb2EG66iPsR99MQ7 |
||||
FqIL9VMHPm2mtR+XvbnKkH2rYyEqaMbSdk29jGapkAWle4sIhSKk749A4tGkHl08KZ2N9o6G |
||||
rfUehP/V2eJLaph2DioFL1HxRryrKy80QQKLMJRekxigq8greW8xB4zuf9Mkuou+RHNmo8Pe |
||||
bHjFstLigiD6/zP2e+4tUmrT0/JTGOShoGMl8Rt0VRxdPImKun+4LOXbfOxArOSkY6i35+gs |
||||
gkkSy1gTJE0BY3S9auT6+YrglY/TWPQ9IJxWVOKlT+3WIp5wJu2bBKQ420VLqDYzkoWytel/ |
||||
bM1ACUtipMiIVeUs2uFiRjpzA1Wy0QHKPTdSuGlJPRrfcQARAQABiQIlBBgBAgAPAhsMBQJa |
||||
CWIIBQkFo2BYAAoJEOiNMzT6X2oKgSwQAKKs7BGF8TyZeIEO2EUK7R2bdQDCdSGZY06tqLFg |
||||
3IHMGxDMb/7FVoa2AEsFgv6xpoebxBB5zkhUk7lslgxvKiSLYjxfNjTBltfiFJ+eQnf+OTs8 |
||||
KeR51lLa66rvIH2qUzkNDCCTF45H4wIDpV05AXhBjKYkrDCrtey1rQyFp5fxI+0IQ1UKKXvz |
||||
ZK4GdxhxDbOUSd38MYy93nqcmclGSGK/gF8XiyuVjeifDCM6+T1NQTX0K9lneidcqtBDvlgg |
||||
JTLJtQPO33o5EHzXSiud+dKth1uUhZOFEaYRZoye1YE3yB0TNOOE8fXlvu8iuIAMBSDL9ep6 |
||||
sEIaXYwoD60I2gHdWD0lkP0DOjGQpi4ouXM3Edsd5MTi0MDRNTij431kn8T/D0LCgmoUmYYM |
||||
BgbwFhXr67axPZlKjrqR0z3F/Elv0ZPPcVg1tNznsALYQ9Ovl6b5M3cJ5GapbbvNWC7yEE1q |
||||
Scl9HiMxjt/H6aPastH63/7wcN0TslW+zRBy05VNJvpWGStQXcngsSUeJtI1Gd992YNjUJq4 |
||||
/Lih6Z1TlwcFVap+cTcDptoUvXYGg/9mRNNPZwErSfIJ0Ibnx9wPVuRN6NiCLOt2mtKp2F1p |
||||
M6AOQPpZ85vEh6I8i6OaO0w/Z0UHBwvpY6jDUliaROsWUQsqz78Z34CVj4cy6vPW2EF4iQIl |
||||
BBgBAgAPBQJYJd7YAhsMBQkB4TOAAAoJEOiNMzT6X2oKTjgP/1ojCVyGyvHMLUgnX0zwrR5Q |
||||
1M5RKFz6kHwKjODVLR3Isp8I935oTQt3DY7yFDI4t0GqbYRQMtxcNEb7maianhK2trCXfhPs |
||||
6/L04igjDf5iTcmzamXN6xnh5xkz06hZJJCMuu4MvKxC9MQHCVKAwjswl/9H9JqIBXAY3E2l |
||||
LpX5P+5jDZuPxS86p3+k4Rrdp9KTGXjiuEleM3zGlz5BLWydqovOck7C2aKh27ETFpDYY0z3 |
||||
yQ5AsPJyk1rAr0wrH6+ywmwWlzuQewavnrLnJ2M8iMFXpIhyHeEIU/f7o8f+dQk72rZ9CGzd |
||||
cqig2za/BS3zawZWgbv2vB2elNsIllYLdir45jxBOxx2yvJvEuu4glz78y4oJTCTAYAbMlle |
||||
5gVdPkVcGyvvVS9tinnSaiIzuvWrYHKWll1uYPm2Q1CDs06P5I7bUGAXpgQLUh/XQguy/0sX |
||||
GWqW3FS5JzP+XgcR/7UASvwBdHylubKbeqEpB7G1s+m+8C67qOrc7EQv3Jmy1YDOkhEyNig1 |
||||
rmjplLuir3tC1X+D7dHpn7NJe7nMwFx2b2MpMkLA9jPPAGPp/ekcu5sxCe+E0J/4UF++K+CR |
||||
XIxgtzU2UJfp8p9x+ygbx5qHinR0tVRdIzv3ZnGsXrfxnWfSOaB582cU3VRN9INzHHax8ETa |
||||
QVDnGO5uQa+FiQI8BBgBCAAmAhsMFiEErpbtlp5HmwCE8+F/6I0zNPpfagoFAmEAELYFCQyc |
||||
mN4ACgkQ6I0zNPpfagoqAQ/+MnDjBx8JWMd/XjeFoYKx/Oo0ntkInV+ME61JTBls4PdVk+TB |
||||
8PWZdPQHw9SnTvRmykFeznXIRzuxkowjrZYXdPXBxY2b1WyD5V3Ati1TM9vqpaR4osyPs2xy |
||||
I4dzDssh9YvUsIRL99O04/65lGiYeBNuACq+yK/7nD/ErzBkDYJHhMCdadbVWUACxvVIDvro |
||||
yQeVLKMsHqMCd8BTGD7VDs79NXskPnN77pAFnkzS4Z2b8SNzrlgTc5pUiuZHIXPIpEYmsYzh |
||||
ucTU6uI3dN1PbSFHK5tG2pHb4ZrPxY3L20Dgc2Tfu5/SDApZzwvvKTqjdO891MEJ++H+ssOz |
||||
i4O1UeWKs9owWttan9+PI47ozBSKOTxmMqLSQ0f56Np9FJsV0ilGxRKfjhzJ4KniOMUBA7mP |
||||
+m+TmXfVtthJred4sHlJMTJNpt+sCcT6wLMmyc3keIEAu33gsJj3LTpkEA2q+V+ZiP6Q8HRB |
||||
402ITklABSArrPSE/fQU9L8hZ5qmy0Z96z0iyILgVMLuRCCfQOMWhwl8yQWIIaf1yPI07xur |
||||
epy6lH7HmxjjOR7eo0DaSxQGQpThAtFGwkWkFh8yki8j3E42kkrxvEyyYZDXn2YcI3bpqhJx |
||||
PtwCMZUJ3kc/skOrs6bOI19iBNaEoNX5Dllm7UHjOgWNDQkcCuOCxucKano= |
||||
=arte |
||||
-----END PGP PUBLIC KEY BLOCK------ |
||||
``` |
@ -1,255 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
"io" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// The ABI holds information about a contract's context and available
|
||||
// invokable methods. It will allow you to type check function calls and
|
||||
// packs data accordingly.
|
||||
type ABI struct { |
||||
Constructor Method |
||||
Methods map[string]Method |
||||
Events map[string]Event |
||||
Errors map[string]Error |
||||
|
||||
// Additional "special" functions introduced in solidity v0.6.0.
|
||||
// It's separated from the original default fallback. Each contract
|
||||
// can only define one fallback and receive function.
|
||||
Fallback Method // Note it's also used to represent legacy fallback before v0.6.0
|
||||
Receive Method |
||||
} |
||||
|
||||
// JSON returns a parsed ABI interface and error if it failed.
|
||||
func JSON(reader io.Reader) (ABI, error) { |
||||
dec := json.NewDecoder(reader) |
||||
|
||||
var abi ABI |
||||
if err := dec.Decode(&abi); err != nil { |
||||
return ABI{}, err |
||||
} |
||||
return abi, nil |
||||
} |
||||
|
||||
// Pack the given method name to conform the ABI. Method call's data
|
||||
// will consist of method_id, args0, arg1, ... argN. Method id consists
|
||||
// of 4 bytes and arguments are all 32 bytes.
|
||||
// Method ids are created from the first 4 bytes of the hash of the
|
||||
// methods string signature. (signature = baz(uint32,string32))
|
||||
func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { |
||||
// Fetch the ABI of the requested method
|
||||
if name == "" { |
||||
// constructor
|
||||
arguments, err := abi.Constructor.Inputs.Pack(args...) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return arguments, nil |
||||
} |
||||
method, exist := abi.Methods[name] |
||||
if !exist { |
||||
return nil, fmt.Errorf("method '%s' not found", name) |
||||
} |
||||
arguments, err := method.Inputs.Pack(args...) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Pack up the method ID too if not a constructor and return
|
||||
return append(method.ID, arguments...), nil |
||||
} |
||||
|
||||
func (abi ABI) getArguments(name string, data []byte) (Arguments, error) { |
||||
// since there can't be naming collisions with contracts and events,
|
||||
// we need to decide whether we're calling a method or an event
|
||||
var args Arguments |
||||
if method, ok := abi.Methods[name]; ok { |
||||
if len(data)%32 != 0 { |
||||
return nil, fmt.Errorf("abi: improperly formatted output: %q - Bytes: %+v", data, data) |
||||
} |
||||
args = method.Outputs |
||||
} |
||||
if event, ok := abi.Events[name]; ok { |
||||
args = event.Inputs |
||||
} |
||||
if args == nil { |
||||
return nil, fmt.Errorf("abi: could not locate named method or event: %s", name) |
||||
} |
||||
return args, nil |
||||
} |
||||
|
||||
// Unpack unpacks the output according to the abi specification.
|
||||
func (abi ABI) Unpack(name string, data []byte) ([]interface{}, error) { |
||||
args, err := abi.getArguments(name, data) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return args.Unpack(data) |
||||
} |
||||
|
||||
// UnpackIntoInterface unpacks the output in v according to the abi specification.
|
||||
// It performs an additional copy. Please only use, if you want to unpack into a
|
||||
// structure that does not strictly conform to the abi structure (e.g. has additional arguments)
|
||||
func (abi ABI) UnpackIntoInterface(v interface{}, name string, data []byte) error { |
||||
args, err := abi.getArguments(name, data) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
unpacked, err := args.Unpack(data) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return args.Copy(v, unpacked) |
||||
} |
||||
|
||||
// UnpackIntoMap unpacks a log into the provided map[string]interface{}.
|
||||
func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) { |
||||
args, err := abi.getArguments(name, data) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return args.UnpackIntoMap(v, data) |
||||
} |
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler interface.
|
||||
func (abi *ABI) UnmarshalJSON(data []byte) error { |
||||
var fields []struct { |
||||
Type string |
||||
Name string |
||||
Inputs []Argument |
||||
Outputs []Argument |
||||
|
||||
// Status indicator which can be: "pure", "view",
|
||||
// "nonpayable" or "payable".
|
||||
StateMutability string |
||||
|
||||
// Deprecated Status indicators, but removed in v0.6.0.
|
||||
Constant bool // True if function is either pure or view
|
||||
Payable bool // True if function is payable
|
||||
|
||||
// Event relevant indicator represents the event is
|
||||
// declared as anonymous.
|
||||
Anonymous bool |
||||
} |
||||
if err := json.Unmarshal(data, &fields); err != nil { |
||||
return err |
||||
} |
||||
abi.Methods = make(map[string]Method) |
||||
abi.Events = make(map[string]Event) |
||||
abi.Errors = make(map[string]Error) |
||||
for _, field := range fields { |
||||
switch field.Type { |
||||
case "constructor": |
||||
abi.Constructor = NewMethod("", "", Constructor, field.StateMutability, field.Constant, field.Payable, field.Inputs, nil) |
||||
case "function": |
||||
name := ResolveNameConflict(field.Name, func(s string) bool { _, ok := abi.Methods[s]; return ok }) |
||||
abi.Methods[name] = NewMethod(name, field.Name, Function, field.StateMutability, field.Constant, field.Payable, field.Inputs, field.Outputs) |
||||
case "fallback": |
||||
// New introduced function type in v0.6.0, check more detail
|
||||
// here https://solidity.readthedocs.io/en/v0.6.0/contracts.html#fallback-function
|
||||
if abi.HasFallback() { |
||||
return errors.New("only single fallback is allowed") |
||||
} |
||||
abi.Fallback = NewMethod("", "", Fallback, field.StateMutability, field.Constant, field.Payable, nil, nil) |
||||
case "receive": |
||||
// New introduced function type in v0.6.0, check more detail
|
||||
// here https://solidity.readthedocs.io/en/v0.6.0/contracts.html#fallback-function
|
||||
if abi.HasReceive() { |
||||
return errors.New("only single receive is allowed") |
||||
} |
||||
if field.StateMutability != "payable" { |
||||
return errors.New("the statemutability of receive can only be payable") |
||||
} |
||||
abi.Receive = NewMethod("", "", Receive, field.StateMutability, field.Constant, field.Payable, nil, nil) |
||||
case "event": |
||||
name := ResolveNameConflict(field.Name, func(s string) bool { _, ok := abi.Events[s]; return ok }) |
||||
abi.Events[name] = NewEvent(name, field.Name, field.Anonymous, field.Inputs) |
||||
case "error": |
||||
// Errors cannot be overloaded or overridden but are inherited,
|
||||
// no need to resolve the name conflict here.
|
||||
abi.Errors[field.Name] = NewError(field.Name, field.Inputs) |
||||
default: |
||||
return fmt.Errorf("abi: could not recognize type %v of field %v", field.Type, field.Name) |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// MethodById looks up a method by the 4-byte id,
|
||||
// returns nil if none found.
|
||||
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) { |
||||
if len(sigdata) < 4 { |
||||
return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata)) |
||||
} |
||||
for _, method := range abi.Methods { |
||||
if bytes.Equal(method.ID, sigdata[:4]) { |
||||
return &method, nil |
||||
} |
||||
} |
||||
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4]) |
||||
} |
||||
|
||||
// EventByID looks an event up by its topic hash in the
|
||||
// ABI and returns nil if none found.
|
||||
func (abi *ABI) EventByID(topic common.Hash) (*Event, error) { |
||||
for _, event := range abi.Events { |
||||
if bytes.Equal(event.ID.Bytes(), topic.Bytes()) { |
||||
return &event, nil |
||||
} |
||||
} |
||||
return nil, fmt.Errorf("no event with id: %#x", topic.Hex()) |
||||
} |
||||
|
||||
// HasFallback returns an indicator whether a fallback function is included.
|
||||
func (abi *ABI) HasFallback() bool { |
||||
return abi.Fallback.Type == Fallback |
||||
} |
||||
|
||||
// HasReceive returns an indicator whether a receive function is included.
|
||||
func (abi *ABI) HasReceive() bool { |
||||
return abi.Receive.Type == Receive |
||||
} |
||||
|
||||
// revertSelector is a special function selector for revert reason unpacking.
|
||||
var revertSelector = crypto.Keccak256([]byte("Error(string)"))[:4] |
||||
|
||||
// UnpackRevert resolves the abi-encoded revert reason. According to the solidity
|
||||
// spec https://solidity.readthedocs.io/en/latest/control-structures.html#revert,
|
||||
// the provided revert reason is abi-encoded as if it were a call to a function
|
||||
// `Error(string)`. So it's a special tool for it.
|
||||
func UnpackRevert(data []byte) (string, error) { |
||||
if len(data) < 4 { |
||||
return "", errors.New("invalid data for unpacking") |
||||
} |
||||
if !bytes.Equal(data[:4], revertSelector) { |
||||
return "", errors.New("invalid data for unpacking") |
||||
} |
||||
typ, _ := NewType("string", "", nil) |
||||
unpacked, err := (Arguments{{Type: typ}}).Unpack(data[4:]) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
return unpacked[0].(string), nil |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,273 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
"strings" |
||||
) |
||||
|
||||
// Argument holds the name of the argument and the corresponding type.
|
||||
// Types are used when packing and testing arguments.
|
||||
type Argument struct { |
||||
Name string |
||||
Type Type |
||||
Indexed bool // indexed is only used by events
|
||||
} |
||||
|
||||
type Arguments []Argument |
||||
|
||||
type ArgumentMarshaling struct { |
||||
Name string |
||||
Type string |
||||
InternalType string |
||||
Components []ArgumentMarshaling |
||||
Indexed bool |
||||
} |
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler interface.
|
||||
func (argument *Argument) UnmarshalJSON(data []byte) error { |
||||
var arg ArgumentMarshaling |
||||
err := json.Unmarshal(data, &arg) |
||||
if err != nil { |
||||
return fmt.Errorf("argument json err: %v", err) |
||||
} |
||||
|
||||
argument.Type, err = NewType(arg.Type, arg.InternalType, arg.Components) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
argument.Name = arg.Name |
||||
argument.Indexed = arg.Indexed |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// NonIndexed returns the arguments with indexed arguments filtered out.
|
||||
func (arguments Arguments) NonIndexed() Arguments { |
||||
var ret []Argument |
||||
for _, arg := range arguments { |
||||
if !arg.Indexed { |
||||
ret = append(ret, arg) |
||||
} |
||||
} |
||||
return ret |
||||
} |
||||
|
||||
// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[].
|
||||
func (arguments Arguments) isTuple() bool { |
||||
return len(arguments) > 1 |
||||
} |
||||
|
||||
// Unpack performs the operation hexdata -> Go format.
|
||||
func (arguments Arguments) Unpack(data []byte) ([]interface{}, error) { |
||||
if len(data) == 0 { |
||||
if len(arguments.NonIndexed()) != 0 { |
||||
return nil, errors.New("abi: attempting to unmarshall an empty string while arguments are expected") |
||||
} |
||||
return make([]interface{}, 0), nil |
||||
} |
||||
return arguments.UnpackValues(data) |
||||
} |
||||
|
||||
// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value.
|
||||
func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error { |
||||
// Make sure map is not nil
|
||||
if v == nil { |
||||
return errors.New("abi: cannot unpack into a nil map") |
||||
} |
||||
if len(data) == 0 { |
||||
if len(arguments.NonIndexed()) != 0 { |
||||
return errors.New("abi: attempting to unmarshall an empty string while arguments are expected") |
||||
} |
||||
return nil // Nothing to unmarshal, return
|
||||
} |
||||
marshalledValues, err := arguments.UnpackValues(data) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
for i, arg := range arguments.NonIndexed() { |
||||
v[arg.Name] = marshalledValues[i] |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// Copy performs the operation go format -> provided struct.
|
||||
func (arguments Arguments) Copy(v interface{}, values []interface{}) error { |
||||
// make sure the passed value is arguments pointer
|
||||
if reflect.Ptr != reflect.ValueOf(v).Kind() { |
||||
return fmt.Errorf("abi: Unpack(non-pointer %T)", v) |
||||
} |
||||
if len(values) == 0 { |
||||
if len(arguments.NonIndexed()) != 0 { |
||||
return errors.New("abi: attempting to copy no values while arguments are expected") |
||||
} |
||||
return nil // Nothing to copy, return
|
||||
} |
||||
if arguments.isTuple() { |
||||
return arguments.copyTuple(v, values) |
||||
} |
||||
return arguments.copyAtomic(v, values[0]) |
||||
} |
||||
|
||||
// unpackAtomic unpacks ( hexdata -> go ) a single value
|
||||
func (arguments Arguments) copyAtomic(v interface{}, marshalledValues interface{}) error { |
||||
dst := reflect.ValueOf(v).Elem() |
||||
src := reflect.ValueOf(marshalledValues) |
||||
|
||||
if dst.Kind() == reflect.Struct { |
||||
return set(dst.Field(0), src) |
||||
} |
||||
return set(dst, src) |
||||
} |
||||
|
||||
// copyTuple copies a batch of values from marshalledValues to v.
|
||||
func (arguments Arguments) copyTuple(v interface{}, marshalledValues []interface{}) error { |
||||
value := reflect.ValueOf(v).Elem() |
||||
nonIndexedArgs := arguments.NonIndexed() |
||||
|
||||
switch value.Kind() { |
||||
case reflect.Struct: |
||||
argNames := make([]string, len(nonIndexedArgs)) |
||||
for i, arg := range nonIndexedArgs { |
||||
argNames[i] = arg.Name |
||||
} |
||||
var err error |
||||
abi2struct, err := mapArgNamesToStructFields(argNames, value) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
for i, arg := range nonIndexedArgs { |
||||
field := value.FieldByName(abi2struct[arg.Name]) |
||||
if !field.IsValid() { |
||||
return fmt.Errorf("abi: field %s can't be found in the given value", arg.Name) |
||||
} |
||||
if err := set(field, reflect.ValueOf(marshalledValues[i])); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
case reflect.Slice, reflect.Array: |
||||
if value.Len() < len(marshalledValues) { |
||||
return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", len(arguments), value.Len()) |
||||
} |
||||
for i := range nonIndexedArgs { |
||||
if err := set(value.Index(i), reflect.ValueOf(marshalledValues[i])); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
default: |
||||
return fmt.Errorf("abi:[2] cannot unmarshal tuple in to %v", value.Type()) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
|
||||
// without supplying a struct to unpack into. Instead, this method returns a list containing the
|
||||
// values. An atomic argument will be a list with one element.
|
||||
func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error) { |
||||
nonIndexedArgs := arguments.NonIndexed() |
||||
retval := make([]interface{}, 0, len(nonIndexedArgs)) |
||||
virtualArgs := 0 |
||||
for index, arg := range nonIndexedArgs { |
||||
marshalledValue, err := toGoType((index+virtualArgs)*32, arg.Type, data) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if arg.Type.T == ArrayTy && !isDynamicType(arg.Type) { |
||||
// If we have a static array, like [3]uint256, these are coded as
|
||||
// just like uint256,uint256,uint256.
|
||||
// This means that we need to add two 'virtual' arguments when
|
||||
// we count the index from now on.
|
||||
//
|
||||
// Array values nested multiple levels deep are also encoded inline:
|
||||
// [2][3]uint256: uint256,uint256,uint256,uint256,uint256,uint256
|
||||
//
|
||||
// Calculate the full array size to get the correct offset for the next argument.
|
||||
// Decrement it by 1, as the normal index increment is still applied.
|
||||
virtualArgs += getTypeSize(arg.Type)/32 - 1 |
||||
} else if arg.Type.T == TupleTy && !isDynamicType(arg.Type) { |
||||
// If we have a static tuple, like (uint256, bool, uint256), these are
|
||||
// coded as just like uint256,bool,uint256
|
||||
virtualArgs += getTypeSize(arg.Type)/32 - 1 |
||||
} |
||||
retval = append(retval, marshalledValue) |
||||
} |
||||
return retval, nil |
||||
} |
||||
|
||||
// PackValues performs the operation Go format -> Hexdata.
|
||||
// It is the semantic opposite of UnpackValues.
|
||||
func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) { |
||||
return arguments.Pack(args...) |
||||
} |
||||
|
||||
// Pack performs the operation Go format -> Hexdata.
|
||||
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) { |
||||
// Make sure arguments match up and pack them
|
||||
abiArgs := arguments |
||||
if len(args) != len(abiArgs) { |
||||
return nil, fmt.Errorf("argument count mismatch: got %d for %d", len(args), len(abiArgs)) |
||||
} |
||||
// variable input is the output appended at the end of packed
|
||||
// output. This is used for strings and bytes types input.
|
||||
var variableInput []byte |
||||
|
||||
// input offset is the bytes offset for packed output
|
||||
inputOffset := 0 |
||||
for _, abiArg := range abiArgs { |
||||
inputOffset += getTypeSize(abiArg.Type) |
||||
} |
||||
var ret []byte |
||||
for i, a := range args { |
||||
input := abiArgs[i] |
||||
// pack the input
|
||||
packed, err := input.Type.pack(reflect.ValueOf(a)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// check for dynamic types
|
||||
if isDynamicType(input.Type) { |
||||
// set the offset
|
||||
ret = append(ret, packNum(reflect.ValueOf(inputOffset))...) |
||||
// calculate next offset
|
||||
inputOffset += len(packed) |
||||
// append to variable input
|
||||
variableInput = append(variableInput, packed...) |
||||
} else { |
||||
// append the packed value to the input
|
||||
ret = append(ret, packed...) |
||||
} |
||||
} |
||||
// append the variable input at the end of the packed input
|
||||
ret = append(ret, variableInput...) |
||||
|
||||
return ret, nil |
||||
} |
||||
|
||||
// ToCamelCase converts an under-score string to a camel-case string
|
||||
func ToCamelCase(input string) string { |
||||
parts := strings.Split(input, "_") |
||||
for i, s := range parts { |
||||
if len(s) > 0 { |
||||
parts[i] = strings.ToUpper(s[:1]) + s[1:] |
||||
} |
||||
} |
||||
return strings.Join(parts, "") |
||||
} |
@ -1,179 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/ecdsa" |
||||
"errors" |
||||
"io" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/accounts/external" |
||||
"github.com/ethereum/go-ethereum/accounts/keystore" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
) |
||||
|
||||
// ErrNoChainID is returned whenever the user failed to specify a chain id.
|
||||
var ErrNoChainID = errors.New("no chain id specified") |
||||
|
||||
// ErrNotAuthorized is returned when an account is not properly unlocked.
|
||||
var ErrNotAuthorized = errors.New("not authorized to sign this account") |
||||
|
||||
// NewTransactor is a utility method to easily create a transaction signer from
|
||||
// an encrypted json key stream and the associated passphrase.
|
||||
//
|
||||
// Deprecated: Use NewTransactorWithChainID instead.
|
||||
func NewTransactor(keyin io.Reader, passphrase string) (*TransactOpts, error) { |
||||
log.Warn("WARNING: NewTransactor has been deprecated in favour of NewTransactorWithChainID") |
||||
json, err := io.ReadAll(keyin) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
key, err := keystore.DecryptKey(json, passphrase) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return NewKeyedTransactor(key.PrivateKey), nil |
||||
} |
||||
|
||||
// NewKeyStoreTransactor is a utility method to easily create a transaction signer from
|
||||
// an decrypted key from a keystore.
|
||||
//
|
||||
// Deprecated: Use NewKeyStoreTransactorWithChainID instead.
|
||||
func NewKeyStoreTransactor(keystore *keystore.KeyStore, account accounts.Account) (*TransactOpts, error) { |
||||
log.Warn("WARNING: NewKeyStoreTransactor has been deprecated in favour of NewTransactorWithChainID") |
||||
signer := types.HomesteadSigner{} |
||||
return &TransactOpts{ |
||||
From: account.Address, |
||||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { |
||||
if address != account.Address { |
||||
return nil, ErrNotAuthorized |
||||
} |
||||
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return tx.WithSignature(signer, signature) |
||||
}, |
||||
Context: context.Background(), |
||||
}, nil |
||||
} |
||||
|
||||
// NewKeyedTransactor is a utility method to easily create a transaction signer
|
||||
// from a single private key.
|
||||
//
|
||||
// Deprecated: Use NewKeyedTransactorWithChainID instead.
|
||||
func NewKeyedTransactor(key *ecdsa.PrivateKey) *TransactOpts { |
||||
log.Warn("WARNING: NewKeyedTransactor has been deprecated in favour of NewKeyedTransactorWithChainID") |
||||
keyAddr := crypto.PubkeyToAddress(key.PublicKey) |
||||
signer := types.HomesteadSigner{} |
||||
return &TransactOpts{ |
||||
From: keyAddr, |
||||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { |
||||
if address != keyAddr { |
||||
return nil, ErrNotAuthorized |
||||
} |
||||
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return tx.WithSignature(signer, signature) |
||||
}, |
||||
Context: context.Background(), |
||||
} |
||||
} |
||||
|
||||
// NewTransactorWithChainID is a utility method to easily create a transaction signer from
|
||||
// an encrypted json key stream and the associated passphrase.
|
||||
func NewTransactorWithChainID(keyin io.Reader, passphrase string, chainID *big.Int) (*TransactOpts, error) { |
||||
json, err := io.ReadAll(keyin) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
key, err := keystore.DecryptKey(json, passphrase) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return NewKeyedTransactorWithChainID(key.PrivateKey, chainID) |
||||
} |
||||
|
||||
// NewKeyStoreTransactorWithChainID is a utility method to easily create a transaction signer from
|
||||
// an decrypted key from a keystore.
|
||||
func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accounts.Account, chainID *big.Int) (*TransactOpts, error) { |
||||
if chainID == nil { |
||||
return nil, ErrNoChainID |
||||
} |
||||
signer := types.LatestSignerForChainID(chainID) |
||||
return &TransactOpts{ |
||||
From: account.Address, |
||||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { |
||||
if address != account.Address { |
||||
return nil, ErrNotAuthorized |
||||
} |
||||
signature, err := keystore.SignHash(account, signer.Hash(tx).Bytes()) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return tx.WithSignature(signer, signature) |
||||
}, |
||||
Context: context.Background(), |
||||
}, nil |
||||
} |
||||
|
||||
// NewKeyedTransactorWithChainID is a utility method to easily create a transaction signer
|
||||
// from a single private key.
|
||||
func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*TransactOpts, error) { |
||||
keyAddr := crypto.PubkeyToAddress(key.PublicKey) |
||||
if chainID == nil { |
||||
return nil, ErrNoChainID |
||||
} |
||||
signer := types.LatestSignerForChainID(chainID) |
||||
return &TransactOpts{ |
||||
From: keyAddr, |
||||
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { |
||||
if address != keyAddr { |
||||
return nil, ErrNotAuthorized |
||||
} |
||||
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return tx.WithSignature(signer, signature) |
||||
}, |
||||
Context: context.Background(), |
||||
}, nil |
||||
} |
||||
|
||||
// NewClefTransactor is a utility method to easily create a transaction signer
|
||||
// with a clef backend.
|
||||
func NewClefTransactor(clef *external.ExternalSigner, account accounts.Account) *TransactOpts { |
||||
return &TransactOpts{ |
||||
From: account.Address, |
||||
Signer: func(address common.Address, transaction *types.Transaction) (*types.Transaction, error) { |
||||
if address != account.Address { |
||||
return nil, ErrNotAuthorized |
||||
} |
||||
return clef.SignTx(account, transaction, nil) // Clef enforces its own chain id
|
||||
}, |
||||
Context: context.Background(), |
||||
} |
||||
} |
@ -1,126 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
) |
||||
|
||||
var ( |
||||
// ErrNoCode is returned by call and transact operations for which the requested
|
||||
// recipient contract to operate on does not exist in the state db or does not
|
||||
// have any code associated with it (i.e. suicided).
|
||||
ErrNoCode = errors.New("no contract code at given address") |
||||
|
||||
// ErrNoPendingState is raised when attempting to perform a pending state action
|
||||
// on a backend that doesn't implement PendingContractCaller.
|
||||
ErrNoPendingState = errors.New("backend does not support pending state") |
||||
|
||||
// ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
|
||||
// an empty contract behind.
|
||||
ErrNoCodeAfterDeploy = errors.New("no contract code after deployment") |
||||
) |
||||
|
||||
// ContractCaller defines the methods needed to allow operating with a contract on a read
|
||||
// only basis.
|
||||
type ContractCaller interface { |
||||
// CodeAt returns the code of the given account. This is needed to differentiate
|
||||
// between contract internal errors and the local chain being out of sync.
|
||||
CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) |
||||
|
||||
// CallContract executes an Ethereum contract call with the specified data as the
|
||||
// input.
|
||||
CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) |
||||
} |
||||
|
||||
// PendingContractCaller defines methods to perform contract calls on the pending state.
|
||||
// Call will try to discover this interface when access to the pending state is requested.
|
||||
// If the backend does not support the pending state, Call returns ErrNoPendingState.
|
||||
type PendingContractCaller interface { |
||||
// PendingCodeAt returns the code of the given account in the pending state.
|
||||
PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) |
||||
|
||||
// PendingCallContract executes an Ethereum contract call against the pending state.
|
||||
PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) |
||||
} |
||||
|
||||
// ContractTransactor defines the methods needed to allow operating with a contract
|
||||
// on a write only basis. Besides the transacting method, the remainder are helpers
|
||||
// used when the user does not provide some needed values, but rather leaves it up
|
||||
// to the transactor to decide.
|
||||
type ContractTransactor interface { |
||||
// HeaderByNumber returns a block header from the current canonical chain. If
|
||||
// number is nil, the latest known header is returned.
|
||||
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) |
||||
|
||||
// PendingCodeAt returns the code of the given account in the pending state.
|
||||
PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) |
||||
|
||||
// PendingNonceAt retrieves the current pending nonce associated with an account.
|
||||
PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) |
||||
|
||||
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
|
||||
// execution of a transaction.
|
||||
SuggestGasPrice(ctx context.Context) (*big.Int, error) |
||||
|
||||
// SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
|
||||
// a timely execution of a transaction.
|
||||
SuggestGasTipCap(ctx context.Context) (*big.Int, error) |
||||
|
||||
// EstimateGas tries to estimate the gas needed to execute a specific
|
||||
// transaction based on the current pending state of the backend blockchain.
|
||||
// There is no guarantee that this is the true gas limit requirement as other
|
||||
// transactions may be added or removed by miners, but it should provide a basis
|
||||
// for setting a reasonable default.
|
||||
EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) |
||||
|
||||
// SendTransaction injects the transaction into the pending pool for execution.
|
||||
SendTransaction(ctx context.Context, tx *types.Transaction) error |
||||
} |
||||
|
||||
// ContractFilterer defines the methods needed to access log events using one-off
|
||||
// queries or continuous event subscriptions.
|
||||
type ContractFilterer interface { |
||||
// FilterLogs executes a log filter operation, blocking during execution and
|
||||
// returning all the results in one batch.
|
||||
//
|
||||
// TODO(karalabe): Deprecate when the subscription one can return past data too.
|
||||
FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) |
||||
|
||||
// SubscribeFilterLogs creates a background log filtering operation, returning
|
||||
// a subscription immediately, which can be used to stream the found events.
|
||||
SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) |
||||
} |
||||
|
||||
// DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
|
||||
type DeployBackend interface { |
||||
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) |
||||
CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) |
||||
} |
||||
|
||||
// ContractBackend defines the methods needed to work with contracts on a read-write basis.
|
||||
type ContractBackend interface { |
||||
ContractCaller |
||||
ContractTransactor |
||||
ContractFilterer |
||||
} |
@ -1,931 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package backends |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
"sync" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/accounts/abi" |
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/common/math" |
||||
"github.com/ethereum/go-ethereum/consensus/ethash" |
||||
"github.com/ethereum/go-ethereum/core" |
||||
"github.com/ethereum/go-ethereum/core/bloombits" |
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/core/state" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/core/vm" |
||||
"github.com/ethereum/go-ethereum/eth/filters" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
) |
||||
|
||||
// This nil assignment ensures at compile time that SimulatedBackend implements bind.ContractBackend.
|
||||
var _ bind.ContractBackend = (*SimulatedBackend)(nil) |
||||
|
||||
var ( |
||||
errBlockNumberUnsupported = errors.New("simulatedBackend cannot access blocks other than the latest block") |
||||
errBlockDoesNotExist = errors.New("block does not exist in blockchain") |
||||
errTransactionDoesNotExist = errors.New("transaction does not exist") |
||||
) |
||||
|
||||
// SimulatedBackend implements bind.ContractBackend, simulating a blockchain in
|
||||
// the background. Its main purpose is to allow for easy testing of contract bindings.
|
||||
// Simulated backend implements the following interfaces:
|
||||
// ChainReader, ChainStateReader, ContractBackend, ContractCaller, ContractFilterer, ContractTransactor,
|
||||
// DeployBackend, GasEstimator, GasPricer, LogFilterer, PendingContractCaller, TransactionReader, and TransactionSender
|
||||
type SimulatedBackend struct { |
||||
database ethdb.Database // In memory database to store our testing data
|
||||
blockchain *core.BlockChain // Ethereum blockchain to handle the consensus
|
||||
|
||||
mu sync.Mutex |
||||
pendingBlock *types.Block // Currently pending block that will be imported on request
|
||||
pendingState *state.StateDB // Currently pending state that will be the active on request
|
||||
pendingReceipts types.Receipts // Currently receipts for the pending block
|
||||
|
||||
events *filters.EventSystem // for filtering log events live
|
||||
filterSystem *filters.FilterSystem // for filtering database logs
|
||||
|
||||
config *params.ChainConfig |
||||
} |
||||
|
||||
// NewSimulatedBackendWithDatabase creates a new binding backend based on the given database
|
||||
// and uses a simulated blockchain for testing purposes.
|
||||
// A simulated backend always uses chainID 1337.
|
||||
func NewSimulatedBackendWithDatabase(database ethdb.Database, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { |
||||
genesis := core.Genesis{ |
||||
Config: params.AllEthashProtocolChanges, |
||||
GasLimit: gasLimit, |
||||
Alloc: alloc, |
||||
} |
||||
blockchain, _ := core.NewBlockChain(database, nil, &genesis, nil, ethash.NewFaker(), vm.Config{}, nil, nil) |
||||
|
||||
backend := &SimulatedBackend{ |
||||
database: database, |
||||
blockchain: blockchain, |
||||
config: genesis.Config, |
||||
} |
||||
|
||||
filterBackend := &filterBackend{database, blockchain, backend} |
||||
backend.filterSystem = filters.NewFilterSystem(filterBackend, filters.Config{}) |
||||
backend.events = filters.NewEventSystem(backend.filterSystem, false) |
||||
|
||||
backend.rollback(blockchain.CurrentBlock()) |
||||
return backend |
||||
} |
||||
|
||||
// NewSimulatedBackend creates a new binding backend using a simulated blockchain
|
||||
// for testing purposes.
|
||||
// A simulated backend always uses chainID 1337.
|
||||
func NewSimulatedBackend(alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { |
||||
return NewSimulatedBackendWithDatabase(rawdb.NewMemoryDatabase(), alloc, gasLimit) |
||||
} |
||||
|
||||
// Close terminates the underlying blockchain's update loop.
|
||||
func (b *SimulatedBackend) Close() error { |
||||
b.blockchain.Stop() |
||||
return nil |
||||
} |
||||
|
||||
// Commit imports all the pending transactions as a single block and starts a
|
||||
// fresh new state.
|
||||
func (b *SimulatedBackend) Commit() common.Hash { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if _, err := b.blockchain.InsertChain([]*types.Block{b.pendingBlock}); err != nil { |
||||
panic(err) // This cannot happen unless the simulator is wrong, fail in that case
|
||||
} |
||||
blockHash := b.pendingBlock.Hash() |
||||
|
||||
// Using the last inserted block here makes it possible to build on a side
|
||||
// chain after a fork.
|
||||
b.rollback(b.pendingBlock) |
||||
|
||||
return blockHash |
||||
} |
||||
|
||||
// Rollback aborts all pending transactions, reverting to the last committed state.
|
||||
func (b *SimulatedBackend) Rollback() { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
b.rollback(b.blockchain.CurrentBlock()) |
||||
} |
||||
|
||||
func (b *SimulatedBackend) rollback(parent *types.Block) { |
||||
blocks, _ := core.GenerateChain(b.config, parent, ethash.NewFaker(), b.database, 1, func(int, *core.BlockGen) {}) |
||||
|
||||
b.pendingBlock = blocks[0] |
||||
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.blockchain.StateCache(), nil) |
||||
} |
||||
|
||||
// Fork creates a side-chain that can be used to simulate reorgs.
|
||||
//
|
||||
// This function should be called with the ancestor block where the new side
|
||||
// chain should be started. Transactions (old and new) can then be applied on
|
||||
// top and Commit-ed.
|
||||
//
|
||||
// Note, the side-chain will only become canonical (and trigger the events) when
|
||||
// it becomes longer. Until then CallContract will still operate on the current
|
||||
// canonical chain.
|
||||
//
|
||||
// There is a % chance that the side chain becomes canonical at the same length
|
||||
// to simulate live network behavior.
|
||||
func (b *SimulatedBackend) Fork(ctx context.Context, parent common.Hash) error { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if len(b.pendingBlock.Transactions()) != 0 { |
||||
return errors.New("pending block dirty") |
||||
} |
||||
block, err := b.blockByHash(ctx, parent) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
b.rollback(block) |
||||
return nil |
||||
} |
||||
|
||||
// stateByBlockNumber retrieves a state by a given blocknumber.
|
||||
func (b *SimulatedBackend) stateByBlockNumber(ctx context.Context, blockNumber *big.Int) (*state.StateDB, error) { |
||||
if blockNumber == nil || blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) == 0 { |
||||
return b.blockchain.State() |
||||
} |
||||
block, err := b.blockByNumber(ctx, blockNumber) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return b.blockchain.StateAt(block.Root()) |
||||
} |
||||
|
||||
// CodeAt returns the code associated with a certain account in the blockchain.
|
||||
func (b *SimulatedBackend) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
stateDB, err := b.stateByBlockNumber(ctx, blockNumber) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return stateDB.GetCode(contract), nil |
||||
} |
||||
|
||||
// BalanceAt returns the wei balance of a certain account in the blockchain.
|
||||
func (b *SimulatedBackend) BalanceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (*big.Int, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
stateDB, err := b.stateByBlockNumber(ctx, blockNumber) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return stateDB.GetBalance(contract), nil |
||||
} |
||||
|
||||
// NonceAt returns the nonce of a certain account in the blockchain.
|
||||
func (b *SimulatedBackend) NonceAt(ctx context.Context, contract common.Address, blockNumber *big.Int) (uint64, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
stateDB, err := b.stateByBlockNumber(ctx, blockNumber) |
||||
if err != nil { |
||||
return 0, err |
||||
} |
||||
|
||||
return stateDB.GetNonce(contract), nil |
||||
} |
||||
|
||||
// StorageAt returns the value of key in the storage of an account in the blockchain.
|
||||
func (b *SimulatedBackend) StorageAt(ctx context.Context, contract common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
stateDB, err := b.stateByBlockNumber(ctx, blockNumber) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
val := stateDB.GetState(contract, key) |
||||
return val[:], nil |
||||
} |
||||
|
||||
// TransactionReceipt returns the receipt of a transaction.
|
||||
func (b *SimulatedBackend) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
receipt, _, _, _ := rawdb.ReadReceipt(b.database, txHash, b.config) |
||||
if receipt == nil { |
||||
return nil, ethereum.NotFound |
||||
} |
||||
return receipt, nil |
||||
} |
||||
|
||||
// TransactionByHash checks the pool of pending transactions in addition to the
|
||||
// blockchain. The isPending return value indicates whether the transaction has been
|
||||
// mined yet. Note that the transaction may not be part of the canonical chain even if
|
||||
// it's not pending.
|
||||
func (b *SimulatedBackend) TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
tx := b.pendingBlock.Transaction(txHash) |
||||
if tx != nil { |
||||
return tx, true, nil |
||||
} |
||||
tx, _, _, _ = rawdb.ReadTransaction(b.database, txHash) |
||||
if tx != nil { |
||||
return tx, false, nil |
||||
} |
||||
return nil, false, ethereum.NotFound |
||||
} |
||||
|
||||
// BlockByHash retrieves a block based on the block hash.
|
||||
func (b *SimulatedBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
return b.blockByHash(ctx, hash) |
||||
} |
||||
|
||||
// blockByHash retrieves a block based on the block hash without Locking.
|
||||
func (b *SimulatedBackend) blockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { |
||||
if hash == b.pendingBlock.Hash() { |
||||
return b.pendingBlock, nil |
||||
} |
||||
|
||||
block := b.blockchain.GetBlockByHash(hash) |
||||
if block != nil { |
||||
return block, nil |
||||
} |
||||
|
||||
return nil, errBlockDoesNotExist |
||||
} |
||||
|
||||
// BlockByNumber retrieves a block from the database by number, caching it
|
||||
// (associated with its hash) if found.
|
||||
func (b *SimulatedBackend) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
return b.blockByNumber(ctx, number) |
||||
} |
||||
|
||||
// blockByNumber retrieves a block from the database by number, caching it
|
||||
// (associated with its hash) if found without Lock.
|
||||
func (b *SimulatedBackend) blockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { |
||||
if number == nil || number.Cmp(b.pendingBlock.Number()) == 0 { |
||||
return b.blockchain.CurrentBlock(), nil |
||||
} |
||||
|
||||
block := b.blockchain.GetBlockByNumber(uint64(number.Int64())) |
||||
if block == nil { |
||||
return nil, errBlockDoesNotExist |
||||
} |
||||
|
||||
return block, nil |
||||
} |
||||
|
||||
// HeaderByHash returns a block header from the current canonical chain.
|
||||
func (b *SimulatedBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if hash == b.pendingBlock.Hash() { |
||||
return b.pendingBlock.Header(), nil |
||||
} |
||||
|
||||
header := b.blockchain.GetHeaderByHash(hash) |
||||
if header == nil { |
||||
return nil, errBlockDoesNotExist |
||||
} |
||||
|
||||
return header, nil |
||||
} |
||||
|
||||
// HeaderByNumber returns a block header from the current canonical chain. If number is
|
||||
// nil, the latest known header is returned.
|
||||
func (b *SimulatedBackend) HeaderByNumber(ctx context.Context, block *big.Int) (*types.Header, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if block == nil || block.Cmp(b.pendingBlock.Number()) == 0 { |
||||
return b.blockchain.CurrentHeader(), nil |
||||
} |
||||
|
||||
return b.blockchain.GetHeaderByNumber(uint64(block.Int64())), nil |
||||
} |
||||
|
||||
// TransactionCount returns the number of transactions in a given block.
|
||||
func (b *SimulatedBackend) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if blockHash == b.pendingBlock.Hash() { |
||||
return uint(b.pendingBlock.Transactions().Len()), nil |
||||
} |
||||
|
||||
block := b.blockchain.GetBlockByHash(blockHash) |
||||
if block == nil { |
||||
return uint(0), errBlockDoesNotExist |
||||
} |
||||
|
||||
return uint(block.Transactions().Len()), nil |
||||
} |
||||
|
||||
// TransactionInBlock returns the transaction for a specific block at a specific index.
|
||||
func (b *SimulatedBackend) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if blockHash == b.pendingBlock.Hash() { |
||||
transactions := b.pendingBlock.Transactions() |
||||
if uint(len(transactions)) < index+1 { |
||||
return nil, errTransactionDoesNotExist |
||||
} |
||||
|
||||
return transactions[index], nil |
||||
} |
||||
|
||||
block := b.blockchain.GetBlockByHash(blockHash) |
||||
if block == nil { |
||||
return nil, errBlockDoesNotExist |
||||
} |
||||
|
||||
transactions := block.Transactions() |
||||
if uint(len(transactions)) < index+1 { |
||||
return nil, errTransactionDoesNotExist |
||||
} |
||||
|
||||
return transactions[index], nil |
||||
} |
||||
|
||||
// PendingCodeAt returns the code associated with an account in the pending state.
|
||||
func (b *SimulatedBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
return b.pendingState.GetCode(contract), nil |
||||
} |
||||
|
||||
func newRevertError(result *core.ExecutionResult) *revertError { |
||||
reason, errUnpack := abi.UnpackRevert(result.Revert()) |
||||
err := errors.New("execution reverted") |
||||
if errUnpack == nil { |
||||
err = fmt.Errorf("execution reverted: %v", reason) |
||||
} |
||||
return &revertError{ |
||||
error: err, |
||||
reason: hexutil.Encode(result.Revert()), |
||||
} |
||||
} |
||||
|
||||
// revertError is an API error that encompasses an EVM revert with JSON error
|
||||
// code and a binary data blob.
|
||||
type revertError struct { |
||||
error |
||||
reason string // revert reason hex encoded
|
||||
} |
||||
|
||||
// ErrorCode returns the JSON error code for a revert.
|
||||
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
|
||||
func (e *revertError) ErrorCode() int { |
||||
return 3 |
||||
} |
||||
|
||||
// ErrorData returns the hex encoded revert reason.
|
||||
func (e *revertError) ErrorData() interface{} { |
||||
return e.reason |
||||
} |
||||
|
||||
// CallContract executes a contract call.
|
||||
func (b *SimulatedBackend) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if blockNumber != nil && blockNumber.Cmp(b.blockchain.CurrentBlock().Number()) != 0 { |
||||
return nil, errBlockNumberUnsupported |
||||
} |
||||
stateDB, err := b.blockchain.State() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
res, err := b.callContract(ctx, call, b.blockchain.CurrentBlock(), stateDB) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// If the result contains a revert reason, try to unpack and return it.
|
||||
if len(res.Revert()) > 0 { |
||||
return nil, newRevertError(res) |
||||
} |
||||
return res.Return(), res.Err |
||||
} |
||||
|
||||
// PendingCallContract executes a contract call on the pending state.
|
||||
func (b *SimulatedBackend) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
defer b.pendingState.RevertToSnapshot(b.pendingState.Snapshot()) |
||||
|
||||
res, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// If the result contains a revert reason, try to unpack and return it.
|
||||
if len(res.Revert()) > 0 { |
||||
return nil, newRevertError(res) |
||||
} |
||||
return res.Return(), res.Err |
||||
} |
||||
|
||||
// PendingNonceAt implements PendingStateReader.PendingNonceAt, retrieving
|
||||
// the nonce currently pending for the account.
|
||||
func (b *SimulatedBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
return b.pendingState.GetOrNewStateObject(account).Nonce(), nil |
||||
} |
||||
|
||||
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
|
||||
// chain doesn't have miners, we just return a gas price of 1 for any call.
|
||||
func (b *SimulatedBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if b.pendingBlock.Header().BaseFee != nil { |
||||
return b.pendingBlock.Header().BaseFee, nil |
||||
} |
||||
return big.NewInt(1), nil |
||||
} |
||||
|
||||
// SuggestGasTipCap implements ContractTransactor.SuggestGasTipCap. Since the simulated
|
||||
// chain doesn't have miners, we just return a gas tip of 1 for any call.
|
||||
func (b *SimulatedBackend) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { |
||||
return big.NewInt(1), nil |
||||
} |
||||
|
||||
// EstimateGas executes the requested code against the currently pending block/state and
|
||||
// returns the used amount of gas.
|
||||
func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMsg) (uint64, error) { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
// Determine the lowest and highest possible gas limits to binary search in between
|
||||
var ( |
||||
lo uint64 = params.TxGas - 1 |
||||
hi uint64 |
||||
cap uint64 |
||||
) |
||||
if call.Gas >= params.TxGas { |
||||
hi = call.Gas |
||||
} else { |
||||
hi = b.pendingBlock.GasLimit() |
||||
} |
||||
// Normalize the max fee per gas the call is willing to spend.
|
||||
var feeCap *big.Int |
||||
if call.GasPrice != nil && (call.GasFeeCap != nil || call.GasTipCap != nil) { |
||||
return 0, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") |
||||
} else if call.GasPrice != nil { |
||||
feeCap = call.GasPrice |
||||
} else if call.GasFeeCap != nil { |
||||
feeCap = call.GasFeeCap |
||||
} else { |
||||
feeCap = common.Big0 |
||||
} |
||||
// Recap the highest gas allowance with account's balance.
|
||||
if feeCap.BitLen() != 0 { |
||||
balance := b.pendingState.GetBalance(call.From) // from can't be nil
|
||||
available := new(big.Int).Set(balance) |
||||
if call.Value != nil { |
||||
if call.Value.Cmp(available) >= 0 { |
||||
return 0, core.ErrInsufficientFundsForTransfer |
||||
} |
||||
available.Sub(available, call.Value) |
||||
} |
||||
allowance := new(big.Int).Div(available, feeCap) |
||||
if allowance.IsUint64() && hi > allowance.Uint64() { |
||||
transfer := call.Value |
||||
if transfer == nil { |
||||
transfer = new(big.Int) |
||||
} |
||||
log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance, |
||||
"sent", transfer, "feecap", feeCap, "fundable", allowance) |
||||
hi = allowance.Uint64() |
||||
} |
||||
} |
||||
cap = hi |
||||
|
||||
// Create a helper to check if a gas allowance results in an executable transaction
|
||||
executable := func(gas uint64) (bool, *core.ExecutionResult, error) { |
||||
call.Gas = gas |
||||
|
||||
snapshot := b.pendingState.Snapshot() |
||||
res, err := b.callContract(ctx, call, b.pendingBlock, b.pendingState) |
||||
b.pendingState.RevertToSnapshot(snapshot) |
||||
|
||||
if err != nil { |
||||
if errors.Is(err, core.ErrIntrinsicGas) { |
||||
return true, nil, nil // Special case, raise gas limit
|
||||
} |
||||
return true, nil, err // Bail out
|
||||
} |
||||
return res.Failed(), res, nil |
||||
} |
||||
// Execute the binary search and hone in on an executable gas limit
|
||||
for lo+1 < hi { |
||||
mid := (hi + lo) / 2 |
||||
failed, _, err := executable(mid) |
||||
|
||||
// If the error is not nil(consensus error), it means the provided message
|
||||
// call or transaction will never be accepted no matter how much gas it is
|
||||
// assigned. Return the error directly, don't struggle any more
|
||||
if err != nil { |
||||
return 0, err |
||||
} |
||||
if failed { |
||||
lo = mid |
||||
} else { |
||||
hi = mid |
||||
} |
||||
} |
||||
// Reject the transaction as invalid if it still fails at the highest allowance
|
||||
if hi == cap { |
||||
failed, result, err := executable(hi) |
||||
if err != nil { |
||||
return 0, err |
||||
} |
||||
if failed { |
||||
if result != nil && result.Err != vm.ErrOutOfGas { |
||||
if len(result.Revert()) > 0 { |
||||
return 0, newRevertError(result) |
||||
} |
||||
return 0, result.Err |
||||
} |
||||
// Otherwise, the specified gas cap is too low
|
||||
return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap) |
||||
} |
||||
} |
||||
return hi, nil |
||||
} |
||||
|
||||
// callContract implements common code between normal and pending contract calls.
|
||||
// state is modified during execution, make sure to copy it if necessary.
|
||||
func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallMsg, block *types.Block, stateDB *state.StateDB) (*core.ExecutionResult, error) { |
||||
// Gas prices post 1559 need to be initialized
|
||||
if call.GasPrice != nil && (call.GasFeeCap != nil || call.GasTipCap != nil) { |
||||
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") |
||||
} |
||||
head := b.blockchain.CurrentHeader() |
||||
if !b.blockchain.Config().IsLondon(head.Number) { |
||||
// If there's no basefee, then it must be a non-1559 execution
|
||||
if call.GasPrice == nil { |
||||
call.GasPrice = new(big.Int) |
||||
} |
||||
call.GasFeeCap, call.GasTipCap = call.GasPrice, call.GasPrice |
||||
} else { |
||||
// A basefee is provided, necessitating 1559-type execution
|
||||
if call.GasPrice != nil { |
||||
// User specified the legacy gas field, convert to 1559 gas typing
|
||||
call.GasFeeCap, call.GasTipCap = call.GasPrice, call.GasPrice |
||||
} else { |
||||
// User specified 1559 gas fields (or none), use those
|
||||
if call.GasFeeCap == nil { |
||||
call.GasFeeCap = new(big.Int) |
||||
} |
||||
if call.GasTipCap == nil { |
||||
call.GasTipCap = new(big.Int) |
||||
} |
||||
// Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
|
||||
call.GasPrice = new(big.Int) |
||||
if call.GasFeeCap.BitLen() > 0 || call.GasTipCap.BitLen() > 0 { |
||||
call.GasPrice = math.BigMin(new(big.Int).Add(call.GasTipCap, head.BaseFee), call.GasFeeCap) |
||||
} |
||||
} |
||||
} |
||||
// Ensure message is initialized properly.
|
||||
if call.Gas == 0 { |
||||
call.Gas = 50000000 |
||||
} |
||||
if call.Value == nil { |
||||
call.Value = new(big.Int) |
||||
} |
||||
// Set infinite balance to the fake caller account.
|
||||
from := stateDB.GetOrNewStateObject(call.From) |
||||
from.SetBalance(math.MaxBig256) |
||||
// Execute the call.
|
||||
msg := callMsg{call} |
||||
|
||||
txContext := core.NewEVMTxContext(msg) |
||||
evmContext := core.NewEVMBlockContext(block.Header(), b.blockchain, nil) |
||||
// Create a new environment which holds all relevant information
|
||||
// about the transaction and calling mechanisms.
|
||||
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true}) |
||||
gasPool := new(core.GasPool).AddGas(math.MaxUint64) |
||||
|
||||
return core.NewStateTransition(vmEnv, msg, gasPool).TransitionDb() |
||||
} |
||||
|
||||
// SendTransaction updates the pending block to include the given transaction.
|
||||
func (b *SimulatedBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
// Get the last block
|
||||
block, err := b.blockByHash(ctx, b.pendingBlock.ParentHash()) |
||||
if err != nil { |
||||
return fmt.Errorf("could not fetch parent") |
||||
} |
||||
// Check transaction validity
|
||||
signer := types.MakeSigner(b.blockchain.Config(), block.Number()) |
||||
sender, err := types.Sender(signer, tx) |
||||
if err != nil { |
||||
return fmt.Errorf("invalid transaction: %v", err) |
||||
} |
||||
nonce := b.pendingState.GetNonce(sender) |
||||
if tx.Nonce() != nonce { |
||||
return fmt.Errorf("invalid transaction nonce: got %d, want %d", tx.Nonce(), nonce) |
||||
} |
||||
// Include tx in chain
|
||||
blocks, receipts := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) { |
||||
for _, tx := range b.pendingBlock.Transactions() { |
||||
block.AddTxWithChain(b.blockchain, tx) |
||||
} |
||||
block.AddTxWithChain(b.blockchain, tx) |
||||
}) |
||||
stateDB, _ := b.blockchain.State() |
||||
|
||||
b.pendingBlock = blocks[0] |
||||
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil) |
||||
b.pendingReceipts = receipts[0] |
||||
return nil |
||||
} |
||||
|
||||
// FilterLogs executes a log filter operation, blocking during execution and
|
||||
// returning all the results in one batch.
|
||||
//
|
||||
// TODO(karalabe): Deprecate when the subscription one can return past data too.
|
||||
func (b *SimulatedBackend) FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error) { |
||||
var filter *filters.Filter |
||||
if query.BlockHash != nil { |
||||
// Block filter requested, construct a single-shot filter
|
||||
filter = b.filterSystem.NewBlockFilter(*query.BlockHash, query.Addresses, query.Topics) |
||||
} else { |
||||
// Initialize unset filter boundaries to run from genesis to chain head
|
||||
from := int64(0) |
||||
if query.FromBlock != nil { |
||||
from = query.FromBlock.Int64() |
||||
} |
||||
to := int64(-1) |
||||
if query.ToBlock != nil { |
||||
to = query.ToBlock.Int64() |
||||
} |
||||
// Construct the range filter
|
||||
filter = b.filterSystem.NewRangeFilter(from, to, query.Addresses, query.Topics) |
||||
} |
||||
// Run the filter and return all the logs
|
||||
logs, err := filter.Logs(ctx) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
res := make([]types.Log, len(logs)) |
||||
for i, nLog := range logs { |
||||
res[i] = *nLog |
||||
} |
||||
return res, nil |
||||
} |
||||
|
||||
// SubscribeFilterLogs creates a background log filtering operation, returning a
|
||||
// subscription immediately, which can be used to stream the found events.
|
||||
func (b *SimulatedBackend) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) { |
||||
// Subscribe to contract events
|
||||
sink := make(chan []*types.Log) |
||||
|
||||
sub, err := b.events.SubscribeLogs(query, sink) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Since we're getting logs in batches, we need to flatten them into a plain stream
|
||||
return event.NewSubscription(func(quit <-chan struct{}) error { |
||||
defer sub.Unsubscribe() |
||||
for { |
||||
select { |
||||
case logs := <-sink: |
||||
for _, nlog := range logs { |
||||
select { |
||||
case ch <- *nlog: |
||||
case err := <-sub.Err(): |
||||
return err |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
} |
||||
case err := <-sub.Err(): |
||||
return err |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
} |
||||
}), nil |
||||
} |
||||
|
||||
// SubscribeNewHead returns an event subscription for a new header.
|
||||
func (b *SimulatedBackend) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) { |
||||
// subscribe to a new head
|
||||
sink := make(chan *types.Header) |
||||
sub := b.events.SubscribeNewHeads(sink) |
||||
|
||||
return event.NewSubscription(func(quit <-chan struct{}) error { |
||||
defer sub.Unsubscribe() |
||||
for { |
||||
select { |
||||
case head := <-sink: |
||||
select { |
||||
case ch <- head: |
||||
case err := <-sub.Err(): |
||||
return err |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
case err := <-sub.Err(): |
||||
return err |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
} |
||||
}), nil |
||||
} |
||||
|
||||
// AdjustTime adds a time shift to the simulated clock.
|
||||
// It can only be called on empty blocks.
|
||||
func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error { |
||||
b.mu.Lock() |
||||
defer b.mu.Unlock() |
||||
|
||||
if len(b.pendingBlock.Transactions()) != 0 { |
||||
return errors.New("Could not adjust time on non-empty block") |
||||
} |
||||
// Get the last block
|
||||
block := b.blockchain.GetBlockByHash(b.pendingBlock.ParentHash()) |
||||
if block == nil { |
||||
return fmt.Errorf("could not find parent") |
||||
} |
||||
|
||||
blocks, _ := core.GenerateChain(b.config, block, ethash.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) { |
||||
block.OffsetTime(int64(adjustment.Seconds())) |
||||
}) |
||||
stateDB, _ := b.blockchain.State() |
||||
|
||||
b.pendingBlock = blocks[0] |
||||
b.pendingState, _ = state.New(b.pendingBlock.Root(), stateDB.Database(), nil) |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// Blockchain returns the underlying blockchain.
|
||||
func (b *SimulatedBackend) Blockchain() *core.BlockChain { |
||||
return b.blockchain |
||||
} |
||||
|
||||
// callMsg implements core.Message to allow passing it as a transaction simulator.
|
||||
type callMsg struct { |
||||
ethereum.CallMsg |
||||
} |
||||
|
||||
func (m callMsg) From() common.Address { return m.CallMsg.From } |
||||
func (m callMsg) Nonce() uint64 { return 0 } |
||||
func (m callMsg) IsFake() bool { return true } |
||||
func (m callMsg) To() *common.Address { return m.CallMsg.To } |
||||
func (m callMsg) GasPrice() *big.Int { return m.CallMsg.GasPrice } |
||||
func (m callMsg) GasFeeCap() *big.Int { return m.CallMsg.GasFeeCap } |
||||
func (m callMsg) GasTipCap() *big.Int { return m.CallMsg.GasTipCap } |
||||
func (m callMsg) Gas() uint64 { return m.CallMsg.Gas } |
||||
func (m callMsg) Value() *big.Int { return m.CallMsg.Value } |
||||
func (m callMsg) Data() []byte { return m.CallMsg.Data } |
||||
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList } |
||||
|
||||
// filterBackend implements filters.Backend to support filtering for logs without
|
||||
// taking bloom-bits acceleration structures into account.
|
||||
type filterBackend struct { |
||||
db ethdb.Database |
||||
bc *core.BlockChain |
||||
backend *SimulatedBackend |
||||
} |
||||
|
||||
func (fb *filterBackend) ChainDb() ethdb.Database { return fb.db } |
||||
|
||||
func (fb *filterBackend) EventMux() *event.TypeMux { panic("not supported") } |
||||
|
||||
func (fb *filterBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { |
||||
switch number { |
||||
case rpc.PendingBlockNumber: |
||||
if block := fb.backend.pendingBlock; block != nil { |
||||
return block.Header(), nil |
||||
} |
||||
return nil, nil |
||||
case rpc.LatestBlockNumber: |
||||
return fb.bc.CurrentHeader(), nil |
||||
case rpc.FinalizedBlockNumber: |
||||
if block := fb.bc.CurrentFinalizedBlock(); block != nil { |
||||
return block.Header(), nil |
||||
} |
||||
return nil, errors.New("finalized block not found") |
||||
case rpc.SafeBlockNumber: |
||||
if block := fb.bc.CurrentSafeBlock(); block != nil { |
||||
return block.Header(), nil |
||||
} |
||||
return nil, errors.New("safe block not found") |
||||
default: |
||||
return fb.bc.GetHeaderByNumber(uint64(number.Int64())), nil |
||||
} |
||||
} |
||||
|
||||
func (fb *filterBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) { |
||||
return fb.bc.GetHeaderByHash(hash), nil |
||||
} |
||||
|
||||
func (fb *filterBackend) PendingBlockAndReceipts() (*types.Block, types.Receipts) { |
||||
return fb.backend.pendingBlock, fb.backend.pendingReceipts |
||||
} |
||||
|
||||
func (fb *filterBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { |
||||
number := rawdb.ReadHeaderNumber(fb.db, hash) |
||||
if number == nil { |
||||
return nil, nil |
||||
} |
||||
return rawdb.ReadReceipts(fb.db, hash, *number, fb.bc.Config()), nil |
||||
} |
||||
|
||||
func (fb *filterBackend) GetLogs(ctx context.Context, hash common.Hash, number uint64) ([][]*types.Log, error) { |
||||
logs := rawdb.ReadLogs(fb.db, hash, number, fb.bc.Config()) |
||||
return logs, nil |
||||
} |
||||
|
||||
func (fb *filterBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription { |
||||
return nullSubscription() |
||||
} |
||||
|
||||
func (fb *filterBackend) SubscribeChainEvent(ch chan<- core.ChainEvent) event.Subscription { |
||||
return fb.bc.SubscribeChainEvent(ch) |
||||
} |
||||
|
||||
func (fb *filterBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { |
||||
return fb.bc.SubscribeRemovedLogsEvent(ch) |
||||
} |
||||
|
||||
func (fb *filterBackend) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscription { |
||||
return fb.bc.SubscribeLogsEvent(ch) |
||||
} |
||||
|
||||
func (fb *filterBackend) SubscribePendingLogsEvent(ch chan<- []*types.Log) event.Subscription { |
||||
return nullSubscription() |
||||
} |
||||
|
||||
func (fb *filterBackend) BloomStatus() (uint64, uint64) { return 4096, 0 } |
||||
|
||||
func (fb *filterBackend) ServiceFilter(ctx context.Context, ms *bloombits.MatcherSession) { |
||||
panic("not supported") |
||||
} |
||||
|
||||
func (fb *filterBackend) ChainConfig() *params.ChainConfig { |
||||
panic("not supported") |
||||
} |
||||
|
||||
func (fb *filterBackend) CurrentHeader() *types.Header { |
||||
panic("not supported") |
||||
} |
||||
|
||||
func nullSubscription() event.Subscription { |
||||
return event.NewSubscription(func(quit <-chan struct{}) error { |
||||
<-quit |
||||
return nil |
||||
}) |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,534 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
"strings" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/accounts/abi" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
) |
||||
|
||||
const basefeeWiggleMultiplier = 2 |
||||
|
||||
// SignerFn is a signer function callback when a contract requires a method to
|
||||
// sign the transaction before submission.
|
||||
type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error) |
||||
|
||||
// CallOpts is the collection of options to fine tune a contract call request.
|
||||
type CallOpts struct { |
||||
Pending bool // Whether to operate on the pending state or the last known one
|
||||
From common.Address // Optional the sender address, otherwise the first account is used
|
||||
BlockNumber *big.Int // Optional the block number on which the call should be performed
|
||||
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
||||
} |
||||
|
||||
// TransactOpts is the collection of authorization data required to create a
|
||||
// valid Ethereum transaction.
|
||||
type TransactOpts struct { |
||||
From common.Address // Ethereum account to send the transaction from
|
||||
Nonce *big.Int // Nonce to use for the transaction execution (nil = use pending state)
|
||||
Signer SignerFn // Method to use for signing the transaction (mandatory)
|
||||
|
||||
Value *big.Int // Funds to transfer along the transaction (nil = 0 = no funds)
|
||||
GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
|
||||
GasFeeCap *big.Int // Gas fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
||||
GasTipCap *big.Int // Gas priority fee cap to use for the 1559 transaction execution (nil = gas price oracle)
|
||||
GasLimit uint64 // Gas limit to set for the transaction execution (0 = estimate)
|
||||
|
||||
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
||||
|
||||
NoSend bool // Do all transact steps but do not send the transaction
|
||||
} |
||||
|
||||
// FilterOpts is the collection of options to fine tune filtering for events
|
||||
// within a bound contract.
|
||||
type FilterOpts struct { |
||||
Start uint64 // Start of the queried range
|
||||
End *uint64 // End of the range (nil = latest)
|
||||
|
||||
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
||||
} |
||||
|
||||
// WatchOpts is the collection of options to fine tune subscribing for events
|
||||
// within a bound contract.
|
||||
type WatchOpts struct { |
||||
Start *uint64 // Start of the queried range (nil = latest)
|
||||
Context context.Context // Network context to support cancellation and timeouts (nil = no timeout)
|
||||
} |
||||
|
||||
// MetaData collects all metadata for a bound contract.
|
||||
type MetaData struct { |
||||
mu sync.Mutex |
||||
Sigs map[string]string |
||||
Bin string |
||||
ABI string |
||||
ab *abi.ABI |
||||
} |
||||
|
||||
func (m *MetaData) GetAbi() (*abi.ABI, error) { |
||||
m.mu.Lock() |
||||
defer m.mu.Unlock() |
||||
if m.ab != nil { |
||||
return m.ab, nil |
||||
} |
||||
if parsed, err := abi.JSON(strings.NewReader(m.ABI)); err != nil { |
||||
return nil, err |
||||
} else { |
||||
m.ab = &parsed |
||||
} |
||||
return m.ab, nil |
||||
} |
||||
|
||||
// BoundContract is the base wrapper object that reflects a contract on the
|
||||
// Ethereum network. It contains a collection of methods that are used by the
|
||||
// higher level contract bindings to operate.
|
||||
type BoundContract struct { |
||||
address common.Address // Deployment address of the contract on the Ethereum blockchain
|
||||
abi abi.ABI // Reflect based ABI to access the correct Ethereum methods
|
||||
caller ContractCaller // Read interface to interact with the blockchain
|
||||
transactor ContractTransactor // Write interface to interact with the blockchain
|
||||
filterer ContractFilterer // Event filtering to interact with the blockchain
|
||||
} |
||||
|
||||
// NewBoundContract creates a low level contract interface through which calls
|
||||
// and transactions may be made through.
|
||||
func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor, filterer ContractFilterer) *BoundContract { |
||||
return &BoundContract{ |
||||
address: address, |
||||
abi: abi, |
||||
caller: caller, |
||||
transactor: transactor, |
||||
filterer: filterer, |
||||
} |
||||
} |
||||
|
||||
// DeployContract deploys a contract onto the Ethereum blockchain and binds the
|
||||
// deployment address with a Go wrapper.
|
||||
func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error) { |
||||
// Otherwise try to deploy the contract
|
||||
c := NewBoundContract(common.Address{}, abi, backend, backend, backend) |
||||
|
||||
input, err := c.abi.Pack("", params...) |
||||
if err != nil { |
||||
return common.Address{}, nil, nil, err |
||||
} |
||||
tx, err := c.transact(opts, nil, append(bytecode, input...)) |
||||
if err != nil { |
||||
return common.Address{}, nil, nil, err |
||||
} |
||||
c.address = crypto.CreateAddress(opts.From, tx.Nonce()) |
||||
return c.address, tx, c, nil |
||||
} |
||||
|
||||
// Call invokes the (constant) contract method with params as input values and
|
||||
// sets the output to result. The result type might be a single field for simple
|
||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||
// returns.
|
||||
func (c *BoundContract) Call(opts *CallOpts, results *[]interface{}, method string, params ...interface{}) error { |
||||
// Don't crash on a lazy user
|
||||
if opts == nil { |
||||
opts = new(CallOpts) |
||||
} |
||||
if results == nil { |
||||
results = new([]interface{}) |
||||
} |
||||
// Pack the input, call and unpack the results
|
||||
input, err := c.abi.Pack(method, params...) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
var ( |
||||
msg = ethereum.CallMsg{From: opts.From, To: &c.address, Data: input} |
||||
ctx = ensureContext(opts.Context) |
||||
code []byte |
||||
output []byte |
||||
) |
||||
if opts.Pending { |
||||
pb, ok := c.caller.(PendingContractCaller) |
||||
if !ok { |
||||
return ErrNoPendingState |
||||
} |
||||
output, err = pb.PendingCallContract(ctx, msg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if len(output) == 0 { |
||||
// Make sure we have a contract to operate on, and bail out otherwise.
|
||||
if code, err = pb.PendingCodeAt(ctx, c.address); err != nil { |
||||
return err |
||||
} else if len(code) == 0 { |
||||
return ErrNoCode |
||||
} |
||||
} |
||||
} else { |
||||
output, err = c.caller.CallContract(ctx, msg, opts.BlockNumber) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if len(output) == 0 { |
||||
// Make sure we have a contract to operate on, and bail out otherwise.
|
||||
if code, err = c.caller.CodeAt(ctx, c.address, opts.BlockNumber); err != nil { |
||||
return err |
||||
} else if len(code) == 0 { |
||||
return ErrNoCode |
||||
} |
||||
} |
||||
} |
||||
|
||||
if len(*results) == 0 { |
||||
res, err := c.abi.Unpack(method, output) |
||||
*results = res |
||||
return err |
||||
} |
||||
res := *results |
||||
return c.abi.UnpackIntoInterface(res[0], method, output) |
||||
} |
||||
|
||||
// Transact invokes the (paid) contract method with params as input values.
|
||||
func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { |
||||
// Otherwise pack up the parameters and invoke the contract
|
||||
input, err := c.abi.Pack(method, params...) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// todo(rjl493456442) check the method is payable or not,
|
||||
// reject invalid transaction at the first place
|
||||
return c.transact(opts, &c.address, input) |
||||
} |
||||
|
||||
// RawTransact initiates a transaction with the given raw calldata as the input.
|
||||
// It's usually used to initiate transactions for invoking **Fallback** function.
|
||||
func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (*types.Transaction, error) { |
||||
// todo(rjl493456442) check the method is payable or not,
|
||||
// reject invalid transaction at the first place
|
||||
return c.transact(opts, &c.address, calldata) |
||||
} |
||||
|
||||
// Transfer initiates a plain transaction to move funds to the contract, calling
|
||||
// its default method if one is available.
|
||||
func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error) { |
||||
// todo(rjl493456442) check the payable fallback or receive is defined
|
||||
// or not, reject invalid transaction at the first place
|
||||
return c.transact(opts, &c.address, nil) |
||||
} |
||||
|
||||
func (c *BoundContract) createDynamicTx(opts *TransactOpts, contract *common.Address, input []byte, head *types.Header) (*types.Transaction, error) { |
||||
// Normalize value
|
||||
value := opts.Value |
||||
if value == nil { |
||||
value = new(big.Int) |
||||
} |
||||
// Estimate TipCap
|
||||
gasTipCap := opts.GasTipCap |
||||
if gasTipCap == nil { |
||||
tip, err := c.transactor.SuggestGasTipCap(ensureContext(opts.Context)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
gasTipCap = tip |
||||
} |
||||
// Estimate FeeCap
|
||||
gasFeeCap := opts.GasFeeCap |
||||
if gasFeeCap == nil { |
||||
gasFeeCap = new(big.Int).Add( |
||||
gasTipCap, |
||||
new(big.Int).Mul(head.BaseFee, big.NewInt(basefeeWiggleMultiplier)), |
||||
) |
||||
} |
||||
if gasFeeCap.Cmp(gasTipCap) < 0 { |
||||
return nil, fmt.Errorf("maxFeePerGas (%v) < maxPriorityFeePerGas (%v)", gasFeeCap, gasTipCap) |
||||
} |
||||
// Estimate GasLimit
|
||||
gasLimit := opts.GasLimit |
||||
if opts.GasLimit == 0 { |
||||
var err error |
||||
gasLimit, err = c.estimateGasLimit(opts, contract, input, nil, gasTipCap, gasFeeCap, value) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
// create the transaction
|
||||
nonce, err := c.getNonce(opts) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
baseTx := &types.DynamicFeeTx{ |
||||
To: contract, |
||||
Nonce: nonce, |
||||
GasFeeCap: gasFeeCap, |
||||
GasTipCap: gasTipCap, |
||||
Gas: gasLimit, |
||||
Value: value, |
||||
Data: input, |
||||
} |
||||
return types.NewTx(baseTx), nil |
||||
} |
||||
|
||||
func (c *BoundContract) createLegacyTx(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) { |
||||
if opts.GasFeeCap != nil || opts.GasTipCap != nil { |
||||
return nil, errors.New("maxFeePerGas or maxPriorityFeePerGas specified but london is not active yet") |
||||
} |
||||
// Normalize value
|
||||
value := opts.Value |
||||
if value == nil { |
||||
value = new(big.Int) |
||||
} |
||||
// Estimate GasPrice
|
||||
gasPrice := opts.GasPrice |
||||
if gasPrice == nil { |
||||
price, err := c.transactor.SuggestGasPrice(ensureContext(opts.Context)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
gasPrice = price |
||||
} |
||||
// Estimate GasLimit
|
||||
gasLimit := opts.GasLimit |
||||
if opts.GasLimit == 0 { |
||||
var err error |
||||
gasLimit, err = c.estimateGasLimit(opts, contract, input, gasPrice, nil, nil, value) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
// create the transaction
|
||||
nonce, err := c.getNonce(opts) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
baseTx := &types.LegacyTx{ |
||||
To: contract, |
||||
Nonce: nonce, |
||||
GasPrice: gasPrice, |
||||
Gas: gasLimit, |
||||
Value: value, |
||||
Data: input, |
||||
} |
||||
return types.NewTx(baseTx), nil |
||||
} |
||||
|
||||
func (c *BoundContract) estimateGasLimit(opts *TransactOpts, contract *common.Address, input []byte, gasPrice, gasTipCap, gasFeeCap, value *big.Int) (uint64, error) { |
||||
if contract != nil { |
||||
// Gas estimation cannot succeed without code for method invocations.
|
||||
if code, err := c.transactor.PendingCodeAt(ensureContext(opts.Context), c.address); err != nil { |
||||
return 0, err |
||||
} else if len(code) == 0 { |
||||
return 0, ErrNoCode |
||||
} |
||||
} |
||||
msg := ethereum.CallMsg{ |
||||
From: opts.From, |
||||
To: contract, |
||||
GasPrice: gasPrice, |
||||
GasTipCap: gasTipCap, |
||||
GasFeeCap: gasFeeCap, |
||||
Value: value, |
||||
Data: input, |
||||
} |
||||
return c.transactor.EstimateGas(ensureContext(opts.Context), msg) |
||||
} |
||||
|
||||
func (c *BoundContract) getNonce(opts *TransactOpts) (uint64, error) { |
||||
if opts.Nonce == nil { |
||||
return c.transactor.PendingNonceAt(ensureContext(opts.Context), opts.From) |
||||
} else { |
||||
return opts.Nonce.Uint64(), nil |
||||
} |
||||
} |
||||
|
||||
// transact executes an actual transaction invocation, first deriving any missing
|
||||
// authorization fields, and then scheduling the transaction for execution.
|
||||
func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) { |
||||
if opts.GasPrice != nil && (opts.GasFeeCap != nil || opts.GasTipCap != nil) { |
||||
return nil, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") |
||||
} |
||||
// Create the transaction
|
||||
var ( |
||||
rawTx *types.Transaction |
||||
err error |
||||
) |
||||
if opts.GasPrice != nil { |
||||
rawTx, err = c.createLegacyTx(opts, contract, input) |
||||
} else if opts.GasFeeCap != nil && opts.GasTipCap != nil { |
||||
rawTx, err = c.createDynamicTx(opts, contract, input, nil) |
||||
} else { |
||||
// Only query for basefee if gasPrice not specified
|
||||
if head, errHead := c.transactor.HeaderByNumber(ensureContext(opts.Context), nil); errHead != nil { |
||||
return nil, errHead |
||||
} else if head.BaseFee != nil { |
||||
rawTx, err = c.createDynamicTx(opts, contract, input, head) |
||||
} else { |
||||
// Chain is not London ready -> use legacy transaction
|
||||
rawTx, err = c.createLegacyTx(opts, contract, input) |
||||
} |
||||
} |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Sign the transaction and schedule it for execution
|
||||
if opts.Signer == nil { |
||||
return nil, errors.New("no signer to authorize the transaction with") |
||||
} |
||||
signedTx, err := opts.Signer(opts.From, rawTx) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if opts.NoSend { |
||||
return signedTx, nil |
||||
} |
||||
if err := c.transactor.SendTransaction(ensureContext(opts.Context), signedTx); err != nil { |
||||
return nil, err |
||||
} |
||||
return signedTx, nil |
||||
} |
||||
|
||||
// FilterLogs filters contract logs for past blocks, returning the necessary
|
||||
// channels to construct a strongly typed bound iterator on top of them.
|
||||
func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) { |
||||
// Don't crash on a lazy user
|
||||
if opts == nil { |
||||
opts = new(FilterOpts) |
||||
} |
||||
// Append the event selector to the query parameters and construct the topic set
|
||||
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...) |
||||
|
||||
topics, err := abi.MakeTopics(query...) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
// Start the background filtering
|
||||
logs := make(chan types.Log, 128) |
||||
|
||||
config := ethereum.FilterQuery{ |
||||
Addresses: []common.Address{c.address}, |
||||
Topics: topics, |
||||
FromBlock: new(big.Int).SetUint64(opts.Start), |
||||
} |
||||
if opts.End != nil { |
||||
config.ToBlock = new(big.Int).SetUint64(*opts.End) |
||||
} |
||||
/* TODO(karalabe): Replace the rest of the method below with this when supported |
||||
sub, err := c.filterer.SubscribeFilterLogs(ensureContext(opts.Context), config, logs) |
||||
*/ |
||||
buff, err := c.filterer.FilterLogs(ensureContext(opts.Context), config) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
sub, err := event.NewSubscription(func(quit <-chan struct{}) error { |
||||
for _, log := range buff { |
||||
select { |
||||
case logs <- log: |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
} |
||||
return nil |
||||
}), nil |
||||
|
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
return logs, sub, nil |
||||
} |
||||
|
||||
// WatchLogs filters subscribes to contract logs for future blocks, returning a
|
||||
// subscription object that can be used to tear down the watcher.
|
||||
func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) { |
||||
// Don't crash on a lazy user
|
||||
if opts == nil { |
||||
opts = new(WatchOpts) |
||||
} |
||||
// Append the event selector to the query parameters and construct the topic set
|
||||
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...) |
||||
|
||||
topics, err := abi.MakeTopics(query...) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
// Start the background filtering
|
||||
logs := make(chan types.Log, 128) |
||||
|
||||
config := ethereum.FilterQuery{ |
||||
Addresses: []common.Address{c.address}, |
||||
Topics: topics, |
||||
} |
||||
if opts.Start != nil { |
||||
config.FromBlock = new(big.Int).SetUint64(*opts.Start) |
||||
} |
||||
sub, err := c.filterer.SubscribeFilterLogs(ensureContext(opts.Context), config, logs) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
return logs, sub, nil |
||||
} |
||||
|
||||
// UnpackLog unpacks a retrieved log into the provided output structure.
|
||||
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error { |
||||
if log.Topics[0] != c.abi.Events[event].ID { |
||||
return fmt.Errorf("event signature mismatch") |
||||
} |
||||
if len(log.Data) > 0 { |
||||
if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
var indexed abi.Arguments |
||||
for _, arg := range c.abi.Events[event].Inputs { |
||||
if arg.Indexed { |
||||
indexed = append(indexed, arg) |
||||
} |
||||
} |
||||
return abi.ParseTopics(out, indexed, log.Topics[1:]) |
||||
} |
||||
|
||||
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
|
||||
func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error { |
||||
if log.Topics[0] != c.abi.Events[event].ID { |
||||
return fmt.Errorf("event signature mismatch") |
||||
} |
||||
if len(log.Data) > 0 { |
||||
if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
var indexed abi.Arguments |
||||
for _, arg := range c.abi.Events[event].Inputs { |
||||
if arg.Indexed { |
||||
indexed = append(indexed, arg) |
||||
} |
||||
} |
||||
return abi.ParseTopicsIntoMap(out, indexed, log.Topics[1:]) |
||||
} |
||||
|
||||
// ensureContext is a helper method to ensure a context is not nil, even if the
|
||||
// user specified it as such.
|
||||
func ensureContext(ctx context.Context) context.Context { |
||||
if ctx == nil { |
||||
return context.Background() |
||||
} |
||||
return ctx |
||||
} |
@ -1,497 +0,0 @@ |
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind_test |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"math/big" |
||||
"reflect" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/accounts/abi" |
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func mockSign(addr common.Address, tx *types.Transaction) (*types.Transaction, error) { return tx, nil } |
||||
|
||||
type mockTransactor struct { |
||||
baseFee *big.Int |
||||
gasTipCap *big.Int |
||||
gasPrice *big.Int |
||||
suggestGasTipCapCalled bool |
||||
suggestGasPriceCalled bool |
||||
} |
||||
|
||||
func (mt *mockTransactor) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { |
||||
return &types.Header{BaseFee: mt.baseFee}, nil |
||||
} |
||||
|
||||
func (mt *mockTransactor) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) { |
||||
return []byte{1}, nil |
||||
} |
||||
|
||||
func (mt *mockTransactor) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { |
||||
return 0, nil |
||||
} |
||||
|
||||
func (mt *mockTransactor) SuggestGasPrice(ctx context.Context) (*big.Int, error) { |
||||
mt.suggestGasPriceCalled = true |
||||
return mt.gasPrice, nil |
||||
} |
||||
|
||||
func (mt *mockTransactor) SuggestGasTipCap(ctx context.Context) (*big.Int, error) { |
||||
mt.suggestGasTipCapCalled = true |
||||
return mt.gasTipCap, nil |
||||
} |
||||
|
||||
func (mt *mockTransactor) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) { |
||||
return 0, nil |
||||
} |
||||
|
||||
func (mt *mockTransactor) SendTransaction(ctx context.Context, tx *types.Transaction) error { |
||||
return nil |
||||
} |
||||
|
||||
type mockCaller struct { |
||||
codeAtBlockNumber *big.Int |
||||
callContractBlockNumber *big.Int |
||||
callContractBytes []byte |
||||
callContractErr error |
||||
codeAtBytes []byte |
||||
codeAtErr error |
||||
} |
||||
|
||||
func (mc *mockCaller) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) { |
||||
mc.codeAtBlockNumber = blockNumber |
||||
return mc.codeAtBytes, mc.codeAtErr |
||||
} |
||||
|
||||
func (mc *mockCaller) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { |
||||
mc.callContractBlockNumber = blockNumber |
||||
return mc.callContractBytes, mc.callContractErr |
||||
} |
||||
|
||||
type mockPendingCaller struct { |
||||
*mockCaller |
||||
pendingCodeAtBytes []byte |
||||
pendingCodeAtErr error |
||||
pendingCodeAtCalled bool |
||||
pendingCallContractCalled bool |
||||
pendingCallContractBytes []byte |
||||
pendingCallContractErr error |
||||
} |
||||
|
||||
func (mc *mockPendingCaller) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) { |
||||
mc.pendingCodeAtCalled = true |
||||
return mc.pendingCodeAtBytes, mc.pendingCodeAtErr |
||||
} |
||||
|
||||
func (mc *mockPendingCaller) PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error) { |
||||
mc.pendingCallContractCalled = true |
||||
return mc.pendingCallContractBytes, mc.pendingCallContractErr |
||||
} |
||||
|
||||
func TestPassingBlockNumber(t *testing.T) { |
||||
mc := &mockPendingCaller{ |
||||
mockCaller: &mockCaller{ |
||||
codeAtBytes: []byte{1, 2, 3}, |
||||
}, |
||||
} |
||||
|
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{ |
||||
Methods: map[string]abi.Method{ |
||||
"something": { |
||||
Name: "something", |
||||
Outputs: abi.Arguments{}, |
||||
}, |
||||
}, |
||||
}, mc, nil, nil) |
||||
|
||||
blockNumber := big.NewInt(42) |
||||
|
||||
bc.Call(&bind.CallOpts{BlockNumber: blockNumber}, nil, "something") |
||||
|
||||
if mc.callContractBlockNumber != blockNumber { |
||||
t.Fatalf("CallContract() was not passed the block number") |
||||
} |
||||
|
||||
if mc.codeAtBlockNumber != blockNumber { |
||||
t.Fatalf("CodeAt() was not passed the block number") |
||||
} |
||||
|
||||
bc.Call(&bind.CallOpts{}, nil, "something") |
||||
|
||||
if mc.callContractBlockNumber != nil { |
||||
t.Fatalf("CallContract() was passed a block number when it should not have been") |
||||
} |
||||
|
||||
if mc.codeAtBlockNumber != nil { |
||||
t.Fatalf("CodeAt() was passed a block number when it should not have been") |
||||
} |
||||
|
||||
bc.Call(&bind.CallOpts{BlockNumber: blockNumber, Pending: true}, nil, "something") |
||||
|
||||
if !mc.pendingCallContractCalled { |
||||
t.Fatalf("CallContract() was not passed the block number") |
||||
} |
||||
|
||||
if !mc.pendingCodeAtCalled { |
||||
t.Fatalf("CodeAt() was not passed the block number") |
||||
} |
||||
} |
||||
|
||||
const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158" |
||||
|
||||
func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) { |
||||
hash := crypto.Keccak256Hash([]byte("testName")) |
||||
topics := []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("received(string,address,uint256,bytes)")), |
||||
hash, |
||||
} |
||||
mockLog := newMockLog(topics, common.HexToHash("0x0")) |
||||
|
||||
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"string"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]` |
||||
parsedAbi, _ := abi.JSON(strings.NewReader(abiString)) |
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil) |
||||
|
||||
expectedReceivedMap := map[string]interface{}{ |
||||
"name": hash, |
||||
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), |
||||
"amount": big.NewInt(1), |
||||
"memo": []byte{88}, |
||||
} |
||||
unpackAndCheck(t, bc, expectedReceivedMap, mockLog) |
||||
} |
||||
|
||||
func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) { |
||||
sliceBytes, err := rlp.EncodeToBytes([]string{"name1", "name2", "name3", "name4"}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
hash := crypto.Keccak256Hash(sliceBytes) |
||||
topics := []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("received(string[],address,uint256,bytes)")), |
||||
hash, |
||||
} |
||||
mockLog := newMockLog(topics, common.HexToHash("0x0")) |
||||
|
||||
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"names","type":"string[]"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]` |
||||
parsedAbi, _ := abi.JSON(strings.NewReader(abiString)) |
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil) |
||||
|
||||
expectedReceivedMap := map[string]interface{}{ |
||||
"names": hash, |
||||
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), |
||||
"amount": big.NewInt(1), |
||||
"memo": []byte{88}, |
||||
} |
||||
unpackAndCheck(t, bc, expectedReceivedMap, mockLog) |
||||
} |
||||
|
||||
func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) { |
||||
arrBytes, err := rlp.EncodeToBytes([2]common.Address{common.HexToAddress("0x0"), common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2")}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
hash := crypto.Keccak256Hash(arrBytes) |
||||
topics := []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("received(address[2],address,uint256,bytes)")), |
||||
hash, |
||||
} |
||||
mockLog := newMockLog(topics, common.HexToHash("0x0")) |
||||
|
||||
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"addresses","type":"address[2]"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]` |
||||
parsedAbi, _ := abi.JSON(strings.NewReader(abiString)) |
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil) |
||||
|
||||
expectedReceivedMap := map[string]interface{}{ |
||||
"addresses": hash, |
||||
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), |
||||
"amount": big.NewInt(1), |
||||
"memo": []byte{88}, |
||||
} |
||||
unpackAndCheck(t, bc, expectedReceivedMap, mockLog) |
||||
} |
||||
|
||||
func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) { |
||||
mockAddress := common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2") |
||||
addrBytes := mockAddress.Bytes() |
||||
hash := crypto.Keccak256Hash([]byte("mockFunction(address,uint)")) |
||||
functionSelector := hash[:4] |
||||
functionTyBytes := append(addrBytes, functionSelector...) |
||||
var functionTy [24]byte |
||||
copy(functionTy[:], functionTyBytes[0:24]) |
||||
topics := []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("received(function,address,uint256,bytes)")), |
||||
common.BytesToHash(functionTyBytes), |
||||
} |
||||
mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42")) |
||||
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"function","type":"function"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]` |
||||
parsedAbi, _ := abi.JSON(strings.NewReader(abiString)) |
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil) |
||||
|
||||
expectedReceivedMap := map[string]interface{}{ |
||||
"function": functionTy, |
||||
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), |
||||
"amount": big.NewInt(1), |
||||
"memo": []byte{88}, |
||||
} |
||||
unpackAndCheck(t, bc, expectedReceivedMap, mockLog) |
||||
} |
||||
|
||||
func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) { |
||||
bytes := []byte{1, 2, 3, 4, 5} |
||||
hash := crypto.Keccak256Hash(bytes) |
||||
topics := []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("received(bytes,address,uint256,bytes)")), |
||||
hash, |
||||
} |
||||
mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42")) |
||||
|
||||
abiString := `[{"anonymous":false,"inputs":[{"indexed":true,"name":"content","type":"bytes"},{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"}]` |
||||
parsedAbi, _ := abi.JSON(strings.NewReader(abiString)) |
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), parsedAbi, nil, nil, nil) |
||||
|
||||
expectedReceivedMap := map[string]interface{}{ |
||||
"content": hash, |
||||
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), |
||||
"amount": big.NewInt(1), |
||||
"memo": []byte{88}, |
||||
} |
||||
unpackAndCheck(t, bc, expectedReceivedMap, mockLog) |
||||
} |
||||
|
||||
func TestTransactGasFee(t *testing.T) { |
||||
assert := assert.New(t) |
||||
|
||||
// GasTipCap and GasFeeCap
|
||||
// When opts.GasTipCap and opts.GasFeeCap are nil
|
||||
mt := &mockTransactor{baseFee: big.NewInt(100), gasTipCap: big.NewInt(5)} |
||||
bc := bind.NewBoundContract(common.Address{}, abi.ABI{}, nil, mt, nil) |
||||
opts := &bind.TransactOpts{Signer: mockSign} |
||||
tx, err := bc.Transact(opts, "") |
||||
assert.Nil(err) |
||||
assert.Equal(big.NewInt(5), tx.GasTipCap()) |
||||
assert.Equal(big.NewInt(205), tx.GasFeeCap()) |
||||
assert.Nil(opts.GasTipCap) |
||||
assert.Nil(opts.GasFeeCap) |
||||
assert.True(mt.suggestGasTipCapCalled) |
||||
|
||||
// Second call to Transact should use latest suggested GasTipCap
|
||||
mt.gasTipCap = big.NewInt(6) |
||||
mt.suggestGasTipCapCalled = false |
||||
tx, err = bc.Transact(opts, "") |
||||
assert.Nil(err) |
||||
assert.Equal(big.NewInt(6), tx.GasTipCap()) |
||||
assert.Equal(big.NewInt(206), tx.GasFeeCap()) |
||||
assert.True(mt.suggestGasTipCapCalled) |
||||
|
||||
// GasPrice
|
||||
// When opts.GasPrice is nil
|
||||
mt = &mockTransactor{gasPrice: big.NewInt(5)} |
||||
bc = bind.NewBoundContract(common.Address{}, abi.ABI{}, nil, mt, nil) |
||||
opts = &bind.TransactOpts{Signer: mockSign} |
||||
tx, err = bc.Transact(opts, "") |
||||
assert.Nil(err) |
||||
assert.Equal(big.NewInt(5), tx.GasPrice()) |
||||
assert.Nil(opts.GasPrice) |
||||
assert.True(mt.suggestGasPriceCalled) |
||||
|
||||
// Second call to Transact should use latest suggested GasPrice
|
||||
mt.gasPrice = big.NewInt(6) |
||||
mt.suggestGasPriceCalled = false |
||||
tx, err = bc.Transact(opts, "") |
||||
assert.Nil(err) |
||||
assert.Equal(big.NewInt(6), tx.GasPrice()) |
||||
assert.True(mt.suggestGasPriceCalled) |
||||
} |
||||
|
||||
func unpackAndCheck(t *testing.T, bc *bind.BoundContract, expected map[string]interface{}, mockLog types.Log) { |
||||
received := make(map[string]interface{}) |
||||
if err := bc.UnpackLogIntoMap(received, "received", mockLog); err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if len(received) != len(expected) { |
||||
t.Fatalf("unpacked map length %v not equal expected length of %v", len(received), len(expected)) |
||||
} |
||||
for name, elem := range expected { |
||||
if !reflect.DeepEqual(elem, received[name]) { |
||||
t.Errorf("field %v does not match expected, want %v, got %v", name, elem, received[name]) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func newMockLog(topics []common.Hash, txHash common.Hash) types.Log { |
||||
return types.Log{ |
||||
Address: common.HexToAddress("0x0"), |
||||
Topics: topics, |
||||
Data: hexutil.MustDecode(hexData), |
||||
BlockNumber: uint64(26), |
||||
TxHash: txHash, |
||||
TxIndex: 111, |
||||
BlockHash: common.BytesToHash([]byte{1, 2, 3, 4, 5}), |
||||
Index: 7, |
||||
Removed: false, |
||||
} |
||||
} |
||||
|
||||
func TestCall(t *testing.T) { |
||||
var method, methodWithArg = "something", "somethingArrrrg" |
||||
tests := []struct { |
||||
name, method string |
||||
opts *bind.CallOpts |
||||
mc bind.ContractCaller |
||||
results *[]interface{} |
||||
wantErr bool |
||||
wantErrExact error |
||||
}{{ |
||||
name: "ok not pending", |
||||
mc: &mockCaller{ |
||||
codeAtBytes: []byte{0}, |
||||
}, |
||||
method: method, |
||||
}, { |
||||
name: "ok pending", |
||||
mc: &mockPendingCaller{ |
||||
pendingCodeAtBytes: []byte{0}, |
||||
}, |
||||
opts: &bind.CallOpts{ |
||||
Pending: true, |
||||
}, |
||||
method: method, |
||||
}, { |
||||
name: "pack error, no method", |
||||
mc: new(mockCaller), |
||||
method: "else", |
||||
wantErr: true, |
||||
}, { |
||||
name: "interface error, pending but not a PendingContractCaller", |
||||
mc: new(mockCaller), |
||||
opts: &bind.CallOpts{ |
||||
Pending: true, |
||||
}, |
||||
method: method, |
||||
wantErrExact: bind.ErrNoPendingState, |
||||
}, { |
||||
name: "pending call canceled", |
||||
mc: &mockPendingCaller{ |
||||
pendingCallContractErr: context.DeadlineExceeded, |
||||
}, |
||||
opts: &bind.CallOpts{ |
||||
Pending: true, |
||||
}, |
||||
method: method, |
||||
wantErrExact: context.DeadlineExceeded, |
||||
}, { |
||||
name: "pending code at error", |
||||
mc: &mockPendingCaller{ |
||||
pendingCodeAtErr: errors.New(""), |
||||
}, |
||||
opts: &bind.CallOpts{ |
||||
Pending: true, |
||||
}, |
||||
method: method, |
||||
wantErr: true, |
||||
}, { |
||||
name: "no pending code at", |
||||
mc: new(mockPendingCaller), |
||||
opts: &bind.CallOpts{ |
||||
Pending: true, |
||||
}, |
||||
method: method, |
||||
wantErrExact: bind.ErrNoCode, |
||||
}, { |
||||
name: "call contract error", |
||||
mc: &mockCaller{ |
||||
callContractErr: context.DeadlineExceeded, |
||||
}, |
||||
method: method, |
||||
wantErrExact: context.DeadlineExceeded, |
||||
}, { |
||||
name: "code at error", |
||||
mc: &mockCaller{ |
||||
codeAtErr: errors.New(""), |
||||
}, |
||||
method: method, |
||||
wantErr: true, |
||||
}, { |
||||
name: "no code at", |
||||
mc: new(mockCaller), |
||||
method: method, |
||||
wantErrExact: bind.ErrNoCode, |
||||
}, { |
||||
name: "unpack error missing arg", |
||||
mc: &mockCaller{ |
||||
codeAtBytes: []byte{0}, |
||||
}, |
||||
method: methodWithArg, |
||||
wantErr: true, |
||||
}, { |
||||
name: "interface unpack error", |
||||
mc: &mockCaller{ |
||||
codeAtBytes: []byte{0}, |
||||
}, |
||||
method: method, |
||||
results: &[]interface{}{0}, |
||||
wantErr: true, |
||||
}} |
||||
for _, test := range tests { |
||||
bc := bind.NewBoundContract(common.HexToAddress("0x0"), abi.ABI{ |
||||
Methods: map[string]abi.Method{ |
||||
method: { |
||||
Name: method, |
||||
Outputs: abi.Arguments{}, |
||||
}, |
||||
methodWithArg: { |
||||
Name: methodWithArg, |
||||
Outputs: abi.Arguments{abi.Argument{}}, |
||||
}, |
||||
}, |
||||
}, test.mc, nil, nil) |
||||
err := bc.Call(test.opts, test.results, test.method) |
||||
if test.wantErr || test.wantErrExact != nil { |
||||
if err == nil { |
||||
t.Fatalf("%q expected error", test.name) |
||||
} |
||||
if test.wantErrExact != nil && !errors.Is(err, test.wantErrExact) { |
||||
t.Fatalf("%q expected error %q but got %q", test.name, test.wantErrExact, err) |
||||
} |
||||
continue |
||||
} |
||||
if err != nil { |
||||
t.Fatalf("%q unexpected error: %v", test.name, err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestCrashers contains some strings which previously caused the abi codec to crash.
|
||||
func TestCrashers(t *testing.T) { |
||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"_1"}]}]}]`)) |
||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"&"}]}]}]`)) |
||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"----"}]}]}]`)) |
||||
abi.JSON(strings.NewReader(`[{"inputs":[{"type":"tuple[]","components":[{"type":"bool","name":"foo.Bar"}]}]}]`)) |
||||
} |
@ -1,649 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Package bind generates Ethereum contract Go bindings.
|
||||
//
|
||||
// Detailed usage document and tutorial available on the go-ethereum Wiki page:
|
||||
// https://github.com/ethereum/go-ethereum/wiki/Native-DApps:-Go-bindings-to-Ethereum-contracts
|
||||
package bind |
||||
|
||||
import ( |
||||
"bytes" |
||||
"errors" |
||||
"fmt" |
||||
"go/format" |
||||
"regexp" |
||||
"strings" |
||||
"text/template" |
||||
"unicode" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
) |
||||
|
||||
// Lang is a target programming language selector to generate bindings for.
|
||||
type Lang int |
||||
|
||||
const ( |
||||
LangGo Lang = iota |
||||
LangJava |
||||
LangObjC |
||||
) |
||||
|
||||
func isKeyWord(arg string) bool { |
||||
switch arg { |
||||
case "break": |
||||
case "case": |
||||
case "chan": |
||||
case "const": |
||||
case "continue": |
||||
case "default": |
||||
case "defer": |
||||
case "else": |
||||
case "fallthrough": |
||||
case "for": |
||||
case "func": |
||||
case "go": |
||||
case "goto": |
||||
case "if": |
||||
case "import": |
||||
case "interface": |
||||
case "iota": |
||||
case "map": |
||||
case "make": |
||||
case "new": |
||||
case "package": |
||||
case "range": |
||||
case "return": |
||||
case "select": |
||||
case "struct": |
||||
case "switch": |
||||
case "type": |
||||
case "var": |
||||
default: |
||||
return false |
||||
} |
||||
|
||||
return true |
||||
} |
||||
|
||||
// Bind generates a Go wrapper around a contract ABI. This wrapper isn't meant
|
||||
// to be used as is in client code, but rather as an intermediate struct which
|
||||
// enforces compile time type safety and naming convention opposed to having to
|
||||
// manually maintain hard coded strings that break on runtime.
|
||||
func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string) (string, error) { |
||||
var ( |
||||
// contracts is the map of each individual contract requested binding
|
||||
contracts = make(map[string]*tmplContract) |
||||
|
||||
// structs is the map of all redeclared structs shared by passed contracts.
|
||||
structs = make(map[string]*tmplStruct) |
||||
|
||||
// isLib is the map used to flag each encountered library as such
|
||||
isLib = make(map[string]struct{}) |
||||
) |
||||
for i := 0; i < len(types); i++ { |
||||
// Parse the actual ABI to generate the binding for
|
||||
evmABI, err := abi.JSON(strings.NewReader(abis[i])) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
// Strip any whitespace from the JSON ABI
|
||||
strippedABI := strings.Map(func(r rune) rune { |
||||
if unicode.IsSpace(r) { |
||||
return -1 |
||||
} |
||||
return r |
||||
}, abis[i]) |
||||
|
||||
// Extract the call and transact methods; events, struct definitions; and sort them alphabetically
|
||||
var ( |
||||
calls = make(map[string]*tmplMethod) |
||||
transacts = make(map[string]*tmplMethod) |
||||
events = make(map[string]*tmplEvent) |
||||
fallback *tmplMethod |
||||
receive *tmplMethod |
||||
|
||||
// identifiers are used to detect duplicated identifiers of functions
|
||||
// and events. For all calls, transacts and events, abigen will generate
|
||||
// corresponding bindings. However we have to ensure there is no
|
||||
// identifier collisions in the bindings of these categories.
|
||||
callIdentifiers = make(map[string]bool) |
||||
transactIdentifiers = make(map[string]bool) |
||||
eventIdentifiers = make(map[string]bool) |
||||
) |
||||
|
||||
for _, input := range evmABI.Constructor.Inputs { |
||||
if hasStruct(input.Type) { |
||||
bindStructType[lang](input.Type, structs) |
||||
} |
||||
} |
||||
|
||||
for _, original := range evmABI.Methods { |
||||
// Normalize the method for capital cases and non-anonymous inputs/outputs
|
||||
normalized := original |
||||
normalizedName := methodNormalizer[lang](alias(aliases, original.Name)) |
||||
|
||||
// Ensure there is no duplicated identifier
|
||||
var identifiers = callIdentifiers |
||||
if !original.IsConstant() { |
||||
identifiers = transactIdentifiers |
||||
} |
||||
if identifiers[normalizedName] { |
||||
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName) |
||||
} |
||||
identifiers[normalizedName] = true |
||||
|
||||
normalized.Name = normalizedName |
||||
normalized.Inputs = make([]abi.Argument, len(original.Inputs)) |
||||
copy(normalized.Inputs, original.Inputs) |
||||
for j, input := range normalized.Inputs { |
||||
if input.Name == "" || isKeyWord(input.Name) { |
||||
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j) |
||||
} |
||||
if hasStruct(input.Type) { |
||||
bindStructType[lang](input.Type, structs) |
||||
} |
||||
} |
||||
normalized.Outputs = make([]abi.Argument, len(original.Outputs)) |
||||
copy(normalized.Outputs, original.Outputs) |
||||
for j, output := range normalized.Outputs { |
||||
if output.Name != "" { |
||||
normalized.Outputs[j].Name = capitalise(output.Name) |
||||
} |
||||
if hasStruct(output.Type) { |
||||
bindStructType[lang](output.Type, structs) |
||||
} |
||||
} |
||||
// Append the methods to the call or transact lists
|
||||
if original.IsConstant() { |
||||
calls[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)} |
||||
} else { |
||||
transacts[original.Name] = &tmplMethod{Original: original, Normalized: normalized, Structured: structured(original.Outputs)} |
||||
} |
||||
} |
||||
for _, original := range evmABI.Events { |
||||
// Skip anonymous events as they don't support explicit filtering
|
||||
if original.Anonymous { |
||||
continue |
||||
} |
||||
// Normalize the event for capital cases and non-anonymous outputs
|
||||
normalized := original |
||||
|
||||
// Ensure there is no duplicated identifier
|
||||
normalizedName := methodNormalizer[lang](alias(aliases, original.Name)) |
||||
if eventIdentifiers[normalizedName] { |
||||
return "", fmt.Errorf("duplicated identifier \"%s\"(normalized \"%s\"), use --alias for renaming", original.Name, normalizedName) |
||||
} |
||||
eventIdentifiers[normalizedName] = true |
||||
normalized.Name = normalizedName |
||||
|
||||
used := make(map[string]bool) |
||||
normalized.Inputs = make([]abi.Argument, len(original.Inputs)) |
||||
copy(normalized.Inputs, original.Inputs) |
||||
for j, input := range normalized.Inputs { |
||||
if input.Name == "" || isKeyWord(input.Name) { |
||||
normalized.Inputs[j].Name = fmt.Sprintf("arg%d", j) |
||||
} |
||||
// Event is a bit special, we need to define event struct in binding,
|
||||
// ensure there is no camel-case-style name conflict.
|
||||
for index := 0; ; index++ { |
||||
if !used[capitalise(normalized.Inputs[j].Name)] { |
||||
used[capitalise(normalized.Inputs[j].Name)] = true |
||||
break |
||||
} |
||||
normalized.Inputs[j].Name = fmt.Sprintf("%s%d", normalized.Inputs[j].Name, index) |
||||
} |
||||
if hasStruct(input.Type) { |
||||
bindStructType[lang](input.Type, structs) |
||||
} |
||||
} |
||||
// Append the event to the accumulator list
|
||||
events[original.Name] = &tmplEvent{Original: original, Normalized: normalized} |
||||
} |
||||
// Add two special fallback functions if they exist
|
||||
if evmABI.HasFallback() { |
||||
fallback = &tmplMethod{Original: evmABI.Fallback} |
||||
} |
||||
if evmABI.HasReceive() { |
||||
receive = &tmplMethod{Original: evmABI.Receive} |
||||
} |
||||
// There is no easy way to pass arbitrary java objects to the Go side.
|
||||
if len(structs) > 0 && lang == LangJava { |
||||
return "", errors.New("java binding for tuple arguments is not supported yet") |
||||
} |
||||
|
||||
contracts[types[i]] = &tmplContract{ |
||||
Type: capitalise(types[i]), |
||||
InputABI: strings.ReplaceAll(strippedABI, "\"", "\\\""), |
||||
InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"), |
||||
Constructor: evmABI.Constructor, |
||||
Calls: calls, |
||||
Transacts: transacts, |
||||
Fallback: fallback, |
||||
Receive: receive, |
||||
Events: events, |
||||
Libraries: make(map[string]string), |
||||
} |
||||
// Function 4-byte signatures are stored in the same sequence
|
||||
// as types, if available.
|
||||
if len(fsigs) > i { |
||||
contracts[types[i]].FuncSigs = fsigs[i] |
||||
} |
||||
// Parse library references.
|
||||
for pattern, name := range libs { |
||||
matched, err := regexp.Match("__\\$"+pattern+"\\$__", []byte(contracts[types[i]].InputBin)) |
||||
if err != nil { |
||||
log.Error("Could not search for pattern", "pattern", pattern, "contract", contracts[types[i]], "err", err) |
||||
} |
||||
if matched { |
||||
contracts[types[i]].Libraries[pattern] = name |
||||
// keep track that this type is a library
|
||||
if _, ok := isLib[name]; !ok { |
||||
isLib[name] = struct{}{} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
// Check if that type has already been identified as a library
|
||||
for i := 0; i < len(types); i++ { |
||||
_, ok := isLib[types[i]] |
||||
contracts[types[i]].Library = ok |
||||
} |
||||
// Generate the contract template data content and render it
|
||||
data := &tmplData{ |
||||
Package: pkg, |
||||
Contracts: contracts, |
||||
Libraries: libs, |
||||
Structs: structs, |
||||
} |
||||
buffer := new(bytes.Buffer) |
||||
|
||||
funcs := map[string]interface{}{ |
||||
"bindtype": bindType[lang], |
||||
"bindtopictype": bindTopicType[lang], |
||||
"namedtype": namedType[lang], |
||||
"capitalise": capitalise, |
||||
"decapitalise": decapitalise, |
||||
} |
||||
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tmplSource[lang])) |
||||
if err := tmpl.Execute(buffer, data); err != nil { |
||||
return "", err |
||||
} |
||||
// For Go bindings pass the code through gofmt to clean it up
|
||||
if lang == LangGo { |
||||
code, err := format.Source(buffer.Bytes()) |
||||
if err != nil { |
||||
return "", fmt.Errorf("%v\n%s", err, buffer) |
||||
} |
||||
return string(code), nil |
||||
} |
||||
// For all others just return as is for now
|
||||
return buffer.String(), nil |
||||
} |
||||
|
||||
// bindType is a set of type binders that convert Solidity types to some supported
|
||||
// programming language types.
|
||||
var bindType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{ |
||||
LangGo: bindTypeGo, |
||||
LangJava: bindTypeJava, |
||||
} |
||||
|
||||
// bindBasicTypeGo converts basic solidity types(except array, slice and tuple) to Go ones.
|
||||
func bindBasicTypeGo(kind abi.Type) string { |
||||
switch kind.T { |
||||
case abi.AddressTy: |
||||
return "common.Address" |
||||
case abi.IntTy, abi.UintTy: |
||||
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String()) |
||||
switch parts[2] { |
||||
case "8", "16", "32", "64": |
||||
return fmt.Sprintf("%sint%s", parts[1], parts[2]) |
||||
} |
||||
return "*big.Int" |
||||
case abi.FixedBytesTy: |
||||
return fmt.Sprintf("[%d]byte", kind.Size) |
||||
case abi.BytesTy: |
||||
return "[]byte" |
||||
case abi.FunctionTy: |
||||
return "[24]byte" |
||||
default: |
||||
// string, bool types
|
||||
return kind.String() |
||||
} |
||||
} |
||||
|
||||
// bindTypeGo converts solidity types to Go ones. Since there is no clear mapping
|
||||
// from all Solidity types to Go ones (e.g. uint17), those that cannot be exactly
|
||||
// mapped will use an upscaled type (e.g. BigDecimal).
|
||||
func bindTypeGo(kind abi.Type, structs map[string]*tmplStruct) string { |
||||
switch kind.T { |
||||
case abi.TupleTy: |
||||
return structs[kind.TupleRawName+kind.String()].Name |
||||
case abi.ArrayTy: |
||||
return fmt.Sprintf("[%d]", kind.Size) + bindTypeGo(*kind.Elem, structs) |
||||
case abi.SliceTy: |
||||
return "[]" + bindTypeGo(*kind.Elem, structs) |
||||
default: |
||||
return bindBasicTypeGo(kind) |
||||
} |
||||
} |
||||
|
||||
// bindBasicTypeJava converts basic solidity types(except array, slice and tuple) to Java ones.
|
||||
func bindBasicTypeJava(kind abi.Type) string { |
||||
switch kind.T { |
||||
case abi.AddressTy: |
||||
return "Address" |
||||
case abi.IntTy, abi.UintTy: |
||||
// Note that uint and int (without digits) are also matched,
|
||||
// these are size 256, and will translate to BigInt (the default).
|
||||
parts := regexp.MustCompile(`(u)?int([0-9]*)`).FindStringSubmatch(kind.String()) |
||||
if len(parts) != 3 { |
||||
return kind.String() |
||||
} |
||||
// All unsigned integers should be translated to BigInt since gomobile doesn't
|
||||
// support them.
|
||||
if parts[1] == "u" { |
||||
return "BigInt" |
||||
} |
||||
|
||||
namedSize := map[string]string{ |
||||
"8": "byte", |
||||
"16": "short", |
||||
"32": "int", |
||||
"64": "long", |
||||
}[parts[2]] |
||||
|
||||
// default to BigInt
|
||||
if namedSize == "" { |
||||
namedSize = "BigInt" |
||||
} |
||||
return namedSize |
||||
case abi.FixedBytesTy, abi.BytesTy: |
||||
return "byte[]" |
||||
case abi.BoolTy: |
||||
return "boolean" |
||||
case abi.StringTy: |
||||
return "String" |
||||
case abi.FunctionTy: |
||||
return "byte[24]" |
||||
default: |
||||
return kind.String() |
||||
} |
||||
} |
||||
|
||||
// pluralizeJavaType explicitly converts multidimensional types to predefined
|
||||
// types in go side.
|
||||
func pluralizeJavaType(typ string) string { |
||||
switch typ { |
||||
case "boolean": |
||||
return "Bools" |
||||
case "String": |
||||
return "Strings" |
||||
case "Address": |
||||
return "Addresses" |
||||
case "byte[]": |
||||
return "Binaries" |
||||
case "BigInt": |
||||
return "BigInts" |
||||
} |
||||
return typ + "[]" |
||||
} |
||||
|
||||
// bindTypeJava converts a Solidity type to a Java one. Since there is no clear mapping
|
||||
// from all Solidity types to Java ones (e.g. uint17), those that cannot be exactly
|
||||
// mapped will use an upscaled type (e.g. BigDecimal).
|
||||
func bindTypeJava(kind abi.Type, structs map[string]*tmplStruct) string { |
||||
switch kind.T { |
||||
case abi.TupleTy: |
||||
return structs[kind.TupleRawName+kind.String()].Name |
||||
case abi.ArrayTy, abi.SliceTy: |
||||
return pluralizeJavaType(bindTypeJava(*kind.Elem, structs)) |
||||
default: |
||||
return bindBasicTypeJava(kind) |
||||
} |
||||
} |
||||
|
||||
// bindTopicType is a set of type binders that convert Solidity types to some
|
||||
// supported programming language topic types.
|
||||
var bindTopicType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{ |
||||
LangGo: bindTopicTypeGo, |
||||
LangJava: bindTopicTypeJava, |
||||
} |
||||
|
||||
// bindTopicTypeGo converts a Solidity topic type to a Go one. It is almost the same
|
||||
// functionality as for simple types, but dynamic types get converted to hashes.
|
||||
func bindTopicTypeGo(kind abi.Type, structs map[string]*tmplStruct) string { |
||||
bound := bindTypeGo(kind, structs) |
||||
|
||||
// todo(rjl493456442) according solidity documentation, indexed event
|
||||
// parameters that are not value types i.e. arrays and structs are not
|
||||
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||
//
|
||||
// We only convert stringS and bytes to hash, still need to deal with
|
||||
// array(both fixed-size and dynamic-size) and struct.
|
||||
if bound == "string" || bound == "[]byte" { |
||||
bound = "common.Hash" |
||||
} |
||||
return bound |
||||
} |
||||
|
||||
// bindTopicTypeJava converts a Solidity topic type to a Java one. It is almost the same
|
||||
// functionality as for simple types, but dynamic types get converted to hashes.
|
||||
func bindTopicTypeJava(kind abi.Type, structs map[string]*tmplStruct) string { |
||||
bound := bindTypeJava(kind, structs) |
||||
|
||||
// todo(rjl493456442) according solidity documentation, indexed event
|
||||
// parameters that are not value types i.e. arrays and structs are not
|
||||
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||
//
|
||||
// We only convert strings and bytes to hash, still need to deal with
|
||||
// array(both fixed-size and dynamic-size) and struct.
|
||||
if bound == "String" || bound == "byte[]" { |
||||
bound = "Hash" |
||||
} |
||||
return bound |
||||
} |
||||
|
||||
// bindStructType is a set of type binders that convert Solidity tuple types to some supported
|
||||
// programming language struct definition.
|
||||
var bindStructType = map[Lang]func(kind abi.Type, structs map[string]*tmplStruct) string{ |
||||
LangGo: bindStructTypeGo, |
||||
LangJava: bindStructTypeJava, |
||||
} |
||||
|
||||
// bindStructTypeGo converts a Solidity tuple type to a Go one and records the mapping
|
||||
// in the given map.
|
||||
// Notably, this function will resolve and record nested struct recursively.
|
||||
func bindStructTypeGo(kind abi.Type, structs map[string]*tmplStruct) string { |
||||
switch kind.T { |
||||
case abi.TupleTy: |
||||
// We compose a raw struct name and a canonical parameter expression
|
||||
// together here. The reason is before solidity v0.5.11, kind.TupleRawName
|
||||
// is empty, so we use canonical parameter expression to distinguish
|
||||
// different struct definition. From the consideration of backward
|
||||
// compatibility, we concat these two together so that if kind.TupleRawName
|
||||
// is not empty, it can have unique id.
|
||||
id := kind.TupleRawName + kind.String() |
||||
if s, exist := structs[id]; exist { |
||||
return s.Name |
||||
} |
||||
var ( |
||||
names = make(map[string]bool) |
||||
fields []*tmplField |
||||
) |
||||
for i, elem := range kind.TupleElems { |
||||
name := capitalise(kind.TupleRawNames[i]) |
||||
name = abi.ResolveNameConflict(name, func(s string) bool { return names[s] }) |
||||
names[name] = true |
||||
fields = append(fields, &tmplField{Type: bindStructTypeGo(*elem, structs), Name: name, SolKind: *elem}) |
||||
} |
||||
name := kind.TupleRawName |
||||
if name == "" { |
||||
name = fmt.Sprintf("Struct%d", len(structs)) |
||||
} |
||||
name = capitalise(name) |
||||
|
||||
structs[id] = &tmplStruct{ |
||||
Name: name, |
||||
Fields: fields, |
||||
} |
||||
return name |
||||
case abi.ArrayTy: |
||||
return fmt.Sprintf("[%d]", kind.Size) + bindStructTypeGo(*kind.Elem, structs) |
||||
case abi.SliceTy: |
||||
return "[]" + bindStructTypeGo(*kind.Elem, structs) |
||||
default: |
||||
return bindBasicTypeGo(kind) |
||||
} |
||||
} |
||||
|
||||
// bindStructTypeJava converts a Solidity tuple type to a Java one and records the mapping
|
||||
// in the given map.
|
||||
// Notably, this function will resolve and record nested struct recursively.
|
||||
func bindStructTypeJava(kind abi.Type, structs map[string]*tmplStruct) string { |
||||
switch kind.T { |
||||
case abi.TupleTy: |
||||
// We compose a raw struct name and a canonical parameter expression
|
||||
// together here. The reason is before solidity v0.5.11, kind.TupleRawName
|
||||
// is empty, so we use canonical parameter expression to distinguish
|
||||
// different struct definition. From the consideration of backward
|
||||
// compatibility, we concat these two together so that if kind.TupleRawName
|
||||
// is not empty, it can have unique id.
|
||||
id := kind.TupleRawName + kind.String() |
||||
if s, exist := structs[id]; exist { |
||||
return s.Name |
||||
} |
||||
var fields []*tmplField |
||||
for i, elem := range kind.TupleElems { |
||||
field := bindStructTypeJava(*elem, structs) |
||||
fields = append(fields, &tmplField{Type: field, Name: decapitalise(kind.TupleRawNames[i]), SolKind: *elem}) |
||||
} |
||||
name := kind.TupleRawName |
||||
if name == "" { |
||||
name = fmt.Sprintf("Class%d", len(structs)) |
||||
} |
||||
structs[id] = &tmplStruct{ |
||||
Name: name, |
||||
Fields: fields, |
||||
} |
||||
return name |
||||
case abi.ArrayTy, abi.SliceTy: |
||||
return pluralizeJavaType(bindStructTypeJava(*kind.Elem, structs)) |
||||
default: |
||||
return bindBasicTypeJava(kind) |
||||
} |
||||
} |
||||
|
||||
// namedType is a set of functions that transform language specific types to
|
||||
// named versions that may be used inside method names.
|
||||
var namedType = map[Lang]func(string, abi.Type) string{ |
||||
LangGo: func(string, abi.Type) string { panic("this shouldn't be needed") }, |
||||
LangJava: namedTypeJava, |
||||
} |
||||
|
||||
// namedTypeJava converts some primitive data types to named variants that can
|
||||
// be used as parts of method names.
|
||||
func namedTypeJava(javaKind string, solKind abi.Type) string { |
||||
switch javaKind { |
||||
case "byte[]": |
||||
return "Binary" |
||||
case "boolean": |
||||
return "Bool" |
||||
default: |
||||
parts := regexp.MustCompile(`(u)?int([0-9]*)(\[[0-9]*\])?`).FindStringSubmatch(solKind.String()) |
||||
if len(parts) != 4 { |
||||
return javaKind |
||||
} |
||||
switch parts[2] { |
||||
case "8", "16", "32", "64": |
||||
if parts[3] == "" { |
||||
return capitalise(fmt.Sprintf("%sint%s", parts[1], parts[2])) |
||||
} |
||||
return capitalise(fmt.Sprintf("%sint%ss", parts[1], parts[2])) |
||||
|
||||
default: |
||||
return javaKind |
||||
} |
||||
} |
||||
} |
||||
|
||||
// alias returns an alias of the given string based on the aliasing rules
|
||||
// or returns itself if no rule is matched.
|
||||
func alias(aliases map[string]string, n string) string { |
||||
if alias, exist := aliases[n]; exist { |
||||
return alias |
||||
} |
||||
return n |
||||
} |
||||
|
||||
// methodNormalizer is a name transformer that modifies Solidity method names to
|
||||
// conform to target language naming conventions.
|
||||
var methodNormalizer = map[Lang]func(string) string{ |
||||
LangGo: abi.ToCamelCase, |
||||
LangJava: decapitalise, |
||||
} |
||||
|
||||
// capitalise makes a camel-case string which starts with an upper case character.
|
||||
var capitalise = abi.ToCamelCase |
||||
|
||||
// decapitalise makes a camel-case string which starts with a lower case character.
|
||||
func decapitalise(input string) string { |
||||
if len(input) == 0 { |
||||
return input |
||||
} |
||||
|
||||
goForm := abi.ToCamelCase(input) |
||||
return strings.ToLower(goForm[:1]) + goForm[1:] |
||||
} |
||||
|
||||
// structured checks whether a list of ABI data types has enough information to
|
||||
// operate through a proper Go struct or if flat returns are needed.
|
||||
func structured(args abi.Arguments) bool { |
||||
if len(args) < 2 { |
||||
return false |
||||
} |
||||
exists := make(map[string]bool) |
||||
for _, out := range args { |
||||
// If the name is anonymous, we can't organize into a struct
|
||||
if out.Name == "" { |
||||
return false |
||||
} |
||||
// If the field name is empty when normalized or collides (var, Var, _var, _Var),
|
||||
// we can't organize into a struct
|
||||
field := capitalise(out.Name) |
||||
if field == "" || exists[field] { |
||||
return false |
||||
} |
||||
exists[field] = true |
||||
} |
||||
return true |
||||
} |
||||
|
||||
// hasStruct returns an indicator whether the given type is struct, struct slice
|
||||
// or struct array.
|
||||
func hasStruct(t abi.Type) bool { |
||||
switch t.T { |
||||
case abi.SliceTy: |
||||
return hasStruct(*t.Elem) |
||||
case abi.ArrayTy: |
||||
return hasStruct(*t.Elem) |
||||
case abi.TupleTy: |
||||
return true |
||||
default: |
||||
return false |
||||
} |
||||
} |
File diff suppressed because one or more lines are too long
@ -1,709 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind |
||||
|
||||
import "github.com/ethereum/go-ethereum/accounts/abi" |
||||
|
||||
// tmplData is the data structure required to fill the binding template.
|
||||
type tmplData struct { |
||||
Package string // Name of the package to place the generated file in
|
||||
Contracts map[string]*tmplContract // List of contracts to generate into this file
|
||||
Libraries map[string]string // Map the bytecode's link pattern to the library name
|
||||
Structs map[string]*tmplStruct // Contract struct type definitions
|
||||
} |
||||
|
||||
// tmplContract contains the data needed to generate an individual contract binding.
|
||||
type tmplContract struct { |
||||
Type string // Type name of the main contract binding
|
||||
InputABI string // JSON ABI used as the input to generate the binding from
|
||||
InputBin string // Optional EVM bytecode used to generate deploy code from
|
||||
FuncSigs map[string]string // Optional map: string signature -> 4-byte signature
|
||||
Constructor abi.Method // Contract constructor for deploy parametrization
|
||||
Calls map[string]*tmplMethod // Contract calls that only read state data
|
||||
Transacts map[string]*tmplMethod // Contract calls that write state data
|
||||
Fallback *tmplMethod // Additional special fallback function
|
||||
Receive *tmplMethod // Additional special receive function
|
||||
Events map[string]*tmplEvent // Contract events accessors
|
||||
Libraries map[string]string // Same as tmplData, but filtered to only keep what the contract needs
|
||||
Library bool // Indicator whether the contract is a library
|
||||
} |
||||
|
||||
// tmplMethod is a wrapper around an abi.Method that contains a few preprocessed
|
||||
// and cached data fields.
|
||||
type tmplMethod struct { |
||||
Original abi.Method // Original method as parsed by the abi package
|
||||
Normalized abi.Method // Normalized version of the parsed method (capitalized names, non-anonymous args/returns)
|
||||
Structured bool // Whether the returns should be accumulated into a struct
|
||||
} |
||||
|
||||
// tmplEvent is a wrapper around an abi.Event that contains a few preprocessed
|
||||
// and cached data fields.
|
||||
type tmplEvent struct { |
||||
Original abi.Event // Original event as parsed by the abi package
|
||||
Normalized abi.Event // Normalized version of the parsed fields
|
||||
} |
||||
|
||||
// tmplField is a wrapper around a struct field with binding language
|
||||
// struct type definition and relative filed name.
|
||||
type tmplField struct { |
||||
Type string // Field type representation depends on target binding language
|
||||
Name string // Field name converted from the raw user-defined field name
|
||||
SolKind abi.Type // Raw abi type information
|
||||
} |
||||
|
||||
// tmplStruct is a wrapper around an abi.tuple and contains an auto-generated
|
||||
// struct name.
|
||||
type tmplStruct struct { |
||||
Name string // Auto-generated struct name(before solidity v0.5.11) or raw name.
|
||||
Fields []*tmplField // Struct fields definition depends on the binding language.
|
||||
} |
||||
|
||||
// tmplSource is language to template mapping containing all the supported
|
||||
// programming languages the package can generate to.
|
||||
var tmplSource = map[Lang]string{ |
||||
LangGo: tmplSourceGo, |
||||
LangJava: tmplSourceJava, |
||||
} |
||||
|
||||
// tmplSourceGo is the Go source template that the generated Go contract binding
|
||||
// is based on.
|
||||
const tmplSourceGo = ` |
||||
// Code generated - DO NOT EDIT.
|
||||
// This file is a generated binding and any manual changes will be lost.
|
||||
|
||||
package {{.Package}} |
||||
|
||||
import ( |
||||
"math/big" |
||||
"strings" |
||||
"errors" |
||||
|
||||
ethereum "github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/accounts/abi" |
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
) |
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var ( |
||||
_ = errors.New |
||||
_ = big.NewInt |
||||
_ = strings.NewReader |
||||
_ = ethereum.NotFound |
||||
_ = bind.Bind |
||||
_ = common.Big1 |
||||
_ = types.BloomLookup |
||||
_ = event.NewSubscription |
||||
_ = abi.ConvertType |
||||
) |
||||
|
||||
{{$structs := .Structs}} |
||||
{{range $structs}} |
||||
// {{.Name}} is an auto generated low-level Go binding around an user-defined struct.
|
||||
type {{.Name}} struct { |
||||
{{range $field := .Fields}} |
||||
{{$field.Name}} {{$field.Type}}{{end}} |
||||
} |
||||
{{end}} |
||||
|
||||
{{range $contract := .Contracts}} |
||||
// {{.Type}}MetaData contains all meta data concerning the {{.Type}} contract.
|
||||
var {{.Type}}MetaData = &bind.MetaData{ |
||||
ABI: "{{.InputABI}}", |
||||
{{if $contract.FuncSigs -}} |
||||
Sigs: map[string]string{ |
||||
{{range $strsig, $binsig := .FuncSigs}}"{{$binsig}}": "{{$strsig}}", |
||||
{{end}} |
||||
}, |
||||
{{end -}} |
||||
{{if .InputBin -}} |
||||
Bin: "0x{{.InputBin}}", |
||||
{{end}} |
||||
} |
||||
// {{.Type}}ABI is the input ABI used to generate the binding from.
|
||||
// Deprecated: Use {{.Type}}MetaData.ABI instead.
|
||||
var {{.Type}}ABI = {{.Type}}MetaData.ABI |
||||
|
||||
{{if $contract.FuncSigs}} |
||||
// Deprecated: Use {{.Type}}MetaData.Sigs instead.
|
||||
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
|
||||
var {{.Type}}FuncSigs = {{.Type}}MetaData.Sigs |
||||
{{end}} |
||||
|
||||
{{if .InputBin}} |
||||
// {{.Type}}Bin is the compiled bytecode used for deploying new contracts.
|
||||
// Deprecated: Use {{.Type}}MetaData.Bin instead.
|
||||
var {{.Type}}Bin = {{.Type}}MetaData.Bin |
||||
|
||||
// Deploy{{.Type}} deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
|
||||
func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Transaction, *{{.Type}}, error) { |
||||
parsed, err := {{.Type}}MetaData.GetAbi() |
||||
if err != nil { |
||||
return common.Address{}, nil, nil, err |
||||
} |
||||
if parsed == nil { |
||||
return common.Address{}, nil, nil, errors.New("GetABI returned nil") |
||||
} |
||||
{{range $pattern, $name := .Libraries}} |
||||
{{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend) |
||||
{{$contract.Type}}Bin = strings.ReplaceAll({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:]) |
||||
{{end}} |
||||
address, tx, contract, err := bind.DeployContract(auth, *parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) |
||||
if err != nil { |
||||
return common.Address{}, nil, nil, err |
||||
} |
||||
return address, tx, &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil |
||||
} |
||||
{{end}} |
||||
|
||||
// {{.Type}} is an auto generated Go binding around an Ethereum contract.
|
||||
type {{.Type}} struct { |
||||
{{.Type}}Caller // Read-only binding to the contract
|
||||
{{.Type}}Transactor // Write-only binding to the contract
|
||||
{{.Type}}Filterer // Log filterer for contract events
|
||||
} |
||||
|
||||
// {{.Type}}Caller is an auto generated read-only Go binding around an Ethereum contract.
|
||||
type {{.Type}}Caller struct { |
||||
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
||||
} |
||||
|
||||
// {{.Type}}Transactor is an auto generated write-only Go binding around an Ethereum contract.
|
||||
type {{.Type}}Transactor struct { |
||||
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
||||
} |
||||
|
||||
// {{.Type}}Filterer is an auto generated log filtering Go binding around an Ethereum contract events.
|
||||
type {{.Type}}Filterer struct { |
||||
contract *bind.BoundContract // Generic contract wrapper for the low level calls
|
||||
} |
||||
|
||||
// {{.Type}}Session is an auto generated Go binding around an Ethereum contract,
|
||||
// with pre-set call and transact options.
|
||||
type {{.Type}}Session struct { |
||||
Contract *{{.Type}} // Generic contract binding to set the session for
|
||||
CallOpts bind.CallOpts // Call options to use throughout this session
|
||||
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
|
||||
} |
||||
|
||||
// {{.Type}}CallerSession is an auto generated read-only Go binding around an Ethereum contract,
|
||||
// with pre-set call options.
|
||||
type {{.Type}}CallerSession struct { |
||||
Contract *{{.Type}}Caller // Generic contract caller binding to set the session for
|
||||
CallOpts bind.CallOpts // Call options to use throughout this session
|
||||
} |
||||
|
||||
// {{.Type}}TransactorSession is an auto generated write-only Go binding around an Ethereum contract,
|
||||
// with pre-set transact options.
|
||||
type {{.Type}}TransactorSession struct { |
||||
Contract *{{.Type}}Transactor // Generic contract transactor binding to set the session for
|
||||
TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session
|
||||
} |
||||
|
||||
// {{.Type}}Raw is an auto generated low-level Go binding around an Ethereum contract.
|
||||
type {{.Type}}Raw struct { |
||||
Contract *{{.Type}} // Generic contract binding to access the raw methods on
|
||||
} |
||||
|
||||
// {{.Type}}CallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract.
|
||||
type {{.Type}}CallerRaw struct { |
||||
Contract *{{.Type}}Caller // Generic read-only contract binding to access the raw methods on
|
||||
} |
||||
|
||||
// {{.Type}}TransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract.
|
||||
type {{.Type}}TransactorRaw struct { |
||||
Contract *{{.Type}}Transactor // Generic write-only contract binding to access the raw methods on
|
||||
} |
||||
|
||||
// New{{.Type}} creates a new instance of {{.Type}}, bound to a specific deployed contract.
|
||||
func New{{.Type}}(address common.Address, backend bind.ContractBackend) (*{{.Type}}, error) { |
||||
contract, err := bind{{.Type}}(address, backend, backend, backend) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &{{.Type}}{ {{.Type}}Caller: {{.Type}}Caller{contract: contract}, {{.Type}}Transactor: {{.Type}}Transactor{contract: contract}, {{.Type}}Filterer: {{.Type}}Filterer{contract: contract} }, nil |
||||
} |
||||
|
||||
// New{{.Type}}Caller creates a new read-only instance of {{.Type}}, bound to a specific deployed contract.
|
||||
func New{{.Type}}Caller(address common.Address, caller bind.ContractCaller) (*{{.Type}}Caller, error) { |
||||
contract, err := bind{{.Type}}(address, caller, nil, nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &{{.Type}}Caller{contract: contract}, nil |
||||
} |
||||
|
||||
// New{{.Type}}Transactor creates a new write-only instance of {{.Type}}, bound to a specific deployed contract.
|
||||
func New{{.Type}}Transactor(address common.Address, transactor bind.ContractTransactor) (*{{.Type}}Transactor, error) { |
||||
contract, err := bind{{.Type}}(address, nil, transactor, nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &{{.Type}}Transactor{contract: contract}, nil |
||||
} |
||||
|
||||
// New{{.Type}}Filterer creates a new log filterer instance of {{.Type}}, bound to a specific deployed contract.
|
||||
func New{{.Type}}Filterer(address common.Address, filterer bind.ContractFilterer) (*{{.Type}}Filterer, error) { |
||||
contract, err := bind{{.Type}}(address, nil, nil, filterer) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &{{.Type}}Filterer{contract: contract}, nil |
||||
} |
||||
|
||||
// bind{{.Type}} binds a generic wrapper to an already deployed contract.
|
||||
func bind{{.Type}}(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { |
||||
parsed, err := {{.Type}}MetaData.GetAbi() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return bind.NewBoundContract(address, *parsed, caller, transactor, filterer), nil |
||||
} |
||||
|
||||
// Call invokes the (constant) contract method with params as input values and
|
||||
// sets the output to result. The result type might be a single field for simple
|
||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||
// returns.
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { |
||||
return _{{$contract.Type}}.Contract.{{$contract.Type}}Caller.contract.Call(opts, result, method, params...) |
||||
} |
||||
|
||||
// Transfer initiates a plain transaction to move funds to the contract, calling
|
||||
// its default method if one is available.
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transfer(opts) |
||||
} |
||||
|
||||
// Transact invokes the (paid) contract method with params as input values.
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Raw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.{{$contract.Type}}Transactor.contract.Transact(opts, method, params...) |
||||
} |
||||
|
||||
// Call invokes the (constant) contract method with params as input values and
|
||||
// sets the output to result. The result type might be a single field for simple
|
||||
// returns, a slice of interfaces for anonymous returns and a struct for named
|
||||
// returns.
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}CallerRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { |
||||
return _{{$contract.Type}}.Contract.contract.Call(opts, result, method, params...) |
||||
} |
||||
|
||||
// Transfer initiates a plain transaction to move funds to the contract, calling
|
||||
// its default method if one is available.
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.contract.Transfer(opts) |
||||
} |
||||
|
||||
// Transact invokes the (paid) contract method with params as input values.
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.contract.Transact(opts, method, params...) |
||||
} |
||||
|
||||
{{range .Calls}} |
||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Caller) {{.Normalized.Name}}(opts *bind.CallOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} },{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}}{{end}} error) { |
||||
var out []interface{} |
||||
err := _{{$contract.Type}}.contract.Call(opts, &out, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}}) |
||||
{{if .Structured}} |
||||
outstruct := new(struct{ {{range .Normalized.Outputs}} {{.Name}} {{bindtype .Type $structs}}; {{end}} }) |
||||
if err != nil { |
||||
return *outstruct, err |
||||
} |
||||
{{range $i, $t := .Normalized.Outputs}}
|
||||
outstruct.{{.Name}} = *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}} |
||||
|
||||
return *outstruct, err |
||||
{{else}} |
||||
if err != nil { |
||||
return {{range $i, $_ := .Normalized.Outputs}}*new({{bindtype .Type $structs}}), {{end}} err |
||||
} |
||||
{{range $i, $t := .Normalized.Outputs}} |
||||
out{{$i}} := *abi.ConvertType(out[{{$i}}], new({{bindtype .Type $structs}})).(*{{bindtype .Type $structs}}){{end}} |
||||
|
||||
return {{range $i, $t := .Normalized.Outputs}}out{{$i}}, {{end}} err |
||||
{{end}} |
||||
} |
||||
|
||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) { |
||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}}) |
||||
} |
||||
|
||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}CallerSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) ({{if .Structured}}struct{ {{range .Normalized.Outputs}}{{.Name}} {{bindtype .Type $structs}};{{end}} }, {{else}} {{range .Normalized.Outputs}}{{bindtype .Type $structs}},{{end}} {{end}} error) { |
||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.CallOpts {{range .Normalized.Inputs}}, {{.Name}}{{end}}) |
||||
} |
||||
{{end}} |
||||
|
||||
{{range .Transacts}} |
||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) {{.Normalized.Name}}(opts *bind.TransactOpts {{range .Normalized.Inputs}}, {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.contract.Transact(opts, "{{.Original.Name}}" {{range .Normalized.Inputs}}, {{.Name}}{{end}}) |
||||
} |
||||
|
||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}}) |
||||
} |
||||
|
||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) {{.Normalized.Name}}({{range $i, $_ := .Normalized.Inputs}}{{if ne $i 0}},{{end}} {{.Name}} {{bindtype .Type $structs}} {{end}}) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.{{.Normalized.Name}}(&_{{$contract.Type}}.TransactOpts {{range $i, $_ := .Normalized.Inputs}}, {{.Name}}{{end}}) |
||||
} |
||||
{{end}} |
||||
|
||||
{{if .Fallback}}
|
||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||
//
|
||||
// Solidity: {{.Fallback.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Fallback(opts *bind.TransactOpts, calldata []byte) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.contract.RawTransact(opts, calldata) |
||||
} |
||||
|
||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||
//
|
||||
// Solidity: {{.Fallback.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) Fallback(calldata []byte) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata) |
||||
} |
||||
|
||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||
//
|
||||
// Solidity: {{.Fallback.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Fallback(calldata []byte) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.Fallback(&_{{$contract.Type}}.TransactOpts, calldata) |
||||
} |
||||
{{end}} |
||||
|
||||
{{if .Receive}}
|
||||
// Receive is a paid mutator transaction binding the contract receive function.
|
||||
//
|
||||
// Solidity: {{.Receive.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Transactor) Receive(opts *bind.TransactOpts) (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.contract.RawTransact(opts, nil) // calldata is disallowed for receive function
|
||||
} |
||||
|
||||
// Receive is a paid mutator transaction binding the contract receive function.
|
||||
//
|
||||
// Solidity: {{.Receive.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Session) Receive() (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts) |
||||
} |
||||
|
||||
// Receive is a paid mutator transaction binding the contract receive function.
|
||||
//
|
||||
// Solidity: {{.Receive.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}TransactorSession) Receive() (*types.Transaction, error) { |
||||
return _{{$contract.Type}}.Contract.Receive(&_{{$contract.Type}}.TransactOpts) |
||||
} |
||||
{{end}} |
||||
|
||||
{{range .Events}} |
||||
// {{$contract.Type}}{{.Normalized.Name}}Iterator is returned from Filter{{.Normalized.Name}} and is used to iterate over the raw logs and unpacked data for {{.Normalized.Name}} events raised by the {{$contract.Type}} contract.
|
||||
type {{$contract.Type}}{{.Normalized.Name}}Iterator struct { |
||||
Event *{{$contract.Type}}{{.Normalized.Name}} // Event containing the contract specifics and raw log
|
||||
|
||||
contract *bind.BoundContract // Generic contract to use for unpacking event data
|
||||
event string // Event name to use for unpacking event data
|
||||
|
||||
logs chan types.Log // Log channel receiving the found contract events
|
||||
sub ethereum.Subscription // Subscription for errors, completion and termination
|
||||
done bool // Whether the subscription completed delivering logs
|
||||
fail error // Occurred error to stop iteration
|
||||
} |
||||
// Next advances the iterator to the subsequent event, returning whether there
|
||||
// are any more events found. In case of a retrieval or parsing error, false is
|
||||
// returned and Error() can be queried for the exact failure.
|
||||
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Next() bool { |
||||
// If the iterator failed, stop iterating
|
||||
if (it.fail != nil) { |
||||
return false |
||||
} |
||||
// If the iterator completed, deliver directly whatever's available
|
||||
if (it.done) { |
||||
select { |
||||
case log := <-it.logs: |
||||
it.Event = new({{$contract.Type}}{{.Normalized.Name}}) |
||||
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { |
||||
it.fail = err |
||||
return false |
||||
} |
||||
it.Event.Raw = log |
||||
return true |
||||
|
||||
default: |
||||
return false |
||||
} |
||||
} |
||||
// Iterator still in progress, wait for either a data or an error event
|
||||
select { |
||||
case log := <-it.logs: |
||||
it.Event = new({{$contract.Type}}{{.Normalized.Name}}) |
||||
if err := it.contract.UnpackLog(it.Event, it.event, log); err != nil { |
||||
it.fail = err |
||||
return false |
||||
} |
||||
it.Event.Raw = log |
||||
return true |
||||
|
||||
case err := <-it.sub.Err(): |
||||
it.done = true |
||||
it.fail = err |
||||
return it.Next() |
||||
} |
||||
} |
||||
// Error returns any retrieval or parsing error occurred during filtering.
|
||||
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Error() error { |
||||
return it.fail |
||||
} |
||||
// Close terminates the iteration process, releasing any pending underlying
|
||||
// resources.
|
||||
func (it *{{$contract.Type}}{{.Normalized.Name}}Iterator) Close() error { |
||||
it.sub.Unsubscribe() |
||||
return nil |
||||
} |
||||
|
||||
// {{$contract.Type}}{{.Normalized.Name}} represents a {{.Normalized.Name}} event raised by the {{$contract.Type}} contract.
|
||||
type {{$contract.Type}}{{.Normalized.Name}} struct { {{range .Normalized.Inputs}} |
||||
{{capitalise .Name}} {{if .Indexed}}{{bindtopictype .Type $structs}}{{else}}{{bindtype .Type $structs}}{{end}}; {{end}} |
||||
Raw types.Log // Blockchain specific contextual infos
|
||||
} |
||||
|
||||
// Filter{{.Normalized.Name}} is a free log retrieval operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Filter{{.Normalized.Name}}(opts *bind.FilterOpts{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (*{{$contract.Type}}{{.Normalized.Name}}Iterator, error) { |
||||
{{range .Normalized.Inputs}} |
||||
{{if .Indexed}}var {{.Name}}Rule []interface{} |
||||
for _, {{.Name}}Item := range {{.Name}} { |
||||
{{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item) |
||||
}{{end}}{{end}} |
||||
|
||||
logs, sub, err := _{{$contract.Type}}.contract.FilterLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &{{$contract.Type}}{{.Normalized.Name}}Iterator{contract: _{{$contract.Type}}.contract, event: "{{.Original.Name}}", logs: logs, sub: sub}, nil |
||||
} |
||||
|
||||
// Watch{{.Normalized.Name}} is a free log subscription operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Watch{{.Normalized.Name}}(opts *bind.WatchOpts, sink chan<- *{{$contract.Type}}{{.Normalized.Name}}{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}} []{{bindtype .Type $structs}}{{end}}{{end}}) (event.Subscription, error) { |
||||
{{range .Normalized.Inputs}} |
||||
{{if .Indexed}}var {{.Name}}Rule []interface{} |
||||
for _, {{.Name}}Item := range {{.Name}} { |
||||
{{.Name}}Rule = append({{.Name}}Rule, {{.Name}}Item) |
||||
}{{end}}{{end}} |
||||
|
||||
logs, sub, err := _{{$contract.Type}}.contract.WatchLogs(opts, "{{.Original.Name}}"{{range .Normalized.Inputs}}{{if .Indexed}}, {{.Name}}Rule{{end}}{{end}}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return event.NewSubscription(func(quit <-chan struct{}) error { |
||||
defer sub.Unsubscribe() |
||||
for { |
||||
select { |
||||
case log := <-logs: |
||||
// New log arrived, parse the event and forward to the user
|
||||
event := new({{$contract.Type}}{{.Normalized.Name}}) |
||||
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil { |
||||
return err |
||||
} |
||||
event.Raw = log |
||||
|
||||
select { |
||||
case sink <- event: |
||||
case err := <-sub.Err(): |
||||
return err |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
case err := <-sub.Err(): |
||||
return err |
||||
case <-quit: |
||||
return nil |
||||
} |
||||
} |
||||
}), nil |
||||
} |
||||
|
||||
// Parse{{.Normalized.Name}} is a log parse operation binding the contract event 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
func (_{{$contract.Type}} *{{$contract.Type}}Filterer) Parse{{.Normalized.Name}}(log types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) { |
||||
event := new({{$contract.Type}}{{.Normalized.Name}}) |
||||
if err := _{{$contract.Type}}.contract.UnpackLog(event, "{{.Original.Name}}", log); err != nil { |
||||
return nil, err |
||||
} |
||||
event.Raw = log |
||||
return event, nil |
||||
} |
||||
|
||||
{{end}} |
||||
{{end}} |
||||
` |
||||
|
||||
// tmplSourceJava is the Java source template that the generated Java contract binding
|
||||
// is based on.
|
||||
const tmplSourceJava = ` |
||||
// This file is an automatically generated Java binding. Do not modify as any
|
||||
// change will likely be lost upon the next re-generation!
|
||||
|
||||
package {{.Package}}; |
||||
|
||||
import org.ethereum.geth.*; |
||||
import java.util.*; |
||||
|
||||
{{$structs := .Structs}} |
||||
{{range $contract := .Contracts}} |
||||
{{if not .Library}}public {{end}}class {{.Type}} { |
||||
// ABI is the input ABI used to generate the binding from.
|
||||
public final static String ABI = "{{.InputABI}}"; |
||||
{{if $contract.FuncSigs}} |
||||
// {{.Type}}FuncSigs maps the 4-byte function signature to its string representation.
|
||||
public final static Map<String, String> {{.Type}}FuncSigs; |
||||
static { |
||||
Hashtable<String, String> temp = new Hashtable<String, String>(); |
||||
{{range $strsig, $binsig := .FuncSigs}}temp.put("{{$binsig}}", "{{$strsig}}"); |
||||
{{end}} |
||||
{{.Type}}FuncSigs = Collections.unmodifiableMap(temp); |
||||
} |
||||
{{end}} |
||||
{{if .InputBin}} |
||||
// BYTECODE is the compiled bytecode used for deploying new contracts.
|
||||
public final static String BYTECODE = "0x{{.InputBin}}"; |
||||
|
||||
// deploy deploys a new Ethereum contract, binding an instance of {{.Type}} to it.
|
||||
public static {{.Type}} deploy(TransactOpts auth, EthereumClient client{{range .Constructor.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception { |
||||
Interfaces args = Geth.newInterfaces({{(len .Constructor.Inputs)}}); |
||||
String bytecode = BYTECODE; |
||||
{{if .Libraries}} |
||||
|
||||
// "link" contract to dependent libraries by deploying them first.
|
||||
{{range $pattern, $name := .Libraries}} |
||||
{{capitalise $name}} {{decapitalise $name}}Inst = {{capitalise $name}}.deploy(auth, client); |
||||
bytecode = bytecode.replace("__${{$pattern}}$__", {{decapitalise $name}}Inst.Address.getHex().substring(2)); |
||||
{{end}} |
||||
{{end}} |
||||
{{range $index, $element := .Constructor.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); |
||||
{{end}} |
||||
return new {{.Type}}(Geth.deployContract(auth, ABI, Geth.decodeFromHex(bytecode), client, args)); |
||||
} |
||||
|
||||
// Internal constructor used by contract deployment.
|
||||
private {{.Type}}(BoundContract deployment) { |
||||
this.Address = deployment.getAddress(); |
||||
this.Deployer = deployment.getDeployer(); |
||||
this.Contract = deployment; |
||||
} |
||||
{{end}} |
||||
|
||||
// Ethereum address where this contract is located at.
|
||||
public final Address Address; |
||||
|
||||
// Ethereum transaction in which this contract was deployed (if known!).
|
||||
public final Transaction Deployer; |
||||
|
||||
// Contract instance bound to a blockchain address.
|
||||
private final BoundContract Contract; |
||||
|
||||
// Creates a new instance of {{.Type}}, bound to a specific deployed contract.
|
||||
public {{.Type}}(Address address, EthereumClient client) throws Exception { |
||||
this(Geth.bindContract(address, ABI, client)); |
||||
} |
||||
|
||||
{{range .Calls}} |
||||
{{if gt (len .Normalized.Outputs) 1}} |
||||
// {{capitalise .Normalized.Name}}Results is the output of a call to {{.Normalized.Name}}.
|
||||
public class {{capitalise .Normalized.Name}}Results { |
||||
{{range $index, $item := .Normalized.Outputs}}public {{bindtype .Type $structs}} {{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}}; |
||||
{{end}} |
||||
} |
||||
{{end}} |
||||
|
||||
// {{.Normalized.Name}} is a free data retrieval call binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
public {{if gt (len .Normalized.Outputs) 1}}{{capitalise .Normalized.Name}}Results{{else if eq (len .Normalized.Outputs) 0}}void{{else}}{{range .Normalized.Outputs}}{{bindtype .Type $structs}}{{end}}{{end}} {{.Normalized.Name}}(CallOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception { |
||||
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); |
||||
{{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); |
||||
{{end}} |
||||
|
||||
Interfaces results = Geth.newInterfaces({{(len .Normalized.Outputs)}}); |
||||
{{range $index, $item := .Normalized.Outputs}}Interface result{{$index}} = Geth.newInterface(); result{{$index}}.setDefault{{namedtype (bindtype .Type $structs) .Type}}(); results.set({{$index}}, result{{$index}}); |
||||
{{end}} |
||||
|
||||
if (opts == null) { |
||||
opts = Geth.newCallOpts(); |
||||
} |
||||
this.Contract.call(opts, results, "{{.Original.Name}}", args); |
||||
{{if gt (len .Normalized.Outputs) 1}} |
||||
{{capitalise .Normalized.Name}}Results result = new {{capitalise .Normalized.Name}}Results(); |
||||
{{range $index, $item := .Normalized.Outputs}}result.{{if ne .Name ""}}{{.Name}}{{else}}Return{{$index}}{{end}} = results.get({{$index}}).get{{namedtype (bindtype .Type $structs) .Type}}(); |
||||
{{end}} |
||||
return result; |
||||
{{else}}{{range .Normalized.Outputs}}return results.get(0).get{{namedtype (bindtype .Type $structs) .Type}}();{{end}} |
||||
{{end}} |
||||
} |
||||
{{end}} |
||||
|
||||
{{range .Transacts}} |
||||
// {{.Normalized.Name}} is a paid mutator transaction binding the contract method 0x{{printf "%x" .Original.ID}}.
|
||||
//
|
||||
// Solidity: {{.Original.String}}
|
||||
public Transaction {{.Normalized.Name}}(TransactOpts opts{{range .Normalized.Inputs}}, {{bindtype .Type $structs}} {{.Name}}{{end}}) throws Exception { |
||||
Interfaces args = Geth.newInterfaces({{(len .Normalized.Inputs)}}); |
||||
{{range $index, $item := .Normalized.Inputs}}Interface arg{{$index}} = Geth.newInterface();arg{{$index}}.set{{namedtype (bindtype .Type $structs) .Type}}({{.Name}});args.set({{$index}},arg{{$index}}); |
||||
{{end}} |
||||
return this.Contract.transact(opts, "{{.Original.Name}}" , args); |
||||
} |
||||
{{end}} |
||||
|
||||
{{if .Fallback}} |
||||
// Fallback is a paid mutator transaction binding the contract fallback function.
|
||||
//
|
||||
// Solidity: {{.Fallback.Original.String}}
|
||||
public Transaction Fallback(TransactOpts opts, byte[] calldata) throws Exception {
|
||||
return this.Contract.rawTransact(opts, calldata); |
||||
} |
||||
{{end}} |
||||
|
||||
{{if .Receive}} |
||||
// Receive is a paid mutator transaction binding the contract receive function.
|
||||
//
|
||||
// Solidity: {{.Receive.Original.String}}
|
||||
public Transaction Receive(TransactOpts opts) throws Exception {
|
||||
return this.Contract.rawTransact(opts, null); |
||||
} |
||||
{{end}} |
||||
} |
||||
{{end}} |
||||
` |
@ -1,79 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
) |
||||
|
||||
// WaitMined waits for tx to be mined on the blockchain.
|
||||
// It stops waiting when the context is canceled.
|
||||
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error) { |
||||
queryTicker := time.NewTicker(time.Second) |
||||
defer queryTicker.Stop() |
||||
|
||||
logger := log.New("hash", tx.Hash()) |
||||
for { |
||||
receipt, err := b.TransactionReceipt(ctx, tx.Hash()) |
||||
if err == nil { |
||||
return receipt, nil |
||||
} |
||||
|
||||
if errors.Is(err, ethereum.NotFound) { |
||||
logger.Trace("Transaction not yet mined") |
||||
} else { |
||||
logger.Trace("Receipt retrieval failed", "err", err) |
||||
} |
||||
|
||||
// Wait for the next round.
|
||||
select { |
||||
case <-ctx.Done(): |
||||
return nil, ctx.Err() |
||||
case <-queryTicker.C: |
||||
} |
||||
} |
||||
} |
||||
|
||||
// WaitDeployed waits for a contract deployment transaction and returns the on-chain
|
||||
// contract address when it is mined. It stops waiting when ctx is canceled.
|
||||
func WaitDeployed(ctx context.Context, b DeployBackend, tx *types.Transaction) (common.Address, error) { |
||||
if tx.To() != nil { |
||||
return common.Address{}, errors.New("tx is not contract creation") |
||||
} |
||||
receipt, err := WaitMined(ctx, b, tx) |
||||
if err != nil { |
||||
return common.Address{}, err |
||||
} |
||||
if receipt.ContractAddress == (common.Address{}) { |
||||
return common.Address{}, errors.New("zero address") |
||||
} |
||||
// Check that code has indeed been deployed at the address.
|
||||
// This matters on pre-Homestead chains: OOG in the constructor
|
||||
// could leave an empty account behind.
|
||||
code, err := b.CodeAt(ctx, receipt.ContractAddress, nil) |
||||
if err == nil && len(code) == 0 { |
||||
err = ErrNoCodeAfterDeploy |
||||
} |
||||
return receipt.ContractAddress, err |
||||
} |
@ -1,140 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package bind_test |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"math/big" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind" |
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
||||
|
||||
var waitDeployedTests = map[string]struct { |
||||
code string |
||||
gas uint64 |
||||
wantAddress common.Address |
||||
wantErr error |
||||
}{ |
||||
"successful deploy": { |
||||
code: `6060604052600a8060106000396000f360606040526008565b00`, |
||||
gas: 3000000, |
||||
wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), |
||||
}, |
||||
"empty code": { |
||||
code: ``, |
||||
gas: 300000, |
||||
wantErr: bind.ErrNoCodeAfterDeploy, |
||||
wantAddress: common.HexToAddress("0x3a220f351252089d385b29beca14e27f204c296a"), |
||||
}, |
||||
} |
||||
|
||||
func TestWaitDeployed(t *testing.T) { |
||||
for name, test := range waitDeployedTests { |
||||
backend := backends.NewSimulatedBackend( |
||||
core.GenesisAlloc{ |
||||
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)}, |
||||
}, |
||||
10000000, |
||||
) |
||||
defer backend.Close() |
||||
|
||||
// Create the transaction
|
||||
head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
||||
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1)) |
||||
|
||||
tx := types.NewContractCreation(0, big.NewInt(0), test.gas, gasPrice, common.FromHex(test.code)) |
||||
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) |
||||
|
||||
// Wait for it to get mined in the background.
|
||||
var ( |
||||
err error |
||||
address common.Address |
||||
mined = make(chan struct{}) |
||||
ctx = context.Background() |
||||
) |
||||
go func() { |
||||
address, err = bind.WaitDeployed(ctx, backend, tx) |
||||
close(mined) |
||||
}() |
||||
|
||||
// Send and mine the transaction.
|
||||
backend.SendTransaction(ctx, tx) |
||||
backend.Commit() |
||||
|
||||
select { |
||||
case <-mined: |
||||
if err != test.wantErr { |
||||
t.Errorf("test %q: error mismatch: want %q, got %q", name, test.wantErr, err) |
||||
} |
||||
if address != test.wantAddress { |
||||
t.Errorf("test %q: unexpected contract address %s", name, address.Hex()) |
||||
} |
||||
case <-time.After(2 * time.Second): |
||||
t.Errorf("test %q: timeout", name) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestWaitDeployedCornerCases(t *testing.T) { |
||||
backend := backends.NewSimulatedBackend( |
||||
core.GenesisAlloc{ |
||||
crypto.PubkeyToAddress(testKey.PublicKey): {Balance: big.NewInt(10000000000000000)}, |
||||
}, |
||||
10000000, |
||||
) |
||||
defer backend.Close() |
||||
|
||||
head, _ := backend.HeaderByNumber(context.Background(), nil) // Should be child's, good enough
|
||||
gasPrice := new(big.Int).Add(head.BaseFee, big.NewInt(1)) |
||||
|
||||
// Create a transaction to an account.
|
||||
code := "6060604052600a8060106000396000f360606040526008565b00" |
||||
tx := types.NewTransaction(0, common.HexToAddress("0x01"), big.NewInt(0), 3000000, gasPrice, common.FromHex(code)) |
||||
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) |
||||
ctx, cancel := context.WithCancel(context.Background()) |
||||
defer cancel() |
||||
backend.SendTransaction(ctx, tx) |
||||
backend.Commit() |
||||
notContentCreation := errors.New("tx is not contract creation") |
||||
if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != notContentCreation.Error() { |
||||
t.Errorf("error missmatch: want %q, got %q, ", notContentCreation, err) |
||||
} |
||||
|
||||
// Create a transaction that is not mined.
|
||||
tx = types.NewContractCreation(1, big.NewInt(0), 3000000, gasPrice, common.FromHex(code)) |
||||
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey) |
||||
|
||||
go func() { |
||||
contextCanceled := errors.New("context canceled") |
||||
if _, err := bind.WaitDeployed(ctx, backend, tx); err.Error() != contextCanceled.Error() { |
||||
t.Errorf("error missmatch: want %q, got %q, ", contextCanceled, err) |
||||
} |
||||
}() |
||||
|
||||
backend.SendTransaction(ctx, tx) |
||||
cancel() |
||||
} |
@ -1,26 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Package abi implements the Ethereum ABI (Application Binary
|
||||
// Interface).
|
||||
//
|
||||
// The Ethereum ABI is strongly typed, known at compile time
|
||||
// and static. This ABI will handle basic type casting; unsigned
|
||||
// to signed and visa versa. It does not handle slice casting such
|
||||
// as unsigned slice to signed slice. Bit size type casting is also
|
||||
// handled. ints with a bit size of 32 will be properly cast to int256,
|
||||
// etc.
|
||||
package abi |
@ -1,93 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"bytes" |
||||
"errors" |
||||
"fmt" |
||||
"strings" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
type Error struct { |
||||
Name string |
||||
Inputs Arguments |
||||
str string |
||||
|
||||
// Sig contains the string signature according to the ABI spec.
|
||||
// e.g. error foo(uint32 a, int b) = "foo(uint32,int256)"
|
||||
// Please note that "int" is substitute for its canonical representation "int256"
|
||||
Sig string |
||||
|
||||
// ID returns the canonical representation of the error's signature used by the
|
||||
// abi definition to identify event names and types.
|
||||
ID common.Hash |
||||
} |
||||
|
||||
func NewError(name string, inputs Arguments) Error { |
||||
// sanitize inputs to remove inputs without names
|
||||
// and precompute string and sig representation.
|
||||
names := make([]string, len(inputs)) |
||||
types := make([]string, len(inputs)) |
||||
for i, input := range inputs { |
||||
if input.Name == "" { |
||||
inputs[i] = Argument{ |
||||
Name: fmt.Sprintf("arg%d", i), |
||||
Indexed: input.Indexed, |
||||
Type: input.Type, |
||||
} |
||||
} else { |
||||
inputs[i] = input |
||||
} |
||||
// string representation
|
||||
names[i] = fmt.Sprintf("%v %v", input.Type, inputs[i].Name) |
||||
if input.Indexed { |
||||
names[i] = fmt.Sprintf("%v indexed %v", input.Type, inputs[i].Name) |
||||
} |
||||
// sig representation
|
||||
types[i] = input.Type.String() |
||||
} |
||||
|
||||
str := fmt.Sprintf("error %v(%v)", name, strings.Join(names, ", ")) |
||||
sig := fmt.Sprintf("%v(%v)", name, strings.Join(types, ",")) |
||||
id := common.BytesToHash(crypto.Keccak256([]byte(sig))) |
||||
|
||||
return Error{ |
||||
Name: name, |
||||
Inputs: inputs, |
||||
str: str, |
||||
Sig: sig, |
||||
ID: id, |
||||
} |
||||
} |
||||
|
||||
func (e *Error) String() string { |
||||
return e.str |
||||
} |
||||
|
||||
func (e *Error) Unpack(data []byte) (interface{}, error) { |
||||
if len(data) < 4 { |
||||
return "", errors.New("invalid data for unpacking") |
||||
} |
||||
if !bytes.Equal(data[:4], e.ID[:4]) { |
||||
return "", errors.New("invalid data for unpacking") |
||||
} |
||||
return e.Inputs.Unpack(data[4:]) |
||||
} |
@ -1,81 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
) |
||||
|
||||
var ( |
||||
errBadBool = errors.New("abi: improperly encoded boolean value") |
||||
) |
||||
|
||||
// formatSliceString formats the reflection kind with the given slice size
|
||||
// and returns a formatted string representation.
|
||||
func formatSliceString(kind reflect.Kind, sliceSize int) string { |
||||
if sliceSize == -1 { |
||||
return fmt.Sprintf("[]%v", kind) |
||||
} |
||||
return fmt.Sprintf("[%d]%v", sliceSize, kind) |
||||
} |
||||
|
||||
// sliceTypeCheck checks that the given slice can by assigned to the reflection
|
||||
// type in t.
|
||||
func sliceTypeCheck(t Type, val reflect.Value) error { |
||||
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { |
||||
return typeErr(formatSliceString(t.GetType().Kind(), t.Size), val.Type()) |
||||
} |
||||
|
||||
if t.T == ArrayTy && val.Len() != t.Size { |
||||
return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len())) |
||||
} |
||||
|
||||
if t.Elem.T == SliceTy || t.Elem.T == ArrayTy { |
||||
if val.Len() > 0 { |
||||
return sliceTypeCheck(*t.Elem, val.Index(0)) |
||||
} |
||||
} |
||||
|
||||
if val.Type().Elem().Kind() != t.Elem.GetType().Kind() { |
||||
return typeErr(formatSliceString(t.Elem.GetType().Kind(), t.Size), val.Type()) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// typeCheck checks that the given reflection value can be assigned to the reflection
|
||||
// type in t.
|
||||
func typeCheck(t Type, value reflect.Value) error { |
||||
if t.T == SliceTy || t.T == ArrayTy { |
||||
return sliceTypeCheck(t, value) |
||||
} |
||||
|
||||
// Check base type validity. Element types will be checked later on.
|
||||
if t.GetType().Kind() != value.Kind() { |
||||
return typeErr(t.GetType().Kind(), value.Kind()) |
||||
} else if t.T == FixedBytesTy && t.Size != value.Len() { |
||||
return typeErr(t.GetType(), value.Type()) |
||||
} else { |
||||
return nil |
||||
} |
||||
} |
||||
|
||||
// typeErr returns a formatted type casting error.
|
||||
func typeErr(expected, got interface{}) error { |
||||
return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected) |
||||
} |
@ -1,103 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// Event is an event potentially triggered by the EVM's LOG mechanism. The Event
|
||||
// holds type information (inputs) about the yielded output. Anonymous events
|
||||
// don't get the signature canonical representation as the first LOG topic.
|
||||
type Event struct { |
||||
// Name is the event name used for internal representation. It's derived from
|
||||
// the raw name and a suffix will be added in the case of event overloading.
|
||||
//
|
||||
// e.g.
|
||||
// These are two events that have the same name:
|
||||
// * foo(int,int)
|
||||
// * foo(uint,uint)
|
||||
// The event name of the first one will be resolved as foo while the second one
|
||||
// will be resolved as foo0.
|
||||
Name string |
||||
|
||||
// RawName is the raw event name parsed from ABI.
|
||||
RawName string |
||||
Anonymous bool |
||||
Inputs Arguments |
||||
str string |
||||
|
||||
// Sig contains the string signature according to the ABI spec.
|
||||
// e.g. event foo(uint32 a, int b) = "foo(uint32,int256)"
|
||||
// Please note that "int" is substitute for its canonical representation "int256"
|
||||
Sig string |
||||
|
||||
// ID returns the canonical representation of the event's signature used by the
|
||||
// abi definition to identify event names and types.
|
||||
ID common.Hash |
||||
} |
||||
|
||||
// NewEvent creates a new Event.
|
||||
// It sanitizes the input arguments to remove unnamed arguments.
|
||||
// It also precomputes the id, signature and string representation
|
||||
// of the event.
|
||||
func NewEvent(name, rawName string, anonymous bool, inputs Arguments) Event { |
||||
// sanitize inputs to remove inputs without names
|
||||
// and precompute string and sig representation.
|
||||
names := make([]string, len(inputs)) |
||||
types := make([]string, len(inputs)) |
||||
for i, input := range inputs { |
||||
if input.Name == "" { |
||||
inputs[i] = Argument{ |
||||
Name: fmt.Sprintf("arg%d", i), |
||||
Indexed: input.Indexed, |
||||
Type: input.Type, |
||||
} |
||||
} else { |
||||
inputs[i] = input |
||||
} |
||||
// string representation
|
||||
names[i] = fmt.Sprintf("%v %v", input.Type, inputs[i].Name) |
||||
if input.Indexed { |
||||
names[i] = fmt.Sprintf("%v indexed %v", input.Type, inputs[i].Name) |
||||
} |
||||
// sig representation
|
||||
types[i] = input.Type.String() |
||||
} |
||||
|
||||
str := fmt.Sprintf("event %v(%v)", rawName, strings.Join(names, ", ")) |
||||
sig := fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ",")) |
||||
id := common.BytesToHash(crypto.Keccak256([]byte(sig))) |
||||
|
||||
return Event{ |
||||
Name: name, |
||||
RawName: rawName, |
||||
Anonymous: anonymous, |
||||
Inputs: inputs, |
||||
str: str, |
||||
Sig: sig, |
||||
ID: id, |
||||
} |
||||
} |
||||
|
||||
func (e Event) String() string { |
||||
return e.str |
||||
} |
@ -1,389 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"math/big" |
||||
"reflect" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/stretchr/testify/assert" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
var jsonEventTransfer = []byte(`{ |
||||
"anonymous": false, |
||||
"inputs": [ |
||||
{ |
||||
"indexed": true, "name": "from", "type": "address" |
||||
}, { |
||||
"indexed": true, "name": "to", "type": "address" |
||||
}, { |
||||
"indexed": false, "name": "value", "type": "uint256" |
||||
}], |
||||
"name": "Transfer", |
||||
"type": "event" |
||||
}`) |
||||
|
||||
var jsonEventPledge = []byte(`{ |
||||
"anonymous": false, |
||||
"inputs": [{ |
||||
"indexed": false, "name": "who", "type": "address" |
||||
}, { |
||||
"indexed": false, "name": "wad", "type": "uint128" |
||||
}, { |
||||
"indexed": false, "name": "currency", "type": "bytes3" |
||||
}], |
||||
"name": "Pledge", |
||||
"type": "event" |
||||
}`) |
||||
|
||||
var jsonEventMixedCase = []byte(`{ |
||||
"anonymous": false, |
||||
"inputs": [{ |
||||
"indexed": false, "name": "value", "type": "uint256" |
||||
}, { |
||||
"indexed": false, "name": "_value", "type": "uint256" |
||||
}, { |
||||
"indexed": false, "name": "Value", "type": "uint256" |
||||
}], |
||||
"name": "MixedCase", |
||||
"type": "event" |
||||
}`) |
||||
|
||||
// 1000000
|
||||
var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240" |
||||
|
||||
// "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd"
|
||||
var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000" |
||||
|
||||
// 1000000,2218516807680,1000001
|
||||
var mixedCaseData1 = "00000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000000000000000000000020489e8000000000000000000000000000000000000000000000000000000000000000f4241" |
||||
|
||||
func TestEventId(t *testing.T) { |
||||
var table = []struct { |
||||
definition string |
||||
expectations map[string]common.Hash |
||||
}{ |
||||
{ |
||||
definition: `[ |
||||
{ "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, |
||||
{ "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] } |
||||
]`, |
||||
expectations: map[string]common.Hash{ |
||||
"Balance": crypto.Keccak256Hash([]byte("Balance(uint256)")), |
||||
"Check": crypto.Keccak256Hash([]byte("Check(address,uint256)")), |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
for _, test := range table { |
||||
abi, err := JSON(strings.NewReader(test.definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
for name, event := range abi.Events { |
||||
if event.ID != test.expectations[name] { |
||||
t.Errorf("expected id to be %x, got %x", test.expectations[name], event.ID) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestEventString(t *testing.T) { |
||||
var table = []struct { |
||||
definition string |
||||
expectations map[string]string |
||||
}{ |
||||
{ |
||||
definition: `[ |
||||
{ "type" : "event", "name" : "Balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, |
||||
{ "type" : "event", "name" : "Check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] }, |
||||
{ "type" : "event", "name" : "Transfer", "inputs": [{ "name": "from", "type": "address", "indexed": true }, { "name": "to", "type": "address", "indexed": true }, { "name": "value", "type": "uint256" }] } |
||||
]`, |
||||
expectations: map[string]string{ |
||||
"Balance": "event Balance(uint256 in)", |
||||
"Check": "event Check(address t, uint256 b)", |
||||
"Transfer": "event Transfer(address indexed from, address indexed to, uint256 value)", |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
for _, test := range table { |
||||
abi, err := JSON(strings.NewReader(test.definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
for name, event := range abi.Events { |
||||
if event.String() != test.expectations[name] { |
||||
t.Errorf("expected string to be %s, got %s", test.expectations[name], event.String()) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestEventMultiValueWithArrayUnpack verifies that array fields will be counted after parsing array.
|
||||
func TestEventMultiValueWithArrayUnpack(t *testing.T) { |
||||
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": false, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
require.NoError(t, err) |
||||
var b bytes.Buffer |
||||
var i uint8 = 1 |
||||
for ; i <= 3; i++ { |
||||
b.Write(packNum(reflect.ValueOf(i))) |
||||
} |
||||
unpacked, err := abi.Unpack("test", b.Bytes()) |
||||
require.NoError(t, err) |
||||
require.Equal(t, [2]uint8{1, 2}, unpacked[0]) |
||||
require.Equal(t, uint8(3), unpacked[1]) |
||||
} |
||||
|
||||
func TestEventTupleUnpack(t *testing.T) { |
||||
type EventTransfer struct { |
||||
Value *big.Int |
||||
} |
||||
|
||||
type EventTransferWithTag struct { |
||||
// this is valid because `value` is not exportable,
|
||||
// so value is only unmarshalled into `Value1`.
|
||||
value *big.Int //lint:ignore U1000 unused field is part of test
|
||||
Value1 *big.Int `abi:"value"` |
||||
} |
||||
|
||||
type BadEventTransferWithSameFieldAndTag struct { |
||||
Value *big.Int |
||||
Value1 *big.Int `abi:"value"` |
||||
} |
||||
|
||||
type BadEventTransferWithDuplicatedTag struct { |
||||
Value1 *big.Int `abi:"value"` |
||||
Value2 *big.Int `abi:"value"` |
||||
} |
||||
|
||||
type BadEventTransferWithEmptyTag struct { |
||||
Value *big.Int `abi:""` |
||||
} |
||||
|
||||
type EventPledge struct { |
||||
Who common.Address |
||||
Wad *big.Int |
||||
Currency [3]byte |
||||
} |
||||
|
||||
type BadEventPledge struct { |
||||
Who string |
||||
Wad int |
||||
Currency [3]byte |
||||
} |
||||
|
||||
type EventMixedCase struct { |
||||
Value1 *big.Int `abi:"value"` |
||||
Value2 *big.Int `abi:"_value"` |
||||
Value3 *big.Int `abi:"Value"` |
||||
} |
||||
|
||||
bigint := new(big.Int) |
||||
bigintExpected := big.NewInt(1000000) |
||||
bigintExpected2 := big.NewInt(2218516807680) |
||||
bigintExpected3 := big.NewInt(1000001) |
||||
addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268") |
||||
var testCases = []struct { |
||||
data string |
||||
dest interface{} |
||||
expected interface{} |
||||
jsonLog []byte |
||||
error string |
||||
name string |
||||
}{{ |
||||
transferData1, |
||||
&EventTransfer{}, |
||||
&EventTransfer{Value: bigintExpected}, |
||||
jsonEventTransfer, |
||||
"", |
||||
"Can unpack ERC20 Transfer event into structure", |
||||
}, { |
||||
transferData1, |
||||
&[]interface{}{&bigint}, |
||||
&[]interface{}{&bigintExpected}, |
||||
jsonEventTransfer, |
||||
"", |
||||
"Can unpack ERC20 Transfer event into slice", |
||||
}, { |
||||
transferData1, |
||||
&EventTransferWithTag{}, |
||||
&EventTransferWithTag{Value1: bigintExpected}, |
||||
jsonEventTransfer, |
||||
"", |
||||
"Can unpack ERC20 Transfer event into structure with abi: tag", |
||||
}, { |
||||
transferData1, |
||||
&BadEventTransferWithDuplicatedTag{}, |
||||
&BadEventTransferWithDuplicatedTag{}, |
||||
jsonEventTransfer, |
||||
"struct: abi tag in 'Value2' already mapped", |
||||
"Can not unpack ERC20 Transfer event with duplicated abi tag", |
||||
}, { |
||||
transferData1, |
||||
&BadEventTransferWithSameFieldAndTag{}, |
||||
&BadEventTransferWithSameFieldAndTag{}, |
||||
jsonEventTransfer, |
||||
"abi: multiple variables maps to the same abi field 'value'", |
||||
"Can not unpack ERC20 Transfer event with a field and a tag mapping to the same abi variable", |
||||
}, { |
||||
transferData1, |
||||
&BadEventTransferWithEmptyTag{}, |
||||
&BadEventTransferWithEmptyTag{}, |
||||
jsonEventTransfer, |
||||
"struct: abi tag in 'Value' is empty", |
||||
"Can not unpack ERC20 Transfer event with an empty tag", |
||||
}, { |
||||
pledgeData1, |
||||
&EventPledge{}, |
||||
&EventPledge{ |
||||
addr, |
||||
bigintExpected2, |
||||
[3]byte{'u', 's', 'd'}}, |
||||
jsonEventPledge, |
||||
"", |
||||
"Can unpack Pledge event into structure", |
||||
}, { |
||||
pledgeData1, |
||||
&[]interface{}{&common.Address{}, &bigint, &[3]byte{}}, |
||||
&[]interface{}{ |
||||
&addr, |
||||
&bigintExpected2, |
||||
&[3]byte{'u', 's', 'd'}}, |
||||
jsonEventPledge, |
||||
"", |
||||
"Can unpack Pledge event into slice", |
||||
}, { |
||||
pledgeData1, |
||||
&[3]interface{}{&common.Address{}, &bigint, &[3]byte{}}, |
||||
&[3]interface{}{ |
||||
&addr, |
||||
&bigintExpected2, |
||||
&[3]byte{'u', 's', 'd'}}, |
||||
jsonEventPledge, |
||||
"", |
||||
"Can unpack Pledge event into an array", |
||||
}, { |
||||
pledgeData1, |
||||
&[]interface{}{new(int), 0, 0}, |
||||
&[]interface{}{}, |
||||
jsonEventPledge, |
||||
"abi: cannot unmarshal common.Address in to int", |
||||
"Can not unpack Pledge event into slice with wrong types", |
||||
}, { |
||||
pledgeData1, |
||||
&BadEventPledge{}, |
||||
&BadEventPledge{}, |
||||
jsonEventPledge, |
||||
"abi: cannot unmarshal common.Address in to string", |
||||
"Can not unpack Pledge event into struct with wrong filed types", |
||||
}, { |
||||
pledgeData1, |
||||
&[]interface{}{common.Address{}, new(big.Int)}, |
||||
&[]interface{}{}, |
||||
jsonEventPledge, |
||||
"abi: insufficient number of arguments for unpack, want 3, got 2", |
||||
"Can not unpack Pledge event into too short slice", |
||||
}, { |
||||
pledgeData1, |
||||
new(map[string]interface{}), |
||||
&[]interface{}{}, |
||||
jsonEventPledge, |
||||
"abi:[2] cannot unmarshal tuple in to map[string]interface {}", |
||||
"Can not unpack Pledge event into map", |
||||
}, { |
||||
mixedCaseData1, |
||||
&EventMixedCase{}, |
||||
&EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3}, |
||||
jsonEventMixedCase, |
||||
"", |
||||
"Can unpack abi variables with mixed case", |
||||
}} |
||||
|
||||
for _, tc := range testCases { |
||||
assert := assert.New(t) |
||||
tc := tc |
||||
t.Run(tc.name, func(t *testing.T) { |
||||
err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert) |
||||
if tc.error == "" { |
||||
assert.Nil(err, "Should be able to unpack event data.") |
||||
assert.Equal(tc.expected, tc.dest, tc.name) |
||||
} else { |
||||
assert.EqualError(err, tc.error, tc.name) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error { |
||||
data, err := hex.DecodeString(hexData) |
||||
assert.NoError(err, "Hex data should be a correct hex-string") |
||||
var e Event |
||||
assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI") |
||||
a := ABI{Events: map[string]Event{"e": e}} |
||||
return a.UnpackIntoInterface(dest, "e", data) |
||||
} |
||||
|
||||
// TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder.
|
||||
func TestEventUnpackIndexed(t *testing.T) { |
||||
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` |
||||
type testStruct struct { |
||||
Value1 uint8 // indexed
|
||||
Value2 uint8 |
||||
} |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
require.NoError(t, err) |
||||
var b bytes.Buffer |
||||
b.Write(packNum(reflect.ValueOf(uint8(8)))) |
||||
var rst testStruct |
||||
require.NoError(t, abi.UnpackIntoInterface(&rst, "test", b.Bytes())) |
||||
require.Equal(t, uint8(0), rst.Value1) |
||||
require.Equal(t, uint8(8), rst.Value2) |
||||
} |
||||
|
||||
// TestEventIndexedWithArrayUnpack verifies that decoder will not overflow when static array is indexed input.
|
||||
func TestEventIndexedWithArrayUnpack(t *testing.T) { |
||||
definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]` |
||||
type testStruct struct { |
||||
Value1 [2]uint8 // indexed
|
||||
Value2 string |
||||
} |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
require.NoError(t, err) |
||||
var b bytes.Buffer |
||||
stringOut := "abc" |
||||
// number of fields that will be encoded * 32
|
||||
b.Write(packNum(reflect.ValueOf(32))) |
||||
b.Write(packNum(reflect.ValueOf(len(stringOut)))) |
||||
b.Write(common.RightPadBytes([]byte(stringOut), 32)) |
||||
|
||||
var rst testStruct |
||||
require.NoError(t, abi.UnpackIntoInterface(&rst, "test", b.Bytes())) |
||||
require.Equal(t, [2]uint8{0, 0}, rst.Value1) |
||||
require.Equal(t, stringOut, rst.Value2) |
||||
} |
@ -1,167 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"fmt" |
||||
"strings" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// FunctionType represents different types of functions a contract might have.
|
||||
type FunctionType int |
||||
|
||||
const ( |
||||
// Constructor represents the constructor of the contract.
|
||||
// The constructor function is called while deploying a contract.
|
||||
Constructor FunctionType = iota |
||||
// Fallback represents the fallback function.
|
||||
// This function is executed if no other function matches the given function
|
||||
// signature and no receive function is specified.
|
||||
Fallback |
||||
// Receive represents the receive function.
|
||||
// This function is executed on plain Ether transfers.
|
||||
Receive |
||||
// Function represents a normal function.
|
||||
Function |
||||
) |
||||
|
||||
// Method represents a callable given a `Name` and whether the method is a constant.
|
||||
// If the method is `Const` no transaction needs to be created for this
|
||||
// particular Method call. It can easily be simulated using a local VM.
|
||||
// For example a `Balance()` method only needs to retrieve something
|
||||
// from the storage and therefore requires no Tx to be sent to the
|
||||
// network. A method such as `Transact` does require a Tx and thus will
|
||||
// be flagged `false`.
|
||||
// Input specifies the required input parameters for this gives method.
|
||||
type Method struct { |
||||
// Name is the method name used for internal representation. It's derived from
|
||||
// the raw name and a suffix will be added in the case of a function overload.
|
||||
//
|
||||
// e.g.
|
||||
// These are two functions that have the same name:
|
||||
// * foo(int,int)
|
||||
// * foo(uint,uint)
|
||||
// The method name of the first one will be resolved as foo while the second one
|
||||
// will be resolved as foo0.
|
||||
Name string |
||||
RawName string // RawName is the raw method name parsed from ABI
|
||||
|
||||
// Type indicates whether the method is a
|
||||
// special fallback introduced in solidity v0.6.0
|
||||
Type FunctionType |
||||
|
||||
// StateMutability indicates the mutability state of method,
|
||||
// the default value is nonpayable. It can be empty if the abi
|
||||
// is generated by legacy compiler.
|
||||
StateMutability string |
||||
|
||||
// Legacy indicators generated by compiler before v0.6.0
|
||||
Constant bool |
||||
Payable bool |
||||
|
||||
Inputs Arguments |
||||
Outputs Arguments |
||||
str string |
||||
// Sig returns the methods string signature according to the ABI spec.
|
||||
// e.g. function foo(uint32 a, int b) = "foo(uint32,int256)"
|
||||
// Please note that "int" is substitute for its canonical representation "int256"
|
||||
Sig string |
||||
// ID returns the canonical representation of the method's signature used by the
|
||||
// abi definition to identify method names and types.
|
||||
ID []byte |
||||
} |
||||
|
||||
// NewMethod creates a new Method.
|
||||
// A method should always be created using NewMethod.
|
||||
// It also precomputes the sig representation and the string representation
|
||||
// of the method.
|
||||
func NewMethod(name string, rawName string, funType FunctionType, mutability string, isConst, isPayable bool, inputs Arguments, outputs Arguments) Method { |
||||
var ( |
||||
types = make([]string, len(inputs)) |
||||
inputNames = make([]string, len(inputs)) |
||||
outputNames = make([]string, len(outputs)) |
||||
) |
||||
for i, input := range inputs { |
||||
inputNames[i] = fmt.Sprintf("%v %v", input.Type, input.Name) |
||||
types[i] = input.Type.String() |
||||
} |
||||
for i, output := range outputs { |
||||
outputNames[i] = output.Type.String() |
||||
if len(output.Name) > 0 { |
||||
outputNames[i] += fmt.Sprintf(" %v", output.Name) |
||||
} |
||||
} |
||||
// calculate the signature and method id. Note only function
|
||||
// has meaningful signature and id.
|
||||
var ( |
||||
sig string |
||||
id []byte |
||||
) |
||||
if funType == Function { |
||||
sig = fmt.Sprintf("%v(%v)", rawName, strings.Join(types, ",")) |
||||
id = crypto.Keccak256([]byte(sig))[:4] |
||||
} |
||||
// Extract meaningful state mutability of solidity method.
|
||||
// If it's default value, never print it.
|
||||
state := mutability |
||||
if state == "nonpayable" { |
||||
state = "" |
||||
} |
||||
if state != "" { |
||||
state = state + " " |
||||
} |
||||
identity := fmt.Sprintf("function %v", rawName) |
||||
if funType == Fallback { |
||||
identity = "fallback" |
||||
} else if funType == Receive { |
||||
identity = "receive" |
||||
} else if funType == Constructor { |
||||
identity = "constructor" |
||||
} |
||||
str := fmt.Sprintf("%v(%v) %sreturns(%v)", identity, strings.Join(inputNames, ", "), state, strings.Join(outputNames, ", ")) |
||||
|
||||
return Method{ |
||||
Name: name, |
||||
RawName: rawName, |
||||
Type: funType, |
||||
StateMutability: mutability, |
||||
Constant: isConst, |
||||
Payable: isPayable, |
||||
Inputs: inputs, |
||||
Outputs: outputs, |
||||
str: str, |
||||
Sig: sig, |
||||
ID: id, |
||||
} |
||||
} |
||||
|
||||
func (method Method) String() string { |
||||
return method.str |
||||
} |
||||
|
||||
// IsConstant returns the indicator whether the method is read-only.
|
||||
func (method Method) IsConstant() bool { |
||||
return method.StateMutability == "view" || method.StateMutability == "pure" || method.Constant |
||||
} |
||||
|
||||
// IsPayable returns the indicator whether the method can process
|
||||
// plain ether transfers.
|
||||
func (method Method) IsPayable() bool { |
||||
return method.StateMutability == "payable" || method.Payable |
||||
} |
@ -1,145 +0,0 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"strings" |
||||
"testing" |
||||
) |
||||
|
||||
const methoddata = ` |
||||
[ |
||||
{"type": "function", "name": "balance", "stateMutability": "view"}, |
||||
{"type": "function", "name": "send", "inputs": [{ "name": "amount", "type": "uint256" }]}, |
||||
{"type": "function", "name": "transfer", "inputs": [{"name": "from", "type": "address"}, {"name": "to", "type": "address"}, {"name": "value", "type": "uint256"}], "outputs": [{"name": "success", "type": "bool"}]}, |
||||
{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple"}],"name":"tuple","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}, |
||||
{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple[]"}],"name":"tupleSlice","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}, |
||||
{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple[5]"}],"name":"tupleArray","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}, |
||||
{"constant":false,"inputs":[{"components":[{"name":"x","type":"uint256"},{"name":"y","type":"uint256"}],"name":"a","type":"tuple[5][]"}],"name":"complexTuple","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}, |
||||
{"stateMutability":"nonpayable","type":"fallback"}, |
||||
{"stateMutability":"payable","type":"receive"} |
||||
]` |
||||
|
||||
func TestMethodString(t *testing.T) { |
||||
var table = []struct { |
||||
method string |
||||
expectation string |
||||
}{ |
||||
{ |
||||
method: "balance", |
||||
expectation: "function balance() view returns()", |
||||
}, |
||||
{ |
||||
method: "send", |
||||
expectation: "function send(uint256 amount) returns()", |
||||
}, |
||||
{ |
||||
method: "transfer", |
||||
expectation: "function transfer(address from, address to, uint256 value) returns(bool success)", |
||||
}, |
||||
{ |
||||
method: "tuple", |
||||
expectation: "function tuple((uint256,uint256) a) returns()", |
||||
}, |
||||
{ |
||||
method: "tupleArray", |
||||
expectation: "function tupleArray((uint256,uint256)[5] a) returns()", |
||||
}, |
||||
{ |
||||
method: "tupleSlice", |
||||
expectation: "function tupleSlice((uint256,uint256)[] a) returns()", |
||||
}, |
||||
{ |
||||
method: "complexTuple", |
||||
expectation: "function complexTuple((uint256,uint256)[5][] a) returns()", |
||||
}, |
||||
{ |
||||
method: "fallback", |
||||
expectation: "fallback() returns()", |
||||
}, |
||||
{ |
||||
method: "receive", |
||||
expectation: "receive() payable returns()", |
||||
}, |
||||
} |
||||
|
||||
abi, err := JSON(strings.NewReader(methoddata)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
for _, test := range table { |
||||
var got string |
||||
if test.method == "fallback" { |
||||
got = abi.Fallback.String() |
||||
} else if test.method == "receive" { |
||||
got = abi.Receive.String() |
||||
} else { |
||||
got = abi.Methods[test.method].String() |
||||
} |
||||
if got != test.expectation { |
||||
t.Errorf("expected string to be %s, got %s", test.expectation, got) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestMethodSig(t *testing.T) { |
||||
var cases = []struct { |
||||
method string |
||||
expect string |
||||
}{ |
||||
{ |
||||
method: "balance", |
||||
expect: "balance()", |
||||
}, |
||||
{ |
||||
method: "send", |
||||
expect: "send(uint256)", |
||||
}, |
||||
{ |
||||
method: "transfer", |
||||
expect: "transfer(address,address,uint256)", |
||||
}, |
||||
{ |
||||
method: "tuple", |
||||
expect: "tuple((uint256,uint256))", |
||||
}, |
||||
{ |
||||
method: "tupleArray", |
||||
expect: "tupleArray((uint256,uint256)[5])", |
||||
}, |
||||
{ |
||||
method: "tupleSlice", |
||||
expect: "tupleSlice((uint256,uint256)[])", |
||||
}, |
||||
{ |
||||
method: "complexTuple", |
||||
expect: "complexTuple((uint256,uint256)[5][])", |
||||
}, |
||||
} |
||||
abi, err := JSON(strings.NewReader(methoddata)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
for _, test := range cases { |
||||
got := abi.Methods[test.method].Sig |
||||
if got != test.expect { |
||||
t.Errorf("expected string to be %s, got %s", test.expect, got) |
||||
} |
||||
} |
||||
} |
@ -1,85 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
"reflect" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/math" |
||||
) |
||||
|
||||
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
|
||||
// bytes slice.
|
||||
func packBytesSlice(bytes []byte, l int) []byte { |
||||
len := packNum(reflect.ValueOf(l)) |
||||
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...) |
||||
} |
||||
|
||||
// packElement packs the given reflect value according to the abi specification in
|
||||
// t.
|
||||
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) { |
||||
switch t.T { |
||||
case IntTy, UintTy: |
||||
return packNum(reflectValue), nil |
||||
case StringTy: |
||||
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil |
||||
case AddressTy: |
||||
if reflectValue.Kind() == reflect.Array { |
||||
reflectValue = mustArrayToByteSlice(reflectValue) |
||||
} |
||||
|
||||
return common.LeftPadBytes(reflectValue.Bytes(), 32), nil |
||||
case BoolTy: |
||||
if reflectValue.Bool() { |
||||
return math.PaddedBigBytes(common.Big1, 32), nil |
||||
} |
||||
return math.PaddedBigBytes(common.Big0, 32), nil |
||||
case BytesTy: |
||||
if reflectValue.Kind() == reflect.Array { |
||||
reflectValue = mustArrayToByteSlice(reflectValue) |
||||
} |
||||
if reflectValue.Type() != reflect.TypeOf([]byte{}) { |
||||
return []byte{}, errors.New("Bytes type is neither slice nor array") |
||||
} |
||||
return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil |
||||
case FixedBytesTy, FunctionTy: |
||||
if reflectValue.Kind() == reflect.Array { |
||||
reflectValue = mustArrayToByteSlice(reflectValue) |
||||
} |
||||
return common.RightPadBytes(reflectValue.Bytes(), 32), nil |
||||
default: |
||||
return []byte{}, fmt.Errorf("Could not pack element, unknown type: %v", t.T) |
||||
} |
||||
} |
||||
|
||||
// packNum packs the given number (using the reflect value) and will cast it to appropriate number representation.
|
||||
func packNum(value reflect.Value) []byte { |
||||
switch kind := value.Kind(); kind { |
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
||||
return math.U256Bytes(new(big.Int).SetUint64(value.Uint())) |
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
||||
return math.U256Bytes(big.NewInt(value.Int())) |
||||
case reflect.Ptr: |
||||
return math.U256Bytes(new(big.Int).Set(value.Interface().(*big.Int))) |
||||
default: |
||||
panic("abi: fatal error") |
||||
} |
||||
} |
@ -1,211 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/hex" |
||||
"fmt" |
||||
"math" |
||||
"math/big" |
||||
"reflect" |
||||
"strconv" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
// TestPack tests the general pack/unpack tests in packing_test.go
|
||||
func TestPack(t *testing.T) { |
||||
for i, test := range packUnpackTests { |
||||
t.Run(strconv.Itoa(i), func(t *testing.T) { |
||||
encb, err := hex.DecodeString(test.packed) |
||||
if err != nil { |
||||
t.Fatalf("invalid hex %s: %v", test.packed, err) |
||||
} |
||||
inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def) |
||||
inAbi, err := JSON(strings.NewReader(inDef)) |
||||
if err != nil { |
||||
t.Fatalf("invalid ABI definition %s, %v", inDef, err) |
||||
} |
||||
var packed []byte |
||||
packed, err = inAbi.Pack("method", test.unpacked) |
||||
|
||||
if err != nil { |
||||
t.Fatalf("test %d (%v) failed: %v", i, test.def, err) |
||||
} |
||||
if !reflect.DeepEqual(packed[4:], encb) { |
||||
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:]) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestMethodPack(t *testing.T) { |
||||
abi, err := JSON(strings.NewReader(jsondata)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
sig := abi.Methods["slice"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
|
||||
packed, err := abi.Pack("slice", []uint32{1, 2}) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
|
||||
var addrA, addrB = common.Address{1}, common.Address{2} |
||||
sig = abi.Methods["sliceAddress"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) |
||||
|
||||
packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
|
||||
var addrC, addrD = common.Address{3}, common.Address{4} |
||||
sig = abi.Methods["sliceMultiAddress"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) |
||||
|
||||
packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
|
||||
sig = abi.Methods["slice256"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
|
||||
packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
|
||||
a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}} |
||||
sig = abi.Methods["nestedArray"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) |
||||
sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) |
||||
packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
|
||||
sig = abi.Methods["nestedArray2"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
|
||||
sig = abi.Methods["nestedSlice"].ID |
||||
sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) |
||||
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) |
||||
packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}}) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !bytes.Equal(packed, sig) { |
||||
t.Errorf("expected %x got %x", sig, packed) |
||||
} |
||||
} |
||||
|
||||
func TestPackNumber(t *testing.T) { |
||||
tests := []struct { |
||||
value reflect.Value |
||||
packed []byte |
||||
}{ |
||||
// Protocol limits
|
||||
{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")}, |
||||
{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")}, |
||||
{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}, |
||||
|
||||
// Type corner cases
|
||||
{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")}, |
||||
{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")}, |
||||
{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")}, |
||||
{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")}, |
||||
|
||||
{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")}, |
||||
{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")}, |
||||
{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")}, |
||||
{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")}, |
||||
|
||||
{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")}, |
||||
{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")}, |
||||
{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")}, |
||||
{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, |
||||
} |
||||
for i, tt := range tests { |
||||
packed := packNum(tt.value) |
||||
if !bytes.Equal(packed, tt.packed) { |
||||
t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) |
||||
} |
||||
} |
||||
} |
@ -1,990 +0,0 @@ |
||||
// Copyright 2020 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
type packUnpackTest struct { |
||||
def string |
||||
unpacked interface{} |
||||
packed string |
||||
} |
||||
|
||||
var packUnpackTests = []packUnpackTest{ |
||||
// Booleans
|
||||
{ |
||||
def: `[{ "type": "bool" }]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: true, |
||||
}, |
||||
{ |
||||
def: `[{ "type": "bool" }]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: false, |
||||
}, |
||||
// Integers
|
||||
{ |
||||
def: `[{ "type": "uint8" }]`, |
||||
unpacked: uint8(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "uint8[]" }]`, |
||||
unpacked: []uint8{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "uint16" }]`, |
||||
unpacked: uint16(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "uint16[]" }]`, |
||||
unpacked: []uint16{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint17"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: big.NewInt(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: uint32(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[]"}]`, |
||||
unpacked: []uint32{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64"}]`, |
||||
unpacked: uint64(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64[]"}]`, |
||||
unpacked: []uint64{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256"}]`, |
||||
unpacked: big.NewInt(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[]"}]`, |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int8"}]`, |
||||
unpacked: int8(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int8[]"}]`, |
||||
unpacked: []int8{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16"}]`, |
||||
unpacked: int16(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16[]"}]`, |
||||
unpacked: []int16{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int17"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: big.NewInt(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32"}]`, |
||||
unpacked: int32(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: int32(1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32[]"}]`, |
||||
unpacked: []int32{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64"}]`, |
||||
unpacked: int64(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64[]"}]`, |
||||
unpacked: []int64{1, 2}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256"}]`, |
||||
unpacked: big.NewInt(2), |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256"}]`, |
||||
packed: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", |
||||
unpacked: big.NewInt(-1), |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256[]"}]`, |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
}, |
||||
// Address
|
||||
{ |
||||
def: `[{"type": "address"}]`, |
||||
packed: "0000000000000000000000000100000000000000000000000000000000000000", |
||||
unpacked: common.Address{1}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "address[]"}]`, |
||||
unpacked: []common.Address{{1}, {2}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000100000000000000000000000000000000000000" + |
||||
"0000000000000000000000000200000000000000000000000000000000000000", |
||||
}, |
||||
// Bytes
|
||||
{ |
||||
def: `[{"type": "bytes1"}]`, |
||||
unpacked: [1]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes2"}]`, |
||||
unpacked: [2]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes3"}]`, |
||||
unpacked: [3]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes4"}]`, |
||||
unpacked: [4]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes5"}]`, |
||||
unpacked: [5]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes6"}]`, |
||||
unpacked: [6]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes7"}]`, |
||||
unpacked: [7]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes8"}]`, |
||||
unpacked: [8]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes9"}]`, |
||||
unpacked: [9]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes10"}]`, |
||||
unpacked: [10]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes11"}]`, |
||||
unpacked: [11]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes12"}]`, |
||||
unpacked: [12]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes13"}]`, |
||||
unpacked: [13]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes14"}]`, |
||||
unpacked: [14]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes15"}]`, |
||||
unpacked: [15]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes16"}]`, |
||||
unpacked: [16]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes17"}]`, |
||||
unpacked: [17]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes18"}]`, |
||||
unpacked: [18]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes19"}]`, |
||||
unpacked: [19]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes20"}]`, |
||||
unpacked: [20]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes21"}]`, |
||||
unpacked: [21]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes22"}]`, |
||||
unpacked: [22]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes23"}]`, |
||||
unpacked: [23]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes24"}]`, |
||||
unpacked: [24]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes25"}]`, |
||||
unpacked: [25]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes26"}]`, |
||||
unpacked: [26]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes27"}]`, |
||||
unpacked: [27]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes28"}]`, |
||||
unpacked: [28]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes29"}]`, |
||||
unpacked: [29]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes30"}]`, |
||||
unpacked: [30]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes31"}]`, |
||||
unpacked: [31]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
unpacked: [32]byte{1}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
||||
}, |
||||
// Functions
|
||||
{ |
||||
def: `[{"type": "function"}]`, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [24]byte{1}, |
||||
}, |
||||
// Slice and Array
|
||||
{ |
||||
def: `[{"type": "uint8[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint8{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: []uint8{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: []*big.Int{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint8{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int8[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int8{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []int16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int16[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []int32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []int64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int64[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]int64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "int256[3]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003", |
||||
unpacked: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, |
||||
}, |
||||
// multi dimensional, if these pass, all types that don't require length prefix should pass
|
||||
{ |
||||
def: `[{"type": "uint8[][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [][]uint8{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [][]uint8{{1, 2}, {1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003", |
||||
unpacked: [][]uint8{{1, 2}, {1, 2, 3}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2][2]uint8{{1, 2}, {1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000060" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [2][]uint8{{}, {}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[][2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: [2][]uint8{{1}, {1}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [][2]uint8{}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [][2]uint8{{1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint8[2][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [][2]uint8{{1, 2}, {1, 2}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint16[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint16[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint16{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[2][3][4]"}]`, |
||||
unpacked: [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"0000000000000000000000000000000000000000000000000000000000000004" + |
||||
"0000000000000000000000000000000000000000000000000000000000000005" + |
||||
"0000000000000000000000000000000000000000000000000000000000000006" + |
||||
"0000000000000000000000000000000000000000000000000000000000000007" + |
||||
"0000000000000000000000000000000000000000000000000000000000000008" + |
||||
"0000000000000000000000000000000000000000000000000000000000000009" + |
||||
"000000000000000000000000000000000000000000000000000000000000000a" + |
||||
"000000000000000000000000000000000000000000000000000000000000000b" + |
||||
"000000000000000000000000000000000000000000000000000000000000000c" + |
||||
"000000000000000000000000000000000000000000000000000000000000000d" + |
||||
"000000000000000000000000000000000000000000000000000000000000000e" + |
||||
"000000000000000000000000000000000000000000000000000000000000000f" + |
||||
"0000000000000000000000000000000000000000000000000000000000000010" + |
||||
"0000000000000000000000000000000000000000000000000000000000000011" + |
||||
"0000000000000000000000000000000000000000000000000000000000000012" + |
||||
"0000000000000000000000000000000000000000000000000000000000000013" + |
||||
"0000000000000000000000000000000000000000000000000000000000000014" + |
||||
"0000000000000000000000000000000000000000000000000000000000000015" + |
||||
"0000000000000000000000000000000000000000000000000000000000000016" + |
||||
"0000000000000000000000000000000000000000000000000000000000000017" + |
||||
"0000000000000000000000000000000000000000000000000000000000000018", |
||||
}, |
||||
|
||||
{ |
||||
def: `[{"type": "bytes32[]"}]`, |
||||
unpacked: [][32]byte{{1}, {2}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0100000000000000000000000000000000000000000000000000000000000000" + |
||||
"0200000000000000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint32[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint32{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []uint64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint64[2]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: [2]uint64{1, 2}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[3]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003", |
||||
unpacked: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "string[4]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"00000000000000000000000000000000000000000000000000000000000000c0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000100" + |
||||
"0000000000000000000000000000000000000000000000000000000000000140" + |
||||
"0000000000000000000000000000000000000000000000000000000000000005" + |
||||
"48656c6c6f000000000000000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000005" + |
||||
"576f726c64000000000000000000000000000000000000000000000000000000" + |
||||
"000000000000000000000000000000000000000000000000000000000000000b" + |
||||
"476f2d657468657265756d000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000008" + |
||||
"457468657265756d000000000000000000000000000000000000000000000000", |
||||
unpacked: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "string[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"0000000000000000000000000000000000000000000000000000000000000008" + |
||||
"457468657265756d000000000000000000000000000000000000000000000000" + |
||||
"000000000000000000000000000000000000000000000000000000000000000b" + |
||||
"676f2d657468657265756d000000000000000000000000000000000000000000", |
||||
unpacked: []string{"Ethereum", "go-ethereum"}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes[]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"0000000000000000000000000000000000000000000000000000000000000080" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"f0f0f00000000000000000000000000000000000000000000000000000000000" + |
||||
"0000000000000000000000000000000000000000000000000000000000000003" + |
||||
"f0f0f00000000000000000000000000000000000000000000000000000000000", |
||||
unpacked: [][]byte{{0xf0, 0xf0, 0xf0}, {0xf0, 0xf0, 0xf0}}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint256[2][][]"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + |
||||
"00000000000000000000000000000000000000000000000000000000000000e0" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000000c8" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000003e8" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000000c8" + |
||||
"0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"00000000000000000000000000000000000000000000000000000000000003e8", |
||||
unpacked: [][][2]*big.Int{{{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}, {{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}}, |
||||
}, |
||||
// struct outputs
|
||||
{ |
||||
def: `[{"components": [{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int_one","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int__one","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int_one_","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}], "type":"tuple"}]`, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002", |
||||
unpacked: struct { |
||||
IntOne *big.Int |
||||
Intone *big.Int |
||||
}{big.NewInt(1), big.NewInt(2)}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "string"}]`, |
||||
unpacked: "foobar", |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000006" + |
||||
"666f6f6261720000000000000000000000000000000000000000000000000000", |
||||
}, |
||||
{ |
||||
def: `[{"type": "string[]"}]`, |
||||
unpacked: []string{"hello", "foobar"}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
||||
"0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
|
||||
"666f6f6261720000000000000000000000000000000000000000000000000000", // str[1]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "string[2]"}]`, |
||||
unpacked: [2]string{"hello", "foobar"}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0
|
||||
"0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
|
||||
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
|
||||
"666f6f6261720000000000000000000000000000000000000000000000000000", // str[1]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32[][]"}]`, |
||||
unpacked: [][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // array[1][2]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32[][2]"}]`, |
||||
unpacked: [2][][32]byte{{{1}, {2}}, {{3}, {4}, {5}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
||||
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // array[1][2]
|
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32[3][2]"}]`, |
||||
unpacked: [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}, |
||||
packed: "0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // array[1][2]
|
||||
}, |
||||
{ |
||||
// static tuple
|
||||
def: `[{"components": [{"name":"a","type":"int64"},
|
||||
{"name":"b","type":"int256"},
|
||||
{"name":"c","type":"int256"},
|
||||
{"name":"d","type":"bool"}, |
||||
{"name":"e","type":"bytes32[3][2]"}], "type":"tuple"}]`, |
||||
unpacked: struct { |
||||
A int64 |
||||
B *big.Int |
||||
C *big.Int |
||||
D bool |
||||
E [2][3][32]byte |
||||
}{1, big.NewInt(1), big.NewInt(-1), true, [2][3][32]byte{{{1}, {2}, {3}}, {{3}, {4}, {5}}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000001" + // struct[a]
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c]
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[d]
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][0]
|
||||
"0200000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][1]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][2]
|
||||
"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][0]
|
||||
"0400000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][1]
|
||||
"0500000000000000000000000000000000000000000000000000000000000000", // struct[e] array[1][2]
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name":"a","type":"string"},
|
||||
{"name":"b","type":"int64"},
|
||||
{"name":"c","type":"bytes"},
|
||||
{"name":"d","type":"string[]"}, |
||||
{"name":"e","type":"int256[]"}, |
||||
{"name":"f","type":"address[]"}], "type":"tuple"}]`, |
||||
unpacked: struct { |
||||
A string |
||||
B int64 |
||||
C []byte |
||||
D []string |
||||
E []*big.Int |
||||
F []common.Address |
||||
}{"foobar", 1, []byte{1}, []string{"foo", "bar"}, []*big.Int{big.NewInt(1), big.NewInt(-1)}, []common.Address{{1}, {2}}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a
|
||||
"00000000000000000000000000000000000000000000000000000000000000c0" + // struct[a] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
|
||||
"0000000000000000000000000000000000000000000000000000000000000100" + // struct[c] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000140" + // struct[d] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000220" + // struct[e] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000280" + // struct[f] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000006" + // struct[a] length
|
||||
"666f6f6261720000000000000000000000000000000000000000000000000000" + // struct[a] "foobar"
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[c] length
|
||||
"0100000000000000000000000000000000000000000000000000000000000000" + // []byte{1}
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[d] length
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // foo offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000080" + // bar offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // foo length
|
||||
"666f6f0000000000000000000000000000000000000000000000000000000000" + // foo
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // bar offset
|
||||
"6261720000000000000000000000000000000000000000000000000000000000" + // bar
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[e] length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // 1
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // -1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[f] length
|
||||
"0000000000000000000000000100000000000000000000000000000000000000" + // common.Address{1}
|
||||
"0000000000000000000000000200000000000000000000000000000000000000", // common.Address{2}
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{ "type": "tuple","components": [{"name": "a","type": "uint256"},
|
||||
{"name": "b","type": "uint256[]"}],
|
||||
"name": "a","type": "tuple"}, |
||||
{"name": "b","type": "uint256[]"}], "type": "tuple"}]`, |
||||
unpacked: struct { |
||||
A struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
} |
||||
B []*big.Int |
||||
}{ |
||||
A: struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
}{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(2)}}, |
||||
B: []*big.Int{big.NewInt(1), big.NewInt(2)}}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // a offset
|
||||
"00000000000000000000000000000000000000000000000000000000000000e0" + // b offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // a.a value
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // a.b offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // a.b length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // a.b[0] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // a.b[1] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // b length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // b[0] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // b[1] value
|
||||
}, |
||||
|
||||
{ |
||||
def: `[{"components": [{"name": "a","type": "int256"},
|
||||
{"name": "b","type": "int256[]"}],
|
||||
"name": "a","type": "tuple[]"}]`, |
||||
unpacked: []struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
}{ |
||||
{big.NewInt(-1), []*big.Int{big.NewInt(1), big.NewInt(3)}}, |
||||
{big.NewInt(1), []*big.Int{big.NewInt(2), big.NewInt(-1)}}, |
||||
}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple length
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
|
||||
"00000000000000000000000000000000000000000000000000000000000000e0" + // tuple[1] offset
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0].B offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].B length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].B[0] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // tuple[0].B[1] value
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A
|
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[1].B offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B length
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B[0] value
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // tuple[1].B[1] value
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name": "a","type": "int256"},
|
||||
{"name": "b","type": "int256"}],
|
||||
"name": "a","type": "tuple[2]"}]`, |
||||
unpacked: [2]struct { |
||||
A *big.Int |
||||
B *big.Int |
||||
}{ |
||||
{big.NewInt(-1), big.NewInt(1)}, |
||||
{big.NewInt(1), big.NewInt(-1)}, |
||||
}, |
||||
packed: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].a
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].b
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].a
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // tuple[1].b
|
||||
}, |
||||
{ |
||||
def: `[{"components": [{"name": "a","type": "int256[]"}], |
||||
"name": "a","type": "tuple[2]"}]`, |
||||
unpacked: [2]struct { |
||||
A []*big.Int |
||||
}{ |
||||
{[]*big.Int{big.NewInt(-1), big.NewInt(1)}}, |
||||
{[]*big.Int{big.NewInt(1), big.NewInt(-1)}}, |
||||
}, |
||||
packed: "0000000000000000000000000000000000000000000000000000000000000020" + |
||||
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
|
||||
"00000000000000000000000000000000000000000000000000000000000000c0" + // tuple[1] offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[0].A offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].A length
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A[0]
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].A[1]
|
||||
"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[1].A offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].A length
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A[0]
|
||||
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", // tuple[1].A[1]
|
||||
}, |
||||
} |
@ -1,264 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
"reflect" |
||||
"strings" |
||||
) |
||||
|
||||
// ConvertType converts an interface of a runtime type into a interface of the
|
||||
// given type, e.g. turn this code:
|
||||
//
|
||||
// var fields []reflect.StructField
|
||||
//
|
||||
// fields = append(fields, reflect.StructField{
|
||||
// Name: "X",
|
||||
// Type: reflect.TypeOf(new(big.Int)),
|
||||
// Tag: reflect.StructTag("json:\"" + "x" + "\""),
|
||||
// }
|
||||
//
|
||||
// into:
|
||||
//
|
||||
// type TupleT struct { X *big.Int }
|
||||
func ConvertType(in interface{}, proto interface{}) interface{} { |
||||
protoType := reflect.TypeOf(proto) |
||||
if reflect.TypeOf(in).ConvertibleTo(protoType) { |
||||
return reflect.ValueOf(in).Convert(protoType).Interface() |
||||
} |
||||
// Use set as a last ditch effort
|
||||
if err := set(reflect.ValueOf(proto), reflect.ValueOf(in)); err != nil { |
||||
panic(err) |
||||
} |
||||
return proto |
||||
} |
||||
|
||||
// indirect recursively dereferences the value until it either gets the value
|
||||
// or finds a big.Int
|
||||
func indirect(v reflect.Value) reflect.Value { |
||||
if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeOf(big.Int{}) { |
||||
return indirect(v.Elem()) |
||||
} |
||||
return v |
||||
} |
||||
|
||||
// reflectIntType returns the reflect using the given size and
|
||||
// unsignedness.
|
||||
func reflectIntType(unsigned bool, size int) reflect.Type { |
||||
if unsigned { |
||||
switch size { |
||||
case 8: |
||||
return reflect.TypeOf(uint8(0)) |
||||
case 16: |
||||
return reflect.TypeOf(uint16(0)) |
||||
case 32: |
||||
return reflect.TypeOf(uint32(0)) |
||||
case 64: |
||||
return reflect.TypeOf(uint64(0)) |
||||
} |
||||
} |
||||
switch size { |
||||
case 8: |
||||
return reflect.TypeOf(int8(0)) |
||||
case 16: |
||||
return reflect.TypeOf(int16(0)) |
||||
case 32: |
||||
return reflect.TypeOf(int32(0)) |
||||
case 64: |
||||
return reflect.TypeOf(int64(0)) |
||||
} |
||||
return reflect.TypeOf(&big.Int{}) |
||||
} |
||||
|
||||
// mustArrayToByteSlice creates a new byte slice with the exact same size as value
|
||||
// and copies the bytes in value to the new slice.
|
||||
func mustArrayToByteSlice(value reflect.Value) reflect.Value { |
||||
slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len()) |
||||
reflect.Copy(slice, value) |
||||
return slice |
||||
} |
||||
|
||||
// set attempts to assign src to dst by either setting, copying or otherwise.
|
||||
//
|
||||
// set is a bit more lenient when it comes to assignment and doesn't force an as
|
||||
// strict ruleset as bare `reflect` does.
|
||||
func set(dst, src reflect.Value) error { |
||||
dstType, srcType := dst.Type(), src.Type() |
||||
switch { |
||||
case dstType.Kind() == reflect.Interface && dst.Elem().IsValid() && (dst.Elem().Type().Kind() == reflect.Ptr || dst.Elem().CanSet()): |
||||
return set(dst.Elem(), src) |
||||
case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeOf(big.Int{}): |
||||
return set(dst.Elem(), src) |
||||
case srcType.AssignableTo(dstType) && dst.CanSet(): |
||||
dst.Set(src) |
||||
case dstType.Kind() == reflect.Slice && srcType.Kind() == reflect.Slice && dst.CanSet(): |
||||
return setSlice(dst, src) |
||||
case dstType.Kind() == reflect.Array: |
||||
return setArray(dst, src) |
||||
case dstType.Kind() == reflect.Struct: |
||||
return setStruct(dst, src) |
||||
default: |
||||
return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type()) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// setSlice attempts to assign src to dst when slices are not assignable by default
|
||||
// e.g. src: [][]byte -> dst: [][15]byte
|
||||
// setSlice ignores if we cannot copy all of src' elements.
|
||||
func setSlice(dst, src reflect.Value) error { |
||||
slice := reflect.MakeSlice(dst.Type(), src.Len(), src.Len()) |
||||
for i := 0; i < src.Len(); i++ { |
||||
if err := set(slice.Index(i), src.Index(i)); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
if dst.CanSet() { |
||||
dst.Set(slice) |
||||
return nil |
||||
} |
||||
return errors.New("Cannot set slice, destination not settable") |
||||
} |
||||
|
||||
func setArray(dst, src reflect.Value) error { |
||||
if src.Kind() == reflect.Ptr { |
||||
return set(dst, indirect(src)) |
||||
} |
||||
array := reflect.New(dst.Type()).Elem() |
||||
min := src.Len() |
||||
if src.Len() > dst.Len() { |
||||
min = dst.Len() |
||||
} |
||||
for i := 0; i < min; i++ { |
||||
if err := set(array.Index(i), src.Index(i)); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
if dst.CanSet() { |
||||
dst.Set(array) |
||||
return nil |
||||
} |
||||
return errors.New("Cannot set array, destination not settable") |
||||
} |
||||
|
||||
func setStruct(dst, src reflect.Value) error { |
||||
for i := 0; i < src.NumField(); i++ { |
||||
srcField := src.Field(i) |
||||
dstField := dst.Field(i) |
||||
if !dstField.IsValid() || !srcField.IsValid() { |
||||
return fmt.Errorf("Could not find src field: %v value: %v in destination", srcField.Type().Name(), srcField) |
||||
} |
||||
if err := set(dstField, srcField); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// mapArgNamesToStructFields maps a slice of argument names to struct fields.
|
||||
//
|
||||
// first round: for each Exportable field that contains a `abi:""` tag and this field name
|
||||
// exists in the given argument name list, pair them together.
|
||||
//
|
||||
// second round: for each argument name that has not been already linked, find what
|
||||
// variable is expected to be mapped into, if it exists and has not been used, pair them.
|
||||
//
|
||||
// Note this function assumes the given value is a struct value.
|
||||
func mapArgNamesToStructFields(argNames []string, value reflect.Value) (map[string]string, error) { |
||||
typ := value.Type() |
||||
|
||||
abi2struct := make(map[string]string) |
||||
struct2abi := make(map[string]string) |
||||
|
||||
// first round ~~~
|
||||
for i := 0; i < typ.NumField(); i++ { |
||||
structFieldName := typ.Field(i).Name |
||||
|
||||
// skip private struct fields.
|
||||
if structFieldName[:1] != strings.ToUpper(structFieldName[:1]) { |
||||
continue |
||||
} |
||||
// skip fields that have no abi:"" tag.
|
||||
tagName, ok := typ.Field(i).Tag.Lookup("abi") |
||||
if !ok { |
||||
continue |
||||
} |
||||
// check if tag is empty.
|
||||
if tagName == "" { |
||||
return nil, fmt.Errorf("struct: abi tag in '%s' is empty", structFieldName) |
||||
} |
||||
// check which argument field matches with the abi tag.
|
||||
found := false |
||||
for _, arg := range argNames { |
||||
if arg == tagName { |
||||
if abi2struct[arg] != "" { |
||||
return nil, fmt.Errorf("struct: abi tag in '%s' already mapped", structFieldName) |
||||
} |
||||
// pair them
|
||||
abi2struct[arg] = structFieldName |
||||
struct2abi[structFieldName] = arg |
||||
found = true |
||||
} |
||||
} |
||||
// check if this tag has been mapped.
|
||||
if !found { |
||||
return nil, fmt.Errorf("struct: abi tag '%s' defined but not found in abi", tagName) |
||||
} |
||||
} |
||||
|
||||
// second round ~~~
|
||||
for _, argName := range argNames { |
||||
structFieldName := ToCamelCase(argName) |
||||
|
||||
if structFieldName == "" { |
||||
return nil, fmt.Errorf("abi: purely underscored output cannot unpack to struct") |
||||
} |
||||
|
||||
// this abi has already been paired, skip it... unless there exists another, yet unassigned
|
||||
// struct field with the same field name. If so, raise an error:
|
||||
// abi: [ { "name": "value" } ]
|
||||
// struct { Value *big.Int , Value1 *big.Int `abi:"value"`}
|
||||
if abi2struct[argName] != "" { |
||||
if abi2struct[argName] != structFieldName && |
||||
struct2abi[structFieldName] == "" && |
||||
value.FieldByName(structFieldName).IsValid() { |
||||
return nil, fmt.Errorf("abi: multiple variables maps to the same abi field '%s'", argName) |
||||
} |
||||
continue |
||||
} |
||||
|
||||
// return an error if this struct field has already been paired.
|
||||
if struct2abi[structFieldName] != "" { |
||||
return nil, fmt.Errorf("abi: multiple outputs mapping to the same struct field '%s'", structFieldName) |
||||
} |
||||
|
||||
if value.FieldByName(structFieldName).IsValid() { |
||||
// pair them
|
||||
abi2struct[argName] = structFieldName |
||||
struct2abi[structFieldName] = argName |
||||
} else { |
||||
// not paired, but annotate as used, to detect cases like
|
||||
// abi : [ { "name": "value" }, { "name": "_value" } ]
|
||||
// struct { Value *big.Int }
|
||||
struct2abi[structFieldName] = argName |
||||
} |
||||
} |
||||
return abi2struct, nil |
||||
} |
@ -1,261 +0,0 @@ |
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"math/big" |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
type reflectTest struct { |
||||
name string |
||||
args []string |
||||
struc interface{} |
||||
want map[string]string |
||||
err string |
||||
} |
||||
|
||||
var reflectTests = []reflectTest{ |
||||
{ |
||||
name: "OneToOneCorrespondence", |
||||
args: []string{"fieldA"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldA": "FieldA", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "MissingFieldsInStruct", |
||||
args: []string{"fieldA", "fieldB"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldA": "FieldA", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "MoreFieldsInStructThanArgs", |
||||
args: []string{"fieldA"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
FieldB int |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldA": "FieldA", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "MissingFieldInArgs", |
||||
args: []string{"fieldA"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
FieldB int `abi:"fieldB"` |
||||
}{}, |
||||
err: "struct: abi tag 'fieldB' defined but not found in abi", |
||||
}, |
||||
{ |
||||
name: "NoAbiDescriptor", |
||||
args: []string{"fieldA"}, |
||||
struc: struct { |
||||
FieldA int |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldA": "FieldA", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "NoArgs", |
||||
args: []string{}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
}{}, |
||||
err: "struct: abi tag 'fieldA' defined but not found in abi", |
||||
}, |
||||
{ |
||||
name: "DifferentName", |
||||
args: []string{"fieldB"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldB"` |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldB": "FieldA", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "DifferentName", |
||||
args: []string{"fieldB"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldB"` |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldB": "FieldA", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "MultipleFields", |
||||
args: []string{"fieldA", "fieldB"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
FieldB int `abi:"fieldB"` |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldA": "FieldA", |
||||
"fieldB": "FieldB", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "MultipleFieldsABIMissing", |
||||
args: []string{"fieldA", "fieldB"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldA"` |
||||
FieldB int |
||||
}{}, |
||||
want: map[string]string{ |
||||
"fieldA": "FieldA", |
||||
"fieldB": "FieldB", |
||||
}, |
||||
}, |
||||
{ |
||||
name: "NameConflict", |
||||
args: []string{"fieldB"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldB"` |
||||
FieldB int |
||||
}{}, |
||||
err: "abi: multiple variables maps to the same abi field 'fieldB'", |
||||
}, |
||||
{ |
||||
name: "Underscored", |
||||
args: []string{"_"}, |
||||
struc: struct { |
||||
FieldA int |
||||
}{}, |
||||
err: "abi: purely underscored output cannot unpack to struct", |
||||
}, |
||||
{ |
||||
name: "DoubleMapping", |
||||
args: []string{"fieldB", "fieldC", "fieldA"}, |
||||
struc: struct { |
||||
FieldA int `abi:"fieldC"` |
||||
FieldB int |
||||
}{}, |
||||
err: "abi: multiple outputs mapping to the same struct field 'FieldA'", |
||||
}, |
||||
{ |
||||
name: "AlreadyMapped", |
||||
args: []string{"fieldB", "fieldB"}, |
||||
struc: struct { |
||||
FieldB int `abi:"fieldB"` |
||||
}{}, |
||||
err: "struct: abi tag in 'FieldB' already mapped", |
||||
}, |
||||
} |
||||
|
||||
func TestReflectNameToStruct(t *testing.T) { |
||||
for _, test := range reflectTests { |
||||
t.Run(test.name, func(t *testing.T) { |
||||
m, err := mapArgNamesToStructFields(test.args, reflect.ValueOf(test.struc)) |
||||
if len(test.err) > 0 { |
||||
if err == nil || err.Error() != test.err { |
||||
t.Fatalf("Invalid error: expected %v, got %v", test.err, err) |
||||
} |
||||
} else { |
||||
if err != nil { |
||||
t.Fatalf("Unexpected error: %v", err) |
||||
} |
||||
for fname := range test.want { |
||||
if m[fname] != test.want[fname] { |
||||
t.Fatalf("Incorrect value for field %s: expected %v, got %v", fname, test.want[fname], m[fname]) |
||||
} |
||||
} |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestConvertType(t *testing.T) { |
||||
// Test Basic Struct
|
||||
type T struct { |
||||
X *big.Int |
||||
Y *big.Int |
||||
} |
||||
// Create on-the-fly structure
|
||||
var fields []reflect.StructField |
||||
fields = append(fields, reflect.StructField{ |
||||
Name: "X", |
||||
Type: reflect.TypeOf(new(big.Int)), |
||||
Tag: "json:\"" + "x" + "\"", |
||||
}) |
||||
fields = append(fields, reflect.StructField{ |
||||
Name: "Y", |
||||
Type: reflect.TypeOf(new(big.Int)), |
||||
Tag: "json:\"" + "y" + "\"", |
||||
}) |
||||
val := reflect.New(reflect.StructOf(fields)) |
||||
val.Elem().Field(0).Set(reflect.ValueOf(big.NewInt(1))) |
||||
val.Elem().Field(1).Set(reflect.ValueOf(big.NewInt(2))) |
||||
// ConvertType
|
||||
out := *ConvertType(val.Interface(), new(T)).(*T) |
||||
if out.X.Cmp(big.NewInt(1)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out.X, big.NewInt(1)) |
||||
} |
||||
if out.Y.Cmp(big.NewInt(2)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out.Y, big.NewInt(2)) |
||||
} |
||||
// Slice Type
|
||||
val2 := reflect.MakeSlice(reflect.SliceOf(reflect.StructOf(fields)), 2, 2) |
||||
val2.Index(0).Field(0).Set(reflect.ValueOf(big.NewInt(1))) |
||||
val2.Index(0).Field(1).Set(reflect.ValueOf(big.NewInt(2))) |
||||
val2.Index(1).Field(0).Set(reflect.ValueOf(big.NewInt(3))) |
||||
val2.Index(1).Field(1).Set(reflect.ValueOf(big.NewInt(4))) |
||||
out2 := *ConvertType(val2.Interface(), new([]T)).(*[]T) |
||||
if out2[0].X.Cmp(big.NewInt(1)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out2[0].X, big.NewInt(1)) |
||||
} |
||||
if out2[0].Y.Cmp(big.NewInt(2)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out2[1].Y, big.NewInt(2)) |
||||
} |
||||
if out2[1].X.Cmp(big.NewInt(3)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out2[0].X, big.NewInt(1)) |
||||
} |
||||
if out2[1].Y.Cmp(big.NewInt(4)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out2[1].Y, big.NewInt(2)) |
||||
} |
||||
// Array Type
|
||||
val3 := reflect.New(reflect.ArrayOf(2, reflect.StructOf(fields))) |
||||
val3.Elem().Index(0).Field(0).Set(reflect.ValueOf(big.NewInt(1))) |
||||
val3.Elem().Index(0).Field(1).Set(reflect.ValueOf(big.NewInt(2))) |
||||
val3.Elem().Index(1).Field(0).Set(reflect.ValueOf(big.NewInt(3))) |
||||
val3.Elem().Index(1).Field(1).Set(reflect.ValueOf(big.NewInt(4))) |
||||
out3 := *ConvertType(val3.Interface(), new([2]T)).(*[2]T) |
||||
if out3[0].X.Cmp(big.NewInt(1)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out3[0].X, big.NewInt(1)) |
||||
} |
||||
if out3[0].Y.Cmp(big.NewInt(2)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out3[1].Y, big.NewInt(2)) |
||||
} |
||||
if out3[1].X.Cmp(big.NewInt(3)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out3[0].X, big.NewInt(1)) |
||||
} |
||||
if out3[1].Y.Cmp(big.NewInt(4)) != 0 { |
||||
t.Errorf("ConvertType failed, got %v want %v", out3[1].Y, big.NewInt(2)) |
||||
} |
||||
} |
@ -1,176 +0,0 @@ |
||||
// Copyright 2022 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"fmt" |
||||
) |
||||
|
||||
type SelectorMarshaling struct { |
||||
Name string `json:"name"` |
||||
Type string `json:"type"` |
||||
Inputs []ArgumentMarshaling `json:"inputs"` |
||||
} |
||||
|
||||
func isDigit(c byte) bool { |
||||
return c >= '0' && c <= '9' |
||||
} |
||||
|
||||
func isAlpha(c byte) bool { |
||||
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') |
||||
} |
||||
|
||||
func isIdentifierSymbol(c byte) bool { |
||||
return c == '$' || c == '_' |
||||
} |
||||
|
||||
func parseToken(unescapedSelector string, isIdent bool) (string, string, error) { |
||||
if len(unescapedSelector) == 0 { |
||||
return "", "", fmt.Errorf("empty token") |
||||
} |
||||
firstChar := unescapedSelector[0] |
||||
position := 1 |
||||
if !(isAlpha(firstChar) || (isIdent && isIdentifierSymbol(firstChar))) { |
||||
return "", "", fmt.Errorf("invalid token start: %c", firstChar) |
||||
} |
||||
for position < len(unescapedSelector) { |
||||
char := unescapedSelector[position] |
||||
if !(isAlpha(char) || isDigit(char) || (isIdent && isIdentifierSymbol(char))) { |
||||
break |
||||
} |
||||
position++ |
||||
} |
||||
return unescapedSelector[:position], unescapedSelector[position:], nil |
||||
} |
||||
|
||||
func parseIdentifier(unescapedSelector string) (string, string, error) { |
||||
return parseToken(unescapedSelector, true) |
||||
} |
||||
|
||||
func parseElementaryType(unescapedSelector string) (string, string, error) { |
||||
parsedType, rest, err := parseToken(unescapedSelector, false) |
||||
if err != nil { |
||||
return "", "", fmt.Errorf("failed to parse elementary type: %v", err) |
||||
} |
||||
// handle arrays
|
||||
for len(rest) > 0 && rest[0] == '[' { |
||||
parsedType = parsedType + string(rest[0]) |
||||
rest = rest[1:] |
||||
for len(rest) > 0 && isDigit(rest[0]) { |
||||
parsedType = parsedType + string(rest[0]) |
||||
rest = rest[1:] |
||||
} |
||||
if len(rest) == 0 || rest[0] != ']' { |
||||
return "", "", fmt.Errorf("failed to parse array: expected ']', got %c", unescapedSelector[0]) |
||||
} |
||||
parsedType = parsedType + string(rest[0]) |
||||
rest = rest[1:] |
||||
} |
||||
return parsedType, rest, nil |
||||
} |
||||
|
||||
func parseCompositeType(unescapedSelector string) ([]interface{}, string, error) { |
||||
if len(unescapedSelector) == 0 || unescapedSelector[0] != '(' { |
||||
return nil, "", fmt.Errorf("expected '(', got %c", unescapedSelector[0]) |
||||
} |
||||
parsedType, rest, err := parseType(unescapedSelector[1:]) |
||||
if err != nil { |
||||
return nil, "", fmt.Errorf("failed to parse type: %v", err) |
||||
} |
||||
result := []interface{}{parsedType} |
||||
for len(rest) > 0 && rest[0] != ')' { |
||||
parsedType, rest, err = parseType(rest[1:]) |
||||
if err != nil { |
||||
return nil, "", fmt.Errorf("failed to parse type: %v", err) |
||||
} |
||||
result = append(result, parsedType) |
||||
} |
||||
if len(rest) == 0 || rest[0] != ')' { |
||||
return nil, "", fmt.Errorf("expected ')', got '%s'", rest) |
||||
} |
||||
if len(rest) >= 3 && rest[1] == '[' && rest[2] == ']' { |
||||
return append(result, "[]"), rest[3:], nil |
||||
} |
||||
return result, rest[1:], nil |
||||
} |
||||
|
||||
func parseType(unescapedSelector string) (interface{}, string, error) { |
||||
if len(unescapedSelector) == 0 { |
||||
return nil, "", fmt.Errorf("empty type") |
||||
} |
||||
if unescapedSelector[0] == '(' { |
||||
return parseCompositeType(unescapedSelector) |
||||
} else { |
||||
return parseElementaryType(unescapedSelector) |
||||
} |
||||
} |
||||
|
||||
func assembleArgs(args []interface{}) ([]ArgumentMarshaling, error) { |
||||
arguments := make([]ArgumentMarshaling, 0) |
||||
for i, arg := range args { |
||||
// generate dummy name to avoid unmarshal issues
|
||||
name := fmt.Sprintf("name%d", i) |
||||
if s, ok := arg.(string); ok { |
||||
arguments = append(arguments, ArgumentMarshaling{name, s, s, nil, false}) |
||||
} else if components, ok := arg.([]interface{}); ok { |
||||
subArgs, err := assembleArgs(components) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("failed to assemble components: %v", err) |
||||
} |
||||
tupleType := "tuple" |
||||
if len(subArgs) != 0 && subArgs[len(subArgs)-1].Type == "[]" { |
||||
subArgs = subArgs[:len(subArgs)-1] |
||||
tupleType = "tuple[]" |
||||
} |
||||
arguments = append(arguments, ArgumentMarshaling{name, tupleType, tupleType, subArgs, false}) |
||||
} else { |
||||
return nil, fmt.Errorf("failed to assemble args: unexpected type %T", arg) |
||||
} |
||||
} |
||||
return arguments, nil |
||||
} |
||||
|
||||
// ParseSelector converts a method selector into a struct that can be JSON encoded
|
||||
// and consumed by other functions in this package.
|
||||
// Note, although uppercase letters are not part of the ABI spec, this function
|
||||
// still accepts it as the general format is valid.
|
||||
func ParseSelector(unescapedSelector string) (SelectorMarshaling, error) { |
||||
name, rest, err := parseIdentifier(unescapedSelector) |
||||
if err != nil { |
||||
return SelectorMarshaling{}, fmt.Errorf("failed to parse selector '%s': %v", unescapedSelector, err) |
||||
} |
||||
args := []interface{}{} |
||||
if len(rest) >= 2 && rest[0] == '(' && rest[1] == ')' { |
||||
rest = rest[2:] |
||||
} else { |
||||
args, rest, err = parseCompositeType(rest) |
||||
if err != nil { |
||||
return SelectorMarshaling{}, fmt.Errorf("failed to parse selector '%s': %v", unescapedSelector, err) |
||||
} |
||||
} |
||||
if len(rest) > 0 { |
||||
return SelectorMarshaling{}, fmt.Errorf("failed to parse selector '%s': unexpected string '%s'", unescapedSelector, rest) |
||||
} |
||||
|
||||
// Reassemble the fake ABI and construct the JSON
|
||||
fakeArgs, err := assembleArgs(args) |
||||
if err != nil { |
||||
return SelectorMarshaling{}, fmt.Errorf("failed to parse selector: %v", err) |
||||
} |
||||
|
||||
return SelectorMarshaling{name, "function", fakeArgs}, nil |
||||
} |
@ -1,79 +0,0 @@ |
||||
// Copyright 2022 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"fmt" |
||||
"log" |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
func TestParseSelector(t *testing.T) { |
||||
mkType := func(types ...interface{}) []ArgumentMarshaling { |
||||
var result []ArgumentMarshaling |
||||
for i, typeOrComponents := range types { |
||||
name := fmt.Sprintf("name%d", i) |
||||
if typeName, ok := typeOrComponents.(string); ok { |
||||
result = append(result, ArgumentMarshaling{name, typeName, typeName, nil, false}) |
||||
} else if components, ok := typeOrComponents.([]ArgumentMarshaling); ok { |
||||
result = append(result, ArgumentMarshaling{name, "tuple", "tuple", components, false}) |
||||
} else if components, ok := typeOrComponents.([][]ArgumentMarshaling); ok { |
||||
result = append(result, ArgumentMarshaling{name, "tuple[]", "tuple[]", components[0], false}) |
||||
} else { |
||||
log.Fatalf("unexpected type %T", typeOrComponents) |
||||
} |
||||
} |
||||
return result |
||||
} |
||||
tests := []struct { |
||||
input string |
||||
name string |
||||
args []ArgumentMarshaling |
||||
}{ |
||||
{"noargs()", "noargs", []ArgumentMarshaling{}}, |
||||
{"simple(uint256,uint256,uint256)", "simple", mkType("uint256", "uint256", "uint256")}, |
||||
{"other(uint256,address)", "other", mkType("uint256", "address")}, |
||||
{"withArray(uint256[],address[2],uint8[4][][5])", "withArray", mkType("uint256[]", "address[2]", "uint8[4][][5]")}, |
||||
{"singleNest(bytes32,uint8,(uint256,uint256),address)", "singleNest", mkType("bytes32", "uint8", mkType("uint256", "uint256"), "address")}, |
||||
{"multiNest(address,(uint256[],uint256),((address,bytes32),uint256))", "multiNest", |
||||
mkType("address", mkType("uint256[]", "uint256"), mkType(mkType("address", "bytes32"), "uint256"))}, |
||||
{"arrayNest((uint256,uint256)[],bytes32)", "arrayNest", mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, "bytes32")}, |
||||
{"multiArrayNest((uint256,uint256)[],(uint256,uint256)[])", "multiArrayNest", |
||||
mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, [][]ArgumentMarshaling{mkType("uint256", "uint256")})}, |
||||
{"singleArrayNestAndArray((uint256,uint256)[],bytes32[])", "singleArrayNestAndArray", |
||||
mkType([][]ArgumentMarshaling{mkType("uint256", "uint256")}, "bytes32[]")}, |
||||
{"singleArrayNestWithArrayAndArray((uint256[],address[2],uint8[4][][5])[],bytes32[])", "singleArrayNestWithArrayAndArray", |
||||
mkType([][]ArgumentMarshaling{mkType("uint256[]", "address[2]", "uint8[4][][5]")}, "bytes32[]")}, |
||||
} |
||||
for i, tt := range tests { |
||||
selector, err := ParseSelector(tt.input) |
||||
if err != nil { |
||||
t.Errorf("test %d: failed to parse selector '%v': %v", i, tt.input, err) |
||||
} |
||||
if selector.Name != tt.name { |
||||
t.Errorf("test %d: unexpected function name: '%s' != '%s'", i, selector.Name, tt.name) |
||||
} |
||||
|
||||
if selector.Type != "function" { |
||||
t.Errorf("test %d: unexpected type: '%s' != '%s'", i, selector.Type, "function") |
||||
} |
||||
if !reflect.DeepEqual(selector.Inputs, tt.args) { |
||||
t.Errorf("test %d: unexpected args: '%v' != '%v'", i, selector.Inputs, tt.args) |
||||
} |
||||
} |
||||
} |
@ -1,173 +0,0 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
"reflect" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// MakeTopics converts a filter query argument list into a filter topic set.
|
||||
func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) { |
||||
topics := make([][]common.Hash, len(query)) |
||||
for i, filter := range query { |
||||
for _, rule := range filter { |
||||
var topic common.Hash |
||||
|
||||
// Try to generate the topic based on simple types
|
||||
switch rule := rule.(type) { |
||||
case common.Hash: |
||||
copy(topic[:], rule[:]) |
||||
case common.Address: |
||||
copy(topic[common.HashLength-common.AddressLength:], rule[:]) |
||||
case *big.Int: |
||||
blob := rule.Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case bool: |
||||
if rule { |
||||
topic[common.HashLength-1] = 1 |
||||
} |
||||
case int8: |
||||
copy(topic[:], genIntType(int64(rule), 1)) |
||||
case int16: |
||||
copy(topic[:], genIntType(int64(rule), 2)) |
||||
case int32: |
||||
copy(topic[:], genIntType(int64(rule), 4)) |
||||
case int64: |
||||
copy(topic[:], genIntType(rule, 8)) |
||||
case uint8: |
||||
blob := new(big.Int).SetUint64(uint64(rule)).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case uint16: |
||||
blob := new(big.Int).SetUint64(uint64(rule)).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case uint32: |
||||
blob := new(big.Int).SetUint64(uint64(rule)).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case uint64: |
||||
blob := new(big.Int).SetUint64(rule).Bytes() |
||||
copy(topic[common.HashLength-len(blob):], blob) |
||||
case string: |
||||
hash := crypto.Keccak256Hash([]byte(rule)) |
||||
copy(topic[:], hash[:]) |
||||
case []byte: |
||||
hash := crypto.Keccak256Hash(rule) |
||||
copy(topic[:], hash[:]) |
||||
|
||||
default: |
||||
// todo(rjl493456442) according solidity documentation, indexed event
|
||||
// parameters that are not value types i.e. arrays and structs are not
|
||||
// stored directly but instead a keccak256-hash of an encoding is stored.
|
||||
//
|
||||
// We only convert stringS and bytes to hash, still need to deal with
|
||||
// array(both fixed-size and dynamic-size) and struct.
|
||||
|
||||
// Attempt to generate the topic from funky types
|
||||
val := reflect.ValueOf(rule) |
||||
switch { |
||||
// static byte array
|
||||
case val.Kind() == reflect.Array && reflect.TypeOf(rule).Elem().Kind() == reflect.Uint8: |
||||
reflect.Copy(reflect.ValueOf(topic[:val.Len()]), val) |
||||
default: |
||||
return nil, fmt.Errorf("unsupported indexed type: %T", rule) |
||||
} |
||||
} |
||||
topics[i] = append(topics[i], topic) |
||||
} |
||||
} |
||||
return topics, nil |
||||
} |
||||
|
||||
func genIntType(rule int64, size uint) []byte { |
||||
var topic [common.HashLength]byte |
||||
if rule < 0 { |
||||
// if a rule is negative, we need to put it into two's complement.
|
||||
// extended to common.HashLength bytes.
|
||||
topic = [common.HashLength]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} |
||||
} |
||||
for i := uint(0); i < size; i++ { |
||||
topic[common.HashLength-i-1] = byte(rule >> (i * 8)) |
||||
} |
||||
return topic[:] |
||||
} |
||||
|
||||
// ParseTopics converts the indexed topic fields into actual log field values.
|
||||
func ParseTopics(out interface{}, fields Arguments, topics []common.Hash) error { |
||||
return parseTopicWithSetter(fields, topics, |
||||
func(arg Argument, reconstr interface{}) { |
||||
field := reflect.ValueOf(out).Elem().FieldByName(ToCamelCase(arg.Name)) |
||||
field.Set(reflect.ValueOf(reconstr)) |
||||
}) |
||||
} |
||||
|
||||
// ParseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs.
|
||||
func ParseTopicsIntoMap(out map[string]interface{}, fields Arguments, topics []common.Hash) error { |
||||
return parseTopicWithSetter(fields, topics, |
||||
func(arg Argument, reconstr interface{}) { |
||||
out[arg.Name] = reconstr |
||||
}) |
||||
} |
||||
|
||||
// parseTopicWithSetter converts the indexed topic field-value pairs and stores them using the
|
||||
// provided set function.
|
||||
//
|
||||
// Note, dynamic types cannot be reconstructed since they get mapped to Keccak256
|
||||
// hashes as the topic value!
|
||||
func parseTopicWithSetter(fields Arguments, topics []common.Hash, setter func(Argument, interface{})) error { |
||||
// Sanity check that the fields and topics match up
|
||||
if len(fields) != len(topics) { |
||||
return errors.New("topic/field count mismatch") |
||||
} |
||||
// Iterate over all the fields and reconstruct them from topics
|
||||
for i, arg := range fields { |
||||
if !arg.Indexed { |
||||
return errors.New("non-indexed field in topic reconstruction") |
||||
} |
||||
var reconstr interface{} |
||||
switch arg.Type.T { |
||||
case TupleTy: |
||||
return errors.New("tuple type in topic reconstruction") |
||||
case StringTy, BytesTy, SliceTy, ArrayTy: |
||||
// Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash
|
||||
// whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash
|
||||
reconstr = topics[i] |
||||
case FunctionTy: |
||||
if garbage := binary.BigEndian.Uint64(topics[i][0:8]); garbage != 0 { |
||||
return fmt.Errorf("bind: got improperly encoded function type, got %v", topics[i].Bytes()) |
||||
} |
||||
var tmp [24]byte |
||||
copy(tmp[:], topics[i][8:32]) |
||||
reconstr = tmp |
||||
default: |
||||
var err error |
||||
reconstr, err = toGoType(0, arg.Type, topics[i].Bytes()) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
// Use the setter function to store the value
|
||||
setter(arg, reconstr) |
||||
} |
||||
|
||||
return nil |
||||
} |
@ -1,381 +0,0 @@ |
||||
// Copyright 2020 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"math/big" |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
func TestMakeTopics(t *testing.T) { |
||||
type args struct { |
||||
query [][]interface{} |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want [][]common.Hash |
||||
wantErr bool |
||||
}{ |
||||
{ |
||||
"support fixed byte types, right padded to 32 bytes", |
||||
args{[][]interface{}{{[5]byte{1, 2, 3, 4, 5}}}}, |
||||
[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support common hash types in topics", |
||||
args{[][]interface{}{{common.Hash{1, 2, 3, 4, 5}}}}, |
||||
[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support address types in topics", |
||||
args{[][]interface{}{{common.Address{1, 2, 3, 4, 5}}}}, |
||||
[][]common.Hash{{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support *big.Int types in topics", |
||||
args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}}, |
||||
[][]common.Hash{{common.Hash{128}}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support boolean types in topics", |
||||
args{[][]interface{}{ |
||||
{true}, |
||||
{false}, |
||||
}}, |
||||
[][]common.Hash{ |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}, |
||||
{common.Hash{0}}, |
||||
}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support int/uint(8/16/32/64) types in topics", |
||||
args{[][]interface{}{ |
||||
{int8(-2)}, |
||||
{int16(-3)}, |
||||
{int32(-4)}, |
||||
{int64(-5)}, |
||||
{int8(1)}, |
||||
{int16(256)}, |
||||
{int32(65536)}, |
||||
{int64(4294967296)}, |
||||
{uint8(1)}, |
||||
{uint16(256)}, |
||||
{uint32(65536)}, |
||||
{uint64(4294967296)}, |
||||
}}, |
||||
[][]common.Hash{ |
||||
{common.Hash{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254}}, |
||||
{common.Hash{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253}}, |
||||
{common.Hash{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 252}}, |
||||
{common.Hash{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 251}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}}, |
||||
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}}, |
||||
}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support string types in topics", |
||||
args{[][]interface{}{{"hello world"}}}, |
||||
[][]common.Hash{{crypto.Keccak256Hash([]byte("hello world"))}}, |
||||
false, |
||||
}, |
||||
{ |
||||
"support byte slice types in topics", |
||||
args{[][]interface{}{{[]byte{1, 2, 3}}}}, |
||||
[][]common.Hash{{crypto.Keccak256Hash([]byte{1, 2, 3})}}, |
||||
false, |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
got, err := MakeTopics(tt.args.query...) |
||||
if (err != nil) != tt.wantErr { |
||||
t.Errorf("makeTopics() error = %v, wantErr %v", err, tt.wantErr) |
||||
return |
||||
} |
||||
if !reflect.DeepEqual(got, tt.want) { |
||||
t.Errorf("makeTopics() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
type args struct { |
||||
createObj func() interface{} |
||||
resultObj func() interface{} |
||||
resultMap func() map[string]interface{} |
||||
fields Arguments |
||||
topics []common.Hash |
||||
} |
||||
|
||||
type bytesStruct struct { |
||||
StaticBytes [5]byte |
||||
} |
||||
type int8Struct struct { |
||||
Int8Value int8 |
||||
} |
||||
type int256Struct struct { |
||||
Int256Value *big.Int |
||||
} |
||||
|
||||
type hashStruct struct { |
||||
HashValue common.Hash |
||||
} |
||||
|
||||
type funcStruct struct { |
||||
FuncValue [24]byte |
||||
} |
||||
|
||||
type topicTest struct { |
||||
name string |
||||
args args |
||||
wantErr bool |
||||
} |
||||
|
||||
func setupTopicsTests() []topicTest { |
||||
bytesType, _ := NewType("bytes5", "", nil) |
||||
int8Type, _ := NewType("int8", "", nil) |
||||
int256Type, _ := NewType("int256", "", nil) |
||||
tupleType, _ := NewType("tuple(int256,int8)", "", nil) |
||||
stringType, _ := NewType("string", "", nil) |
||||
funcType, _ := NewType("function", "", nil) |
||||
|
||||
tests := []topicTest{ |
||||
{ |
||||
name: "support fixed byte types, right padded to 32 bytes", |
||||
args: args{ |
||||
createObj: func() interface{} { return &bytesStruct{} }, |
||||
resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"staticBytes": [5]byte{1, 2, 3, 4, 5}} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "staticBytes", |
||||
Type: bytesType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{1, 2, 3, 4, 5}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "int8 with negative value", |
||||
args: args{ |
||||
createObj: func() interface{} { return &int8Struct{} }, |
||||
resultObj: func() interface{} { return &int8Struct{Int8Value: -1} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"int8Value": int8(-1)} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "int8Value", |
||||
Type: int8Type, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "int256 with negative value", |
||||
args: args{ |
||||
createObj: func() interface{} { return &int256Struct{} }, |
||||
resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"int256Value": big.NewInt(-1)} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "int256Value", |
||||
Type: int256Type, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "hash type", |
||||
args: args{ |
||||
createObj: func() interface{} { return &hashStruct{} }, |
||||
resultObj: func() interface{} { return &hashStruct{crypto.Keccak256Hash([]byte("stringtopic"))} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"hashValue": crypto.Keccak256Hash([]byte("stringtopic"))} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "hashValue", |
||||
Type: stringType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
crypto.Keccak256Hash([]byte("stringtopic")), |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "function type", |
||||
args: args{ |
||||
createObj: func() interface{} { return &funcStruct{} }, |
||||
resultObj: func() interface{} { |
||||
return &funcStruct{[24]byte{255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}} |
||||
}, |
||||
resultMap: func() map[string]interface{} { |
||||
return map[string]interface{}{"funcValue": [24]byte{255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}} |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "funcValue", |
||||
Type: funcType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: false, |
||||
}, |
||||
{ |
||||
name: "error on topic/field count mismatch", |
||||
args: args{ |
||||
createObj: func() interface{} { return nil }, |
||||
resultObj: func() interface{} { return nil }, |
||||
resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, |
||||
fields: Arguments{Argument{ |
||||
Name: "tupletype", |
||||
Type: tupleType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
{ |
||||
name: "error on unindexed arguments", |
||||
args: args{ |
||||
createObj: func() interface{} { return &int256Struct{} }, |
||||
resultObj: func() interface{} { return &int256Struct{} }, |
||||
resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, |
||||
fields: Arguments{Argument{ |
||||
Name: "int256Value", |
||||
Type: int256Type, |
||||
Indexed: false, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
{ |
||||
name: "error on tuple in topic reconstruction", |
||||
args: args{ |
||||
createObj: func() interface{} { return &tupleType }, |
||||
resultObj: func() interface{} { return &tupleType }, |
||||
resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, |
||||
fields: Arguments{Argument{ |
||||
Name: "tupletype", |
||||
Type: tupleType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{{0}}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
{ |
||||
name: "error on improper encoded function", |
||||
args: args{ |
||||
createObj: func() interface{} { return &funcStruct{} }, |
||||
resultObj: func() interface{} { return &funcStruct{} }, |
||||
resultMap: func() map[string]interface{} { |
||||
return make(map[string]interface{}) |
||||
}, |
||||
fields: Arguments{Argument{ |
||||
Name: "funcValue", |
||||
Type: funcType, |
||||
Indexed: true, |
||||
}}, |
||||
topics: []common.Hash{ |
||||
{0, 0, 0, 0, 0, 0, 0, 128, 255, 255, 255, 255, 255, 255, 255, 255, |
||||
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, |
||||
}, |
||||
}, |
||||
wantErr: true, |
||||
}, |
||||
} |
||||
|
||||
return tests |
||||
} |
||||
|
||||
func TestParseTopics(t *testing.T) { |
||||
tests := setupTopicsTests() |
||||
|
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
createObj := tt.args.createObj() |
||||
if err := ParseTopics(createObj, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { |
||||
t.Errorf("parseTopics() error = %v, wantErr %v", err, tt.wantErr) |
||||
} |
||||
resultObj := tt.args.resultObj() |
||||
if !reflect.DeepEqual(createObj, resultObj) { |
||||
t.Errorf("parseTopics() = %v, want %v", createObj, resultObj) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestParseTopicsIntoMap(t *testing.T) { |
||||
tests := setupTopicsTests() |
||||
|
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
outMap := make(map[string]interface{}) |
||||
if err := ParseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr { |
||||
t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr) |
||||
} |
||||
resultMap := tt.args.resultMap() |
||||
if !reflect.DeepEqual(outMap, resultMap) { |
||||
t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1,426 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"reflect" |
||||
"regexp" |
||||
"strconv" |
||||
"strings" |
||||
"unicode" |
||||
"unicode/utf8" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
// Type enumerator
|
||||
const ( |
||||
IntTy byte = iota |
||||
UintTy |
||||
BoolTy |
||||
StringTy |
||||
SliceTy |
||||
ArrayTy |
||||
TupleTy |
||||
AddressTy |
||||
FixedBytesTy |
||||
BytesTy |
||||
HashTy |
||||
FixedPointTy |
||||
FunctionTy |
||||
) |
||||
|
||||
// Type is the reflection of the supported argument type.
|
||||
type Type struct { |
||||
Elem *Type |
||||
Size int |
||||
T byte // Our own type checking
|
||||
|
||||
stringKind string // holds the unparsed string for deriving signatures
|
||||
|
||||
// Tuple relative fields
|
||||
TupleRawName string // Raw struct name defined in source code, may be empty.
|
||||
TupleElems []*Type // Type information of all tuple fields
|
||||
TupleRawNames []string // Raw field name of all tuple fields
|
||||
TupleType reflect.Type // Underlying struct of the tuple
|
||||
} |
||||
|
||||
var ( |
||||
// typeRegex parses the abi sub types
|
||||
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?") |
||||
) |
||||
|
||||
// NewType creates a new reflection type of abi type given in t.
|
||||
func NewType(t string, internalType string, components []ArgumentMarshaling) (typ Type, err error) { |
||||
// check that array brackets are equal if they exist
|
||||
if strings.Count(t, "[") != strings.Count(t, "]") { |
||||
return Type{}, fmt.Errorf("invalid arg type in abi") |
||||
} |
||||
typ.stringKind = t |
||||
|
||||
// if there are brackets, get ready to go into slice/array mode and
|
||||
// recursively create the type
|
||||
if strings.Count(t, "[") != 0 { |
||||
// Note internalType can be empty here.
|
||||
subInternal := internalType |
||||
if i := strings.LastIndex(internalType, "["); i != -1 { |
||||
subInternal = subInternal[:i] |
||||
} |
||||
// recursively embed the type
|
||||
i := strings.LastIndex(t, "[") |
||||
embeddedType, err := NewType(t[:i], subInternal, components) |
||||
if err != nil { |
||||
return Type{}, err |
||||
} |
||||
// grab the last cell and create a type from there
|
||||
sliced := t[i:] |
||||
// grab the slice size with regexp
|
||||
re := regexp.MustCompile("[0-9]+") |
||||
intz := re.FindAllString(sliced, -1) |
||||
|
||||
if len(intz) == 0 { |
||||
// is a slice
|
||||
typ.T = SliceTy |
||||
typ.Elem = &embeddedType |
||||
typ.stringKind = embeddedType.stringKind + sliced |
||||
} else if len(intz) == 1 { |
||||
// is an array
|
||||
typ.T = ArrayTy |
||||
typ.Elem = &embeddedType |
||||
typ.Size, err = strconv.Atoi(intz[0]) |
||||
if err != nil { |
||||
return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) |
||||
} |
||||
typ.stringKind = embeddedType.stringKind + sliced |
||||
} else { |
||||
return Type{}, fmt.Errorf("invalid formatting of array type") |
||||
} |
||||
return typ, err |
||||
} |
||||
// parse the type and size of the abi-type.
|
||||
matches := typeRegex.FindAllStringSubmatch(t, -1) |
||||
if len(matches) == 0 { |
||||
return Type{}, fmt.Errorf("invalid type '%v'", t) |
||||
} |
||||
parsedType := matches[0] |
||||
|
||||
// varSize is the size of the variable
|
||||
var varSize int |
||||
if len(parsedType[3]) > 0 { |
||||
var err error |
||||
varSize, err = strconv.Atoi(parsedType[2]) |
||||
if err != nil { |
||||
return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) |
||||
} |
||||
} else { |
||||
if parsedType[0] == "uint" || parsedType[0] == "int" { |
||||
// this should fail because it means that there's something wrong with
|
||||
// the abi type (the compiler should always format it to the size...always)
|
||||
return Type{}, fmt.Errorf("unsupported arg type: %s", t) |
||||
} |
||||
} |
||||
// varType is the parsed abi type
|
||||
switch varType := parsedType[1]; varType { |
||||
case "int": |
||||
typ.Size = varSize |
||||
typ.T = IntTy |
||||
case "uint": |
||||
typ.Size = varSize |
||||
typ.T = UintTy |
||||
case "bool": |
||||
typ.T = BoolTy |
||||
case "address": |
||||
typ.Size = 20 |
||||
typ.T = AddressTy |
||||
case "string": |
||||
typ.T = StringTy |
||||
case "bytes": |
||||
if varSize == 0 { |
||||
typ.T = BytesTy |
||||
} else { |
||||
if varSize > 32 { |
||||
return Type{}, fmt.Errorf("unsupported arg type: %s", t) |
||||
} |
||||
typ.T = FixedBytesTy |
||||
typ.Size = varSize |
||||
} |
||||
case "tuple": |
||||
var ( |
||||
fields []reflect.StructField |
||||
elems []*Type |
||||
names []string |
||||
expression string // canonical parameter expression
|
||||
used = make(map[string]bool) |
||||
) |
||||
expression += "(" |
||||
for idx, c := range components { |
||||
cType, err := NewType(c.Type, c.InternalType, c.Components) |
||||
if err != nil { |
||||
return Type{}, err |
||||
} |
||||
name := ToCamelCase(c.Name) |
||||
if name == "" { |
||||
return Type{}, errors.New("abi: purely anonymous or underscored field is not supported") |
||||
} |
||||
fieldName := ResolveNameConflict(name, func(s string) bool { return used[s] }) |
||||
if err != nil { |
||||
return Type{}, err |
||||
} |
||||
used[fieldName] = true |
||||
if !isValidFieldName(fieldName) { |
||||
return Type{}, fmt.Errorf("field %d has invalid name", idx) |
||||
} |
||||
fields = append(fields, reflect.StructField{ |
||||
Name: fieldName, // reflect.StructOf will panic for any exported field.
|
||||
Type: cType.GetType(), |
||||
Tag: reflect.StructTag("json:\"" + c.Name + "\""), |
||||
}) |
||||
elems = append(elems, &cType) |
||||
names = append(names, c.Name) |
||||
expression += cType.stringKind |
||||
if idx != len(components)-1 { |
||||
expression += "," |
||||
} |
||||
} |
||||
expression += ")" |
||||
|
||||
typ.TupleType = reflect.StructOf(fields) |
||||
typ.TupleElems = elems |
||||
typ.TupleRawNames = names |
||||
typ.T = TupleTy |
||||
typ.stringKind = expression |
||||
|
||||
const structPrefix = "struct " |
||||
// After solidity 0.5.10, a new field of abi "internalType"
|
||||
// is introduced. From that we can obtain the struct name
|
||||
// user defined in the source code.
|
||||
if internalType != "" && strings.HasPrefix(internalType, structPrefix) { |
||||
// Foo.Bar type definition is not allowed in golang,
|
||||
// convert the format to FooBar
|
||||
typ.TupleRawName = strings.ReplaceAll(internalType[len(structPrefix):], ".", "") |
||||
} |
||||
|
||||
case "function": |
||||
typ.T = FunctionTy |
||||
typ.Size = 24 |
||||
default: |
||||
return Type{}, fmt.Errorf("unsupported arg type: %s", t) |
||||
} |
||||
|
||||
return |
||||
} |
||||
|
||||
// GetType returns the reflection type of the ABI type.
|
||||
func (t Type) GetType() reflect.Type { |
||||
switch t.T { |
||||
case IntTy: |
||||
return reflectIntType(false, t.Size) |
||||
case UintTy: |
||||
return reflectIntType(true, t.Size) |
||||
case BoolTy: |
||||
return reflect.TypeOf(false) |
||||
case StringTy: |
||||
return reflect.TypeOf("") |
||||
case SliceTy: |
||||
return reflect.SliceOf(t.Elem.GetType()) |
||||
case ArrayTy: |
||||
return reflect.ArrayOf(t.Size, t.Elem.GetType()) |
||||
case TupleTy: |
||||
return t.TupleType |
||||
case AddressTy: |
||||
return reflect.TypeOf(common.Address{}) |
||||
case FixedBytesTy: |
||||
return reflect.ArrayOf(t.Size, reflect.TypeOf(byte(0))) |
||||
case BytesTy: |
||||
return reflect.SliceOf(reflect.TypeOf(byte(0))) |
||||
case HashTy: |
||||
// hashtype currently not used
|
||||
return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) |
||||
case FixedPointTy: |
||||
// fixedpoint type currently not used
|
||||
return reflect.ArrayOf(32, reflect.TypeOf(byte(0))) |
||||
case FunctionTy: |
||||
return reflect.ArrayOf(24, reflect.TypeOf(byte(0))) |
||||
default: |
||||
panic("Invalid type") |
||||
} |
||||
} |
||||
|
||||
// String implements Stringer.
|
||||
func (t Type) String() (out string) { |
||||
return t.stringKind |
||||
} |
||||
|
||||
func (t Type) pack(v reflect.Value) ([]byte, error) { |
||||
// dereference pointer first if it's a pointer
|
||||
v = indirect(v) |
||||
if err := typeCheck(t, v); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
switch t.T { |
||||
case SliceTy, ArrayTy: |
||||
var ret []byte |
||||
|
||||
if t.requiresLengthPrefix() { |
||||
// append length
|
||||
ret = append(ret, packNum(reflect.ValueOf(v.Len()))...) |
||||
} |
||||
|
||||
// calculate offset if any
|
||||
offset := 0 |
||||
offsetReq := isDynamicType(*t.Elem) |
||||
if offsetReq { |
||||
offset = getTypeSize(*t.Elem) * v.Len() |
||||
} |
||||
var tail []byte |
||||
for i := 0; i < v.Len(); i++ { |
||||
val, err := t.Elem.pack(v.Index(i)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if !offsetReq { |
||||
ret = append(ret, val...) |
||||
continue |
||||
} |
||||
ret = append(ret, packNum(reflect.ValueOf(offset))...) |
||||
offset += len(val) |
||||
tail = append(tail, val...) |
||||
} |
||||
return append(ret, tail...), nil |
||||
case TupleTy: |
||||
// (T1,...,Tk) for k >= 0 and any types T1, …, Tk
|
||||
// enc(X) = head(X(1)) ... head(X(k)) tail(X(1)) ... tail(X(k))
|
||||
// where X = (X(1), ..., X(k)) and head and tail are defined for Ti being a static
|
||||
// type as
|
||||
// head(X(i)) = enc(X(i)) and tail(X(i)) = "" (the empty string)
|
||||
// and as
|
||||
// head(X(i)) = enc(len(head(X(1)) ... head(X(k)) tail(X(1)) ... tail(X(i-1))))
|
||||
// tail(X(i)) = enc(X(i))
|
||||
// otherwise, i.e. if Ti is a dynamic type.
|
||||
fieldmap, err := mapArgNamesToStructFields(t.TupleRawNames, v) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Calculate prefix occupied size.
|
||||
offset := 0 |
||||
for _, elem := range t.TupleElems { |
||||
offset += getTypeSize(*elem) |
||||
} |
||||
var ret, tail []byte |
||||
for i, elem := range t.TupleElems { |
||||
field := v.FieldByName(fieldmap[t.TupleRawNames[i]]) |
||||
if !field.IsValid() { |
||||
return nil, fmt.Errorf("field %s for tuple not found in the given struct", t.TupleRawNames[i]) |
||||
} |
||||
val, err := elem.pack(field) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if isDynamicType(*elem) { |
||||
ret = append(ret, packNum(reflect.ValueOf(offset))...) |
||||
tail = append(tail, val...) |
||||
offset += len(val) |
||||
} else { |
||||
ret = append(ret, val...) |
||||
} |
||||
} |
||||
return append(ret, tail...), nil |
||||
|
||||
default: |
||||
return packElement(t, v) |
||||
} |
||||
} |
||||
|
||||
// requireLengthPrefix returns whether the type requires any sort of length
|
||||
// prefixing.
|
||||
func (t Type) requiresLengthPrefix() bool { |
||||
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy |
||||
} |
||||
|
||||
// isDynamicType returns true if the type is dynamic.
|
||||
// The following types are called “dynamic”:
|
||||
// * bytes
|
||||
// * string
|
||||
// * T[] for any T
|
||||
// * T[k] for any dynamic T and any k >= 0
|
||||
// * (T1,...,Tk) if Ti is dynamic for some 1 <= i <= k
|
||||
func isDynamicType(t Type) bool { |
||||
if t.T == TupleTy { |
||||
for _, elem := range t.TupleElems { |
||||
if isDynamicType(*elem) { |
||||
return true |
||||
} |
||||
} |
||||
return false |
||||
} |
||||
return t.T == StringTy || t.T == BytesTy || t.T == SliceTy || (t.T == ArrayTy && isDynamicType(*t.Elem)) |
||||
} |
||||
|
||||
// getTypeSize returns the size that this type needs to occupy.
|
||||
// We distinguish static and dynamic types. Static types are encoded in-place
|
||||
// and dynamic types are encoded at a separately allocated location after the
|
||||
// current block.
|
||||
// So for a static variable, the size returned represents the size that the
|
||||
// variable actually occupies.
|
||||
// For a dynamic variable, the returned size is fixed 32 bytes, which is used
|
||||
// to store the location reference for actual value storage.
|
||||
func getTypeSize(t Type) int { |
||||
if t.T == ArrayTy && !isDynamicType(*t.Elem) { |
||||
// Recursively calculate type size if it is a nested array
|
||||
if t.Elem.T == ArrayTy || t.Elem.T == TupleTy { |
||||
return t.Size * getTypeSize(*t.Elem) |
||||
} |
||||
return t.Size * 32 |
||||
} else if t.T == TupleTy && !isDynamicType(t) { |
||||
total := 0 |
||||
for _, elem := range t.TupleElems { |
||||
total += getTypeSize(*elem) |
||||
} |
||||
return total |
||||
} |
||||
return 32 |
||||
} |
||||
|
||||
// isLetter reports whether a given 'rune' is classified as a Letter.
|
||||
// This method is copied from reflect/type.go
|
||||
func isLetter(ch rune) bool { |
||||
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= utf8.RuneSelf && unicode.IsLetter(ch) |
||||
} |
||||
|
||||
// isValidFieldName checks if a string is a valid (struct) field name or not.
|
||||
//
|
||||
// According to the language spec, a field name should be an identifier.
|
||||
//
|
||||
// identifier = letter { letter | unicode_digit } .
|
||||
// letter = unicode_letter | "_" .
|
||||
// This method is copied from reflect/type.go
|
||||
func isValidFieldName(fieldName string) bool { |
||||
for i, c := range fieldName { |
||||
if i == 0 && !isLetter(c) { |
||||
return false |
||||
} |
||||
|
||||
if !(isLetter(c) || unicode.IsDigit(c)) { |
||||
return false |
||||
} |
||||
} |
||||
|
||||
return len(fieldName) > 0 |
||||
} |
@ -1,375 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"math/big" |
||||
"reflect" |
||||
"testing" |
||||
|
||||
"github.com/davecgh/go-spew/spew" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
// typeWithoutStringer is a alias for the Type type which simply doesn't implement
|
||||
// the stringer interface to allow printing type details in the tests below.
|
||||
type typeWithoutStringer Type |
||||
|
||||
// Tests that all allowed types get recognized by the type parser.
|
||||
func TestTypeRegexp(t *testing.T) { |
||||
tests := []struct { |
||||
blob string |
||||
components []ArgumentMarshaling |
||||
kind Type |
||||
}{ |
||||
{"bool", nil, Type{T: BoolTy, stringKind: "bool"}}, |
||||
{"bool[]", nil, Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}}, |
||||
{"bool[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}}, |
||||
{"bool[2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}}, |
||||
{"bool[][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}}, |
||||
{"bool[][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}}, |
||||
{"bool[2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}}, |
||||
{"bool[2][][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}}, |
||||
{"bool[2][2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}}, |
||||
{"bool[][][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}}, |
||||
{"bool[][2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}}, |
||||
{"int8", nil, Type{Size: 8, T: IntTy, stringKind: "int8"}}, |
||||
{"int16", nil, Type{Size: 16, T: IntTy, stringKind: "int16"}}, |
||||
{"int32", nil, Type{Size: 32, T: IntTy, stringKind: "int32"}}, |
||||
{"int64", nil, Type{Size: 64, T: IntTy, stringKind: "int64"}}, |
||||
{"int256", nil, Type{Size: 256, T: IntTy, stringKind: "int256"}}, |
||||
{"int8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, |
||||
{"int8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, |
||||
{"int16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, |
||||
{"int16[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, |
||||
{"int32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, |
||||
{"int32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, |
||||
{"int64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, |
||||
{"int64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, |
||||
{"int256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, |
||||
{"int256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, |
||||
{"uint8", nil, Type{Size: 8, T: UintTy, stringKind: "uint8"}}, |
||||
{"uint16", nil, Type{Size: 16, T: UintTy, stringKind: "uint16"}}, |
||||
{"uint32", nil, Type{Size: 32, T: UintTy, stringKind: "uint32"}}, |
||||
{"uint64", nil, Type{Size: 64, T: UintTy, stringKind: "uint64"}}, |
||||
{"uint256", nil, Type{Size: 256, T: UintTy, stringKind: "uint256"}}, |
||||
{"uint8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, |
||||
{"uint8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, |
||||
{"uint16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, |
||||
{"uint16[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, |
||||
{"uint32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, |
||||
{"uint32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, |
||||
{"uint64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, |
||||
{"uint64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, |
||||
{"uint256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, |
||||
{"uint256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, |
||||
{"bytes32", nil, Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}}, |
||||
{"bytes[]", nil, Type{T: SliceTy, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, |
||||
{"bytes[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}}, |
||||
{"bytes32[]", nil, Type{T: SliceTy, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, |
||||
{"bytes32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, |
||||
{"string", nil, Type{T: StringTy, stringKind: "string"}}, |
||||
{"string[]", nil, Type{T: SliceTy, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, |
||||
{"string[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[2]"}}, |
||||
{"address", nil, Type{Size: 20, T: AddressTy, stringKind: "address"}}, |
||||
{"address[]", nil, Type{T: SliceTy, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, |
||||
{"address[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, |
||||
// TODO when fixed types are implemented properly
|
||||
// {"fixed", nil, Type{}},
|
||||
// {"fixed128x128", nil, Type{}},
|
||||
// {"fixed[]", nil, Type{}},
|
||||
// {"fixed[2]", nil, Type{}},
|
||||
// {"fixed128x128[]", nil, Type{}},
|
||||
// {"fixed128x128[2]", nil, Type{}},
|
||||
{"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{T: TupleTy, TupleType: reflect.TypeOf(struct { |
||||
A int64 `json:"a"` |
||||
}{}), stringKind: "(int64)", |
||||
TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}}}, |
||||
{"tuple with long name", []ArgumentMarshaling{{Name: "aTypicalParamName", Type: "int64"}}, Type{T: TupleTy, TupleType: reflect.TypeOf(struct { |
||||
ATypicalParamName int64 `json:"aTypicalParamName"` |
||||
}{}), stringKind: "(int64)", |
||||
TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"aTypicalParamName"}}}, |
||||
} |
||||
|
||||
for _, tt := range tests { |
||||
typ, err := NewType(tt.blob, "", tt.components) |
||||
if err != nil { |
||||
t.Errorf("type %q: failed to parse type string: %v", tt.blob, err) |
||||
} |
||||
if !reflect.DeepEqual(typ, tt.kind) { |
||||
t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind))) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestTypeCheck(t *testing.T) { |
||||
for i, test := range []struct { |
||||
typ string |
||||
components []ArgumentMarshaling |
||||
input interface{} |
||||
err string |
||||
}{ |
||||
{"uint", nil, big.NewInt(1), "unsupported arg type: uint"}, |
||||
{"int", nil, big.NewInt(1), "unsupported arg type: int"}, |
||||
{"uint256", nil, big.NewInt(1), ""}, |
||||
{"uint256[][3][]", nil, [][3][]*big.Int{{{}}}, ""}, |
||||
{"uint256[][][3]", nil, [3][][]*big.Int{{{}}}, ""}, |
||||
{"uint256[3][][]", nil, [][][3]*big.Int{{{}}}, ""}, |
||||
{"uint256[3][3][3]", nil, [3][3][3]*big.Int{{{}}}, ""}, |
||||
{"uint8[][]", nil, [][]uint8{}, ""}, |
||||
{"int256", nil, big.NewInt(1), ""}, |
||||
{"uint8", nil, uint8(1), ""}, |
||||
{"uint16", nil, uint16(1), ""}, |
||||
{"uint32", nil, uint32(1), ""}, |
||||
{"uint64", nil, uint64(1), ""}, |
||||
{"int8", nil, int8(1), ""}, |
||||
{"int16", nil, int16(1), ""}, |
||||
{"int32", nil, int32(1), ""}, |
||||
{"int64", nil, int64(1), ""}, |
||||
{"uint24", nil, big.NewInt(1), ""}, |
||||
{"uint40", nil, big.NewInt(1), ""}, |
||||
{"uint48", nil, big.NewInt(1), ""}, |
||||
{"uint56", nil, big.NewInt(1), ""}, |
||||
{"uint72", nil, big.NewInt(1), ""}, |
||||
{"uint80", nil, big.NewInt(1), ""}, |
||||
{"uint88", nil, big.NewInt(1), ""}, |
||||
{"uint96", nil, big.NewInt(1), ""}, |
||||
{"uint104", nil, big.NewInt(1), ""}, |
||||
{"uint112", nil, big.NewInt(1), ""}, |
||||
{"uint120", nil, big.NewInt(1), ""}, |
||||
{"uint128", nil, big.NewInt(1), ""}, |
||||
{"uint136", nil, big.NewInt(1), ""}, |
||||
{"uint144", nil, big.NewInt(1), ""}, |
||||
{"uint152", nil, big.NewInt(1), ""}, |
||||
{"uint160", nil, big.NewInt(1), ""}, |
||||
{"uint168", nil, big.NewInt(1), ""}, |
||||
{"uint176", nil, big.NewInt(1), ""}, |
||||
{"uint184", nil, big.NewInt(1), ""}, |
||||
{"uint192", nil, big.NewInt(1), ""}, |
||||
{"uint200", nil, big.NewInt(1), ""}, |
||||
{"uint208", nil, big.NewInt(1), ""}, |
||||
{"uint216", nil, big.NewInt(1), ""}, |
||||
{"uint224", nil, big.NewInt(1), ""}, |
||||
{"uint232", nil, big.NewInt(1), ""}, |
||||
{"uint240", nil, big.NewInt(1), ""}, |
||||
{"uint248", nil, big.NewInt(1), ""}, |
||||
{"int24", nil, big.NewInt(1), ""}, |
||||
{"int40", nil, big.NewInt(1), ""}, |
||||
{"int48", nil, big.NewInt(1), ""}, |
||||
{"int56", nil, big.NewInt(1), ""}, |
||||
{"int72", nil, big.NewInt(1), ""}, |
||||
{"int80", nil, big.NewInt(1), ""}, |
||||
{"int88", nil, big.NewInt(1), ""}, |
||||
{"int96", nil, big.NewInt(1), ""}, |
||||
{"int104", nil, big.NewInt(1), ""}, |
||||
{"int112", nil, big.NewInt(1), ""}, |
||||
{"int120", nil, big.NewInt(1), ""}, |
||||
{"int128", nil, big.NewInt(1), ""}, |
||||
{"int136", nil, big.NewInt(1), ""}, |
||||
{"int144", nil, big.NewInt(1), ""}, |
||||
{"int152", nil, big.NewInt(1), ""}, |
||||
{"int160", nil, big.NewInt(1), ""}, |
||||
{"int168", nil, big.NewInt(1), ""}, |
||||
{"int176", nil, big.NewInt(1), ""}, |
||||
{"int184", nil, big.NewInt(1), ""}, |
||||
{"int192", nil, big.NewInt(1), ""}, |
||||
{"int200", nil, big.NewInt(1), ""}, |
||||
{"int208", nil, big.NewInt(1), ""}, |
||||
{"int216", nil, big.NewInt(1), ""}, |
||||
{"int224", nil, big.NewInt(1), ""}, |
||||
{"int232", nil, big.NewInt(1), ""}, |
||||
{"int240", nil, big.NewInt(1), ""}, |
||||
{"int248", nil, big.NewInt(1), ""}, |
||||
{"uint30", nil, uint8(1), "abi: cannot use uint8 as type ptr as argument"}, |
||||
{"uint8", nil, uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, |
||||
{"uint8", nil, uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, |
||||
{"uint8", nil, uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, |
||||
{"uint8", nil, int8(1), "abi: cannot use int8 as type uint8 as argument"}, |
||||
{"uint8", nil, int16(1), "abi: cannot use int16 as type uint8 as argument"}, |
||||
{"uint8", nil, int32(1), "abi: cannot use int32 as type uint8 as argument"}, |
||||
{"uint8", nil, int64(1), "abi: cannot use int64 as type uint8 as argument"}, |
||||
{"uint16", nil, uint16(1), ""}, |
||||
{"uint16", nil, uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, |
||||
{"uint16[]", nil, []uint16{1, 2, 3}, ""}, |
||||
{"uint16[]", nil, [3]uint16{1, 2, 3}, ""}, |
||||
{"uint16[]", nil, []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"}, |
||||
{"uint16[3]", nil, [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, |
||||
{"uint16[3]", nil, [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, |
||||
{"uint16[3]", nil, []uint16{1, 2, 3}, ""}, |
||||
{"uint16[3]", nil, []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, |
||||
{"address[]", nil, []common.Address{{1}}, ""}, |
||||
{"address[1]", nil, []common.Address{{1}}, ""}, |
||||
{"address[1]", nil, [1]common.Address{{1}}, ""}, |
||||
{"address[2]", nil, [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, |
||||
{"bytes32", nil, [32]byte{}, ""}, |
||||
{"bytes31", nil, [31]byte{}, ""}, |
||||
{"bytes30", nil, [30]byte{}, ""}, |
||||
{"bytes29", nil, [29]byte{}, ""}, |
||||
{"bytes28", nil, [28]byte{}, ""}, |
||||
{"bytes27", nil, [27]byte{}, ""}, |
||||
{"bytes26", nil, [26]byte{}, ""}, |
||||
{"bytes25", nil, [25]byte{}, ""}, |
||||
{"bytes24", nil, [24]byte{}, ""}, |
||||
{"bytes23", nil, [23]byte{}, ""}, |
||||
{"bytes22", nil, [22]byte{}, ""}, |
||||
{"bytes21", nil, [21]byte{}, ""}, |
||||
{"bytes20", nil, [20]byte{}, ""}, |
||||
{"bytes19", nil, [19]byte{}, ""}, |
||||
{"bytes18", nil, [18]byte{}, ""}, |
||||
{"bytes17", nil, [17]byte{}, ""}, |
||||
{"bytes16", nil, [16]byte{}, ""}, |
||||
{"bytes15", nil, [15]byte{}, ""}, |
||||
{"bytes14", nil, [14]byte{}, ""}, |
||||
{"bytes13", nil, [13]byte{}, ""}, |
||||
{"bytes12", nil, [12]byte{}, ""}, |
||||
{"bytes11", nil, [11]byte{}, ""}, |
||||
{"bytes10", nil, [10]byte{}, ""}, |
||||
{"bytes9", nil, [9]byte{}, ""}, |
||||
{"bytes8", nil, [8]byte{}, ""}, |
||||
{"bytes7", nil, [7]byte{}, ""}, |
||||
{"bytes6", nil, [6]byte{}, ""}, |
||||
{"bytes5", nil, [5]byte{}, ""}, |
||||
{"bytes4", nil, [4]byte{}, ""}, |
||||
{"bytes3", nil, [3]byte{}, ""}, |
||||
{"bytes2", nil, [2]byte{}, ""}, |
||||
{"bytes1", nil, [1]byte{}, ""}, |
||||
{"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, |
||||
{"bytes32", nil, common.Hash{1}, ""}, |
||||
{"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, |
||||
{"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, |
||||
{"bytes", nil, []byte{0, 1}, ""}, |
||||
{"bytes", nil, [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, |
||||
{"bytes", nil, common.Hash{1}, "abi: cannot use array as type slice as argument"}, |
||||
{"string", nil, "hello world", ""}, |
||||
{"string", nil, "", ""}, |
||||
{"string", nil, []byte{}, "abi: cannot use slice as type string as argument"}, |
||||
{"bytes32[]", nil, [][32]byte{{}}, ""}, |
||||
{"function", nil, [24]byte{}, ""}, |
||||
{"bytes20", nil, common.Address{}, ""}, |
||||
{"address", nil, [20]byte{}, ""}, |
||||
{"address", nil, common.Address{}, ""}, |
||||
{"bytes32[]]", nil, "", "invalid arg type in abi"}, |
||||
{"invalidType", nil, "", "unsupported arg type: invalidType"}, |
||||
{"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"}, |
||||
// simple tuple
|
||||
{"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct { |
||||
A *big.Int |
||||
B *big.Int |
||||
}{}, ""}, |
||||
// tuple slice
|
||||
{"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { |
||||
A *big.Int |
||||
B *big.Int |
||||
}{}, ""}, |
||||
// tuple array
|
||||
{"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { |
||||
A *big.Int |
||||
B *big.Int |
||||
}{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""}, |
||||
} { |
||||
typ, err := NewType(test.typ, "", test.components) |
||||
if err != nil && len(test.err) == 0 { |
||||
t.Fatal("unexpected parse error:", err) |
||||
} else if err != nil && len(test.err) != 0 { |
||||
if err.Error() != test.err { |
||||
t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) |
||||
} |
||||
continue |
||||
} |
||||
|
||||
err = typeCheck(typ, reflect.ValueOf(test.input)) |
||||
if err != nil && len(test.err) == 0 { |
||||
t.Errorf("%d failed. Expected no err but got: %v", i, err) |
||||
continue |
||||
} |
||||
if err == nil && len(test.err) != 0 { |
||||
t.Errorf("%d failed. Expected err: %v but got none", i, test.err) |
||||
continue |
||||
} |
||||
|
||||
if err != nil && len(test.err) != 0 && err.Error() != test.err { |
||||
t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestInternalType(t *testing.T) { |
||||
components := []ArgumentMarshaling{{Name: "a", Type: "int64"}} |
||||
internalType := "struct a.b[]" |
||||
kind := Type{ |
||||
T: TupleTy, |
||||
TupleType: reflect.TypeOf(struct { |
||||
A int64 `json:"a"` |
||||
}{}), |
||||
stringKind: "(int64)", |
||||
TupleRawName: "ab[]", |
||||
TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, |
||||
TupleRawNames: []string{"a"}, |
||||
} |
||||
|
||||
blob := "tuple" |
||||
typ, err := NewType(blob, internalType, components) |
||||
if err != nil { |
||||
t.Errorf("type %q: failed to parse type string: %v", blob, err) |
||||
} |
||||
if !reflect.DeepEqual(typ, kind) { |
||||
t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(kind))) |
||||
} |
||||
} |
||||
|
||||
func TestGetTypeSize(t *testing.T) { |
||||
var testCases = []struct { |
||||
typ string |
||||
components []ArgumentMarshaling |
||||
typSize int |
||||
}{ |
||||
// simple array
|
||||
{"uint256[2]", nil, 32 * 2}, |
||||
{"address[3]", nil, 32 * 3}, |
||||
{"bytes32[4]", nil, 32 * 4}, |
||||
// array array
|
||||
{"uint256[2][3][4]", nil, 32 * (2 * 3 * 4)}, |
||||
// array tuple
|
||||
{"tuple[2]", []ArgumentMarshaling{{Name: "x", Type: "bytes32"}, {Name: "y", Type: "bytes32"}}, (32 * 2) * 2}, |
||||
// simple tuple
|
||||
{"tuple", []ArgumentMarshaling{{Name: "x", Type: "uint256"}, {Name: "y", Type: "uint256"}}, 32 * 2}, |
||||
// tuple array
|
||||
{"tuple", []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}}, 32 * 2}, |
||||
// tuple tuple
|
||||
{"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32"}}}}, 32}, |
||||
{"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}, {Name: "y", Type: "uint256"}}}}, 32 * (2 + 1)}, |
||||
} |
||||
|
||||
for i, data := range testCases { |
||||
typ, err := NewType(data.typ, "", data.components) |
||||
if err != nil { |
||||
t.Errorf("type %q: failed to parse type string: %v", data.typ, err) |
||||
} |
||||
|
||||
result := getTypeSize(typ) |
||||
if result != data.typSize { |
||||
t.Errorf("case %d type %q: get type size error: actual: %d expected: %d", i, data.typ, result, data.typSize) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestNewFixedBytesOver32(t *testing.T) { |
||||
_, err := NewType("bytes4096", "", nil) |
||||
if err == nil { |
||||
t.Errorf("fixed bytes with size over 32 is not spec'd") |
||||
} |
||||
} |
@ -1,297 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"fmt" |
||||
"math/big" |
||||
"reflect" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
var ( |
||||
// MaxUint256 is the maximum value that can be represented by a uint256.
|
||||
MaxUint256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1) |
||||
// MaxInt256 is the maximum value that can be represented by a int256.
|
||||
MaxInt256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 255), common.Big1) |
||||
) |
||||
|
||||
// ReadInteger reads the integer based on its kind and returns the appropriate value.
|
||||
func ReadInteger(typ Type, b []byte) interface{} { |
||||
if typ.T == UintTy { |
||||
switch typ.Size { |
||||
case 8: |
||||
return b[len(b)-1] |
||||
case 16: |
||||
return binary.BigEndian.Uint16(b[len(b)-2:]) |
||||
case 32: |
||||
return binary.BigEndian.Uint32(b[len(b)-4:]) |
||||
case 64: |
||||
return binary.BigEndian.Uint64(b[len(b)-8:]) |
||||
default: |
||||
// the only case left for unsigned integer is uint256.
|
||||
return new(big.Int).SetBytes(b) |
||||
} |
||||
} |
||||
switch typ.Size { |
||||
case 8: |
||||
return int8(b[len(b)-1]) |
||||
case 16: |
||||
return int16(binary.BigEndian.Uint16(b[len(b)-2:])) |
||||
case 32: |
||||
return int32(binary.BigEndian.Uint32(b[len(b)-4:])) |
||||
case 64: |
||||
return int64(binary.BigEndian.Uint64(b[len(b)-8:])) |
||||
default: |
||||
// the only case left for integer is int256
|
||||
// big.SetBytes can't tell if a number is negative or positive in itself.
|
||||
// On EVM, if the returned number > max int256, it is negative.
|
||||
// A number is > max int256 if the bit at position 255 is set.
|
||||
ret := new(big.Int).SetBytes(b) |
||||
if ret.Bit(255) == 1 { |
||||
ret.Add(MaxUint256, new(big.Int).Neg(ret)) |
||||
ret.Add(ret, common.Big1) |
||||
ret.Neg(ret) |
||||
} |
||||
return ret |
||||
} |
||||
} |
||||
|
||||
// readBool reads a bool.
|
||||
func readBool(word []byte) (bool, error) { |
||||
for _, b := range word[:31] { |
||||
if b != 0 { |
||||
return false, errBadBool |
||||
} |
||||
} |
||||
switch word[31] { |
||||
case 0: |
||||
return false, nil |
||||
case 1: |
||||
return true, nil |
||||
default: |
||||
return false, errBadBool |
||||
} |
||||
} |
||||
|
||||
// A function type is simply the address with the function selection signature at the end.
|
||||
//
|
||||
// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
|
||||
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) { |
||||
if t.T != FunctionTy { |
||||
return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array") |
||||
} |
||||
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 { |
||||
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word) |
||||
} else { |
||||
copy(funcTy[:], word[0:24]) |
||||
} |
||||
return |
||||
} |
||||
|
||||
// ReadFixedBytes uses reflection to create a fixed array to be read from.
|
||||
func ReadFixedBytes(t Type, word []byte) (interface{}, error) { |
||||
if t.T != FixedBytesTy { |
||||
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array") |
||||
} |
||||
// convert
|
||||
array := reflect.New(t.GetType()).Elem() |
||||
|
||||
reflect.Copy(array, reflect.ValueOf(word[0:t.Size])) |
||||
return array.Interface(), nil |
||||
} |
||||
|
||||
// forEachUnpack iteratively unpack elements.
|
||||
func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) { |
||||
if size < 0 { |
||||
return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size) |
||||
} |
||||
if start+32*size > len(output) { |
||||
return nil, fmt.Errorf("abi: cannot marshal into go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size) |
||||
} |
||||
|
||||
// this value will become our slice or our array, depending on the type
|
||||
var refSlice reflect.Value |
||||
|
||||
if t.T == SliceTy { |
||||
// declare our slice
|
||||
refSlice = reflect.MakeSlice(t.GetType(), size, size) |
||||
} else if t.T == ArrayTy { |
||||
// declare our array
|
||||
refSlice = reflect.New(t.GetType()).Elem() |
||||
} else { |
||||
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage") |
||||
} |
||||
|
||||
// Arrays have packed elements, resulting in longer unpack steps.
|
||||
// Slices have just 32 bytes per element (pointing to the contents).
|
||||
elemSize := getTypeSize(*t.Elem) |
||||
|
||||
for i, j := start, 0; j < size; i, j = i+elemSize, j+1 { |
||||
inter, err := toGoType(i, *t.Elem, output) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
// append the item to our reflect slice
|
||||
refSlice.Index(j).Set(reflect.ValueOf(inter)) |
||||
} |
||||
|
||||
// return the interface
|
||||
return refSlice.Interface(), nil |
||||
} |
||||
|
||||
func forTupleUnpack(t Type, output []byte) (interface{}, error) { |
||||
retval := reflect.New(t.GetType()).Elem() |
||||
virtualArgs := 0 |
||||
for index, elem := range t.TupleElems { |
||||
marshalledValue, err := toGoType((index+virtualArgs)*32, *elem, output) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if elem.T == ArrayTy && !isDynamicType(*elem) { |
||||
// If we have a static array, like [3]uint256, these are coded as
|
||||
// just like uint256,uint256,uint256.
|
||||
// This means that we need to add two 'virtual' arguments when
|
||||
// we count the index from now on.
|
||||
//
|
||||
// Array values nested multiple levels deep are also encoded inline:
|
||||
// [2][3]uint256: uint256,uint256,uint256,uint256,uint256,uint256
|
||||
//
|
||||
// Calculate the full array size to get the correct offset for the next argument.
|
||||
// Decrement it by 1, as the normal index increment is still applied.
|
||||
virtualArgs += getTypeSize(*elem)/32 - 1 |
||||
} else if elem.T == TupleTy && !isDynamicType(*elem) { |
||||
// If we have a static tuple, like (uint256, bool, uint256), these are
|
||||
// coded as just like uint256,bool,uint256
|
||||
virtualArgs += getTypeSize(*elem)/32 - 1 |
||||
} |
||||
retval.Field(index).Set(reflect.ValueOf(marshalledValue)) |
||||
} |
||||
return retval.Interface(), nil |
||||
} |
||||
|
||||
// toGoType parses the output bytes and recursively assigns the value of these bytes
|
||||
// into a go type with accordance with the ABI spec.
|
||||
func toGoType(index int, t Type, output []byte) (interface{}, error) { |
||||
if index+32 > len(output) { |
||||
return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32) |
||||
} |
||||
|
||||
var ( |
||||
returnOutput []byte |
||||
begin, length int |
||||
err error |
||||
) |
||||
|
||||
// if we require a length prefix, find the beginning word and size returned.
|
||||
if t.requiresLengthPrefix() { |
||||
begin, length, err = lengthPrefixPointsTo(index, output) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} else { |
||||
returnOutput = output[index : index+32] |
||||
} |
||||
|
||||
switch t.T { |
||||
case TupleTy: |
||||
if isDynamicType(t) { |
||||
begin, err := tuplePointsTo(index, output) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return forTupleUnpack(t, output[begin:]) |
||||
} |
||||
return forTupleUnpack(t, output[index:]) |
||||
case SliceTy: |
||||
return forEachUnpack(t, output[begin:], 0, length) |
||||
case ArrayTy: |
||||
if isDynamicType(*t.Elem) { |
||||
offset := binary.BigEndian.Uint64(returnOutput[len(returnOutput)-8:]) |
||||
if offset > uint64(len(output)) { |
||||
return nil, fmt.Errorf("abi: toGoType offset greater than output length: offset: %d, len(output): %d", offset, len(output)) |
||||
} |
||||
return forEachUnpack(t, output[offset:], 0, t.Size) |
||||
} |
||||
return forEachUnpack(t, output[index:], 0, t.Size) |
||||
case StringTy: // variable arrays are written at the end of the return bytes
|
||||
return string(output[begin : begin+length]), nil |
||||
case IntTy, UintTy: |
||||
return ReadInteger(t, returnOutput), nil |
||||
case BoolTy: |
||||
return readBool(returnOutput) |
||||
case AddressTy: |
||||
return common.BytesToAddress(returnOutput), nil |
||||
case HashTy: |
||||
return common.BytesToHash(returnOutput), nil |
||||
case BytesTy: |
||||
return output[begin : begin+length], nil |
||||
case FixedBytesTy: |
||||
return ReadFixedBytes(t, returnOutput) |
||||
case FunctionTy: |
||||
return readFunctionType(t, returnOutput) |
||||
default: |
||||
return nil, fmt.Errorf("abi: unknown type %v", t.T) |
||||
} |
||||
} |
||||
|
||||
// lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type.
|
||||
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) { |
||||
bigOffsetEnd := new(big.Int).SetBytes(output[index : index+32]) |
||||
bigOffsetEnd.Add(bigOffsetEnd, common.Big32) |
||||
outputLength := big.NewInt(int64(len(output))) |
||||
|
||||
if bigOffsetEnd.Cmp(outputLength) > 0 { |
||||
return 0, 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", bigOffsetEnd, outputLength) |
||||
} |
||||
|
||||
if bigOffsetEnd.BitLen() > 63 { |
||||
return 0, 0, fmt.Errorf("abi offset larger than int64: %v", bigOffsetEnd) |
||||
} |
||||
|
||||
offsetEnd := int(bigOffsetEnd.Uint64()) |
||||
lengthBig := new(big.Int).SetBytes(output[offsetEnd-32 : offsetEnd]) |
||||
|
||||
totalSize := new(big.Int).Add(bigOffsetEnd, lengthBig) |
||||
if totalSize.BitLen() > 63 { |
||||
return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize) |
||||
} |
||||
|
||||
if totalSize.Cmp(outputLength) > 0 { |
||||
return 0, 0, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %v require %v", outputLength, totalSize) |
||||
} |
||||
start = int(bigOffsetEnd.Uint64()) |
||||
length = int(lengthBig.Uint64()) |
||||
return |
||||
} |
||||
|
||||
// tuplePointsTo resolves the location reference for dynamic tuple.
|
||||
func tuplePointsTo(index int, output []byte) (start int, err error) { |
||||
offset := new(big.Int).SetBytes(output[index : index+32]) |
||||
outputLen := big.NewInt(int64(len(output))) |
||||
|
||||
if offset.Cmp(outputLen) > 0 { |
||||
return 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", offset, outputLen) |
||||
} |
||||
if offset.BitLen() > 63 { |
||||
return 0, fmt.Errorf("abi offset larger than int64: %v", offset) |
||||
} |
||||
return int(offset.Uint64()), nil |
||||
} |
@ -1,945 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/hex" |
||||
"fmt" |
||||
"math/big" |
||||
"reflect" |
||||
"strconv" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
// TestUnpack tests the general pack/unpack tests in packing_test.go
|
||||
func TestUnpack(t *testing.T) { |
||||
for i, test := range packUnpackTests { |
||||
t.Run(strconv.Itoa(i)+" "+test.def, func(t *testing.T) { |
||||
//Unpack
|
||||
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def) |
||||
abi, err := JSON(strings.NewReader(def)) |
||||
if err != nil { |
||||
t.Fatalf("invalid ABI definition %s: %v", def, err) |
||||
} |
||||
encb, err := hex.DecodeString(test.packed) |
||||
if err != nil { |
||||
t.Fatalf("invalid hex %s: %v", test.packed, err) |
||||
} |
||||
out, err := abi.Unpack("method", encb) |
||||
if err != nil { |
||||
t.Errorf("test %d (%v) failed: %v", i, test.def, err) |
||||
return |
||||
} |
||||
if !reflect.DeepEqual(test.unpacked, ConvertType(out[0], test.unpacked)) { |
||||
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.unpacked, out[0]) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
type unpackTest struct { |
||||
def string // ABI definition JSON
|
||||
enc string // evm return data
|
||||
want interface{} // the expected output
|
||||
err string // empty or error if expected
|
||||
} |
||||
|
||||
func (test unpackTest) checkError(err error) error { |
||||
if err != nil { |
||||
if len(test.err) == 0 { |
||||
return fmt.Errorf("expected no err but got: %v", err) |
||||
} else if err.Error() != test.err { |
||||
return fmt.Errorf("expected err: '%v' got err: %q", test.err, err) |
||||
} |
||||
} else if len(test.err) > 0 { |
||||
return fmt.Errorf("expected err: %v but got none", test.err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
var unpackTests = []unpackTest{ |
||||
// Bools
|
||||
{ |
||||
def: `[{ "type": "bool" }]`, |
||||
enc: "0000000000000000000000000000000000000000000000000001000000000001", |
||||
want: false, |
||||
err: "abi: improperly encoded boolean value", |
||||
}, |
||||
{ |
||||
def: `[{ "type": "bool" }]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000003", |
||||
want: false, |
||||
err: "abi: improperly encoded boolean value", |
||||
}, |
||||
// Integers
|
||||
{ |
||||
def: `[{"type": "uint32"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
want: uint16(0), |
||||
err: "abi: cannot unmarshal uint32 in to uint16", |
||||
}, |
||||
{ |
||||
def: `[{"type": "uint17"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
want: uint16(0), |
||||
err: "abi: cannot unmarshal *big.Int in to uint16", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int32"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
want: int16(0), |
||||
err: "abi: cannot unmarshal int32 in to int16", |
||||
}, |
||||
{ |
||||
def: `[{"type": "int17"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000001", |
||||
want: int16(0), |
||||
err: "abi: cannot unmarshal *big.Int in to int16", |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes"}]`, |
||||
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", |
||||
want: [32]byte{1}, |
||||
}, |
||||
{ |
||||
def: `[{"type": "bytes32"}]`, |
||||
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000", |
||||
want: []byte(nil), |
||||
err: "abi: cannot unmarshal [32]uint8 in to []uint8", |
||||
}, |
||||
{ |
||||
def: `[{"name":"___","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
IntOne *big.Int |
||||
Intone *big.Int |
||||
}{IntOne: big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{}, |
||||
err: "abi: multiple outputs mapping to the same struct field 'IntOne'", |
||||
}, |
||||
{ |
||||
def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{}, |
||||
err: "abi: multiple outputs mapping to the same struct field 'Int'", |
||||
}, |
||||
{ |
||||
def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{}, |
||||
err: "abi: multiple outputs mapping to the same struct field 'Int'", |
||||
}, |
||||
{ |
||||
def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{}, |
||||
err: "abi: multiple outputs mapping to the same struct field 'Int'", |
||||
}, |
||||
{ |
||||
def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
Int1 *big.Int |
||||
Int2 *big.Int |
||||
}{}, |
||||
err: "abi: purely underscored output cannot unpack to struct", |
||||
}, |
||||
// Make sure only the first argument is consumed
|
||||
{ |
||||
def: `[{"name":"int_one","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"name":"int__one","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"name":"int_one_","type":"int256"}]`, |
||||
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002", |
||||
want: struct { |
||||
IntOne *big.Int |
||||
}{big.NewInt(1)}, |
||||
}, |
||||
{ |
||||
def: `[{"type":"bool"}]`, |
||||
enc: "", |
||||
want: false, |
||||
err: "abi: attempting to unmarshall an empty string while arguments are expected", |
||||
}, |
||||
{ |
||||
def: `[{"type":"bytes32","indexed":true},{"type":"uint256","indexed":false}]`, |
||||
enc: "", |
||||
want: false, |
||||
err: "abi: attempting to unmarshall an empty string while arguments are expected", |
||||
}, |
||||
{ |
||||
def: `[{"type":"bool","indexed":true},{"type":"uint64","indexed":true}]`, |
||||
enc: "", |
||||
want: false, |
||||
}, |
||||
} |
||||
|
||||
// TestLocalUnpackTests runs test specially designed only for unpacking.
|
||||
// All test cases that can be used to test packing and unpacking should move to packing_test.go
|
||||
func TestLocalUnpackTests(t *testing.T) { |
||||
for i, test := range unpackTests { |
||||
t.Run(strconv.Itoa(i), func(t *testing.T) { |
||||
//Unpack
|
||||
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def) |
||||
abi, err := JSON(strings.NewReader(def)) |
||||
if err != nil { |
||||
t.Fatalf("invalid ABI definition %s: %v", def, err) |
||||
} |
||||
encb, err := hex.DecodeString(test.enc) |
||||
if err != nil { |
||||
t.Fatalf("invalid hex %s: %v", test.enc, err) |
||||
} |
||||
outptr := reflect.New(reflect.TypeOf(test.want)) |
||||
err = abi.UnpackIntoInterface(outptr.Interface(), "method", encb) |
||||
if err := test.checkError(err); err != nil { |
||||
t.Errorf("test %d (%v) failed: %v", i, test.def, err) |
||||
return |
||||
} |
||||
out := outptr.Elem().Interface() |
||||
if !reflect.DeepEqual(test.want, out) { |
||||
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) { |
||||
abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
var ( |
||||
marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000") |
||||
marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000") |
||||
|
||||
out32 [][32]byte |
||||
out15 [][15]byte |
||||
) |
||||
|
||||
// test 32
|
||||
err = abi.UnpackIntoInterface(&out32, "testDynamicFixedBytes32", marshalledReturn32) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if len(out32) != 2 { |
||||
t.Fatalf("expected array with 2 values, got %d", len(out32)) |
||||
} |
||||
expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000") |
||||
if !bytes.Equal(out32[0][:], expected) { |
||||
t.Errorf("expected %x, got %x\n", expected, out32[0]) |
||||
} |
||||
expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000") |
||||
if !bytes.Equal(out32[1][:], expected) { |
||||
t.Errorf("expected %x, got %x\n", expected, out32[1]) |
||||
} |
||||
|
||||
// test 15
|
||||
err = abi.UnpackIntoInterface(&out15, "testDynamicFixedBytes32", marshalledReturn15) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if len(out15) != 2 { |
||||
t.Fatalf("expected array with 2 values, got %d", len(out15)) |
||||
} |
||||
expected = common.Hex2Bytes("307830313233343500000000000000") |
||||
if !bytes.Equal(out15[0][:], expected) { |
||||
t.Errorf("expected %x, got %x\n", expected, out15[0]) |
||||
} |
||||
expected = common.Hex2Bytes("307839383736353400000000000000") |
||||
if !bytes.Equal(out15[1][:], expected) { |
||||
t.Errorf("expected %x, got %x\n", expected, out15[1]) |
||||
} |
||||
} |
||||
|
||||
type methodMultiOutput struct { |
||||
Int *big.Int |
||||
String string |
||||
} |
||||
|
||||
func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { |
||||
const definition = `[ |
||||
{ "name" : "multi", "type": "function", "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` |
||||
var expected = methodMultiOutput{big.NewInt(1), "hello"} |
||||
|
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
require.NoError(err) |
||||
// using buff to make the code readable
|
||||
buff := new(bytes.Buffer) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) |
||||
buff.Write(common.RightPadBytes([]byte(expected.String), 32)) |
||||
return abi, buff.Bytes(), expected |
||||
} |
||||
|
||||
func TestMethodMultiReturn(t *testing.T) { |
||||
type reversed struct { |
||||
String string |
||||
Int *big.Int |
||||
} |
||||
|
||||
newInterfaceSlice := func(len int) interface{} { |
||||
slice := make([]interface{}, len) |
||||
return &slice |
||||
} |
||||
|
||||
abi, data, expected := methodMultiReturn(require.New(t)) |
||||
bigint := new(big.Int) |
||||
var testCases = []struct { |
||||
dest interface{} |
||||
expected interface{} |
||||
error string |
||||
name string |
||||
}{{ |
||||
&methodMultiOutput{}, |
||||
&expected, |
||||
"", |
||||
"Can unpack into structure", |
||||
}, { |
||||
&reversed{}, |
||||
&reversed{expected.String, expected.Int}, |
||||
"", |
||||
"Can unpack into reversed structure", |
||||
}, { |
||||
&[]interface{}{&bigint, new(string)}, |
||||
&[]interface{}{&expected.Int, &expected.String}, |
||||
"", |
||||
"Can unpack into a slice", |
||||
}, { |
||||
&[]interface{}{&bigint, ""}, |
||||
&[]interface{}{&expected.Int, expected.String}, |
||||
"", |
||||
"Can unpack into a slice without indirection", |
||||
}, { |
||||
&[2]interface{}{&bigint, new(string)}, |
||||
&[2]interface{}{&expected.Int, &expected.String}, |
||||
"", |
||||
"Can unpack into an array", |
||||
}, { |
||||
&[2]interface{}{}, |
||||
&[2]interface{}{expected.Int, expected.String}, |
||||
"", |
||||
"Can unpack into interface array", |
||||
}, { |
||||
newInterfaceSlice(2), |
||||
&[]interface{}{expected.Int, expected.String}, |
||||
"", |
||||
"Can unpack into interface slice", |
||||
}, { |
||||
&[]interface{}{new(int), new(int)}, |
||||
&[]interface{}{&expected.Int, &expected.String}, |
||||
"abi: cannot unmarshal *big.Int in to int", |
||||
"Can not unpack into a slice with wrong types", |
||||
}, { |
||||
&[]interface{}{new(int)}, |
||||
&[]interface{}{}, |
||||
"abi: insufficient number of arguments for unpack, want 2, got 1", |
||||
"Can not unpack into a slice with wrong types", |
||||
}} |
||||
for _, tc := range testCases { |
||||
tc := tc |
||||
t.Run(tc.name, func(t *testing.T) { |
||||
require := require.New(t) |
||||
err := abi.UnpackIntoInterface(tc.dest, "multi", data) |
||||
if tc.error == "" { |
||||
require.Nil(err, "Should be able to unpack method outputs.") |
||||
require.Equal(tc.expected, tc.dest) |
||||
} else { |
||||
require.EqualError(err, tc.error) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func TestMultiReturnWithArray(t *testing.T) { |
||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]` |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff := new(bytes.Buffer) |
||||
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) |
||||
|
||||
ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9} |
||||
ret2, ret2Exp := new(uint64), uint64(8) |
||||
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(*ret1, ret1Exp) { |
||||
t.Error("array result", *ret1, "!= Expected", ret1Exp) |
||||
} |
||||
if *ret2 != ret2Exp { |
||||
t.Error("int result", *ret2, "!= Expected", ret2Exp) |
||||
} |
||||
} |
||||
|
||||
func TestMultiReturnWithStringArray(t *testing.T) { |
||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]` |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff := new(bytes.Buffer) |
||||
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) |
||||
temp, _ := new(big.Int).SetString("30000000000000000000", 10) |
||||
ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp} |
||||
ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f") |
||||
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"} |
||||
ret4, ret4Exp := new(bool), false |
||||
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(*ret1, ret1Exp) { |
||||
t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp) |
||||
} |
||||
if !reflect.DeepEqual(*ret2, ret2Exp) { |
||||
t.Error("address result", *ret2, "!= Expected", ret2Exp) |
||||
} |
||||
if !reflect.DeepEqual(*ret3, ret3Exp) { |
||||
t.Error("string array result", *ret3, "!= Expected", ret3Exp) |
||||
} |
||||
if !reflect.DeepEqual(*ret4, ret4Exp) { |
||||
t.Error("bool result", *ret4, "!= Expected", ret4Exp) |
||||
} |
||||
} |
||||
|
||||
func TestMultiReturnWithStringSlice(t *testing.T) { |
||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]` |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff := new(bytes.Buffer) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length
|
||||
buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value
|
||||
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length
|
||||
buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
|
||||
ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"} |
||||
ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)} |
||||
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(*ret1, ret1Exp) { |
||||
t.Error("string slice result", *ret1, "!= Expected", ret1Exp) |
||||
} |
||||
if !reflect.DeepEqual(*ret2, ret2Exp) { |
||||
t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp) |
||||
} |
||||
} |
||||
|
||||
func TestMultiReturnWithDeeplyNestedArray(t *testing.T) { |
||||
// Similar to TestMultiReturnWithArray, but with a special case in mind:
|
||||
// values of nested static arrays count towards the size as well, and any element following
|
||||
// after such nested array argument should be read with the correct offset,
|
||||
// so that it does not read content from the previous array argument.
|
||||
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]` |
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff := new(bytes.Buffer) |
||||
// construct the test array, each 3 char element is joined with 61 '0' chars,
|
||||
// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
|
||||
buff.Write(common.Hex2Bytes(strings.Join([]string{ |
||||
"", //empty, to apply the 61-char separator to the first element as well.
|
||||
"111", "112", "113", "121", "122", "123", |
||||
"211", "212", "213", "221", "222", "223", |
||||
"311", "312", "313", "321", "322", "323", |
||||
"411", "412", "413", "421", "422", "423", |
||||
}, "0000000000000000000000000000000000000000000000000000000000000"))) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876")) |
||||
|
||||
ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{ |
||||
{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}}, |
||||
{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}}, |
||||
{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}}, |
||||
{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}}, |
||||
} |
||||
ret2, ret2Exp := new(uint64), uint64(0x9876) |
||||
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(*ret1, ret1Exp) { |
||||
t.Error("array result", *ret1, "!= Expected", ret1Exp) |
||||
} |
||||
if *ret2 != ret2Exp { |
||||
t.Error("int result", *ret2, "!= Expected", ret2Exp) |
||||
} |
||||
} |
||||
|
||||
func TestUnmarshal(t *testing.T) { |
||||
const definition = `[ |
||||
{ "name" : "int", "type": "function", "outputs": [ { "type": "uint256" } ] }, |
||||
{ "name" : "bool", "type": "function", "outputs": [ { "type": "bool" } ] }, |
||||
{ "name" : "bytes", "type": "function", "outputs": [ { "type": "bytes" } ] }, |
||||
{ "name" : "fixed", "type": "function", "outputs": [ { "type": "bytes32" } ] }, |
||||
{ "name" : "multi", "type": "function", "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, |
||||
{ "name" : "intArraySingle", "type": "function", "outputs": [ { "type": "uint256[3]" } ] }, |
||||
{ "name" : "addressSliceSingle", "type": "function", "outputs": [ { "type": "address[]" } ] }, |
||||
{ "name" : "addressSliceDouble", "type": "function", "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, |
||||
{ "name" : "mixedBytes", "type": "function", "stateMutability" : "view", "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` |
||||
|
||||
abi, err := JSON(strings.NewReader(definition)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff := new(bytes.Buffer) |
||||
|
||||
// marshall mixed bytes (mixedBytes)
|
||||
p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000") |
||||
p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff") |
||||
mixedBytes := []interface{}{&p0, &p1} |
||||
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")) |
||||
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a")) |
||||
buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000")) |
||||
|
||||
err = abi.UnpackIntoInterface(&mixedBytes, "mixedBytes", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} else { |
||||
if !bytes.Equal(p0, p0Exp) { |
||||
t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0) |
||||
} |
||||
|
||||
if !bytes.Equal(p1[:], p1Exp) { |
||||
t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1) |
||||
} |
||||
} |
||||
|
||||
// marshal int
|
||||
var Int *big.Int |
||||
err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { |
||||
t.Error("expected Int to be 1 got", Int) |
||||
} |
||||
|
||||
// marshal bool
|
||||
var Bool bool |
||||
err = abi.UnpackIntoInterface(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !Bool { |
||||
t.Error("expected Bool to be true") |
||||
} |
||||
|
||||
// marshal dynamic bytes max length 32
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) |
||||
bytesOut := common.RightPadBytes([]byte("hello"), 32) |
||||
buff.Write(bytesOut) |
||||
|
||||
var Bytes []byte |
||||
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !bytes.Equal(Bytes, bytesOut) { |
||||
t.Errorf("expected %x got %x", bytesOut, Bytes) |
||||
} |
||||
|
||||
// marshall dynamic bytes max length 64
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) |
||||
bytesOut = common.RightPadBytes([]byte("hello"), 64) |
||||
buff.Write(bytesOut) |
||||
|
||||
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !bytes.Equal(Bytes, bytesOut) { |
||||
t.Errorf("expected %x got %x", bytesOut, Bytes) |
||||
} |
||||
|
||||
// marshall dynamic bytes max length 64
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) |
||||
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) |
||||
bytesOut = common.RightPadBytes([]byte("hello"), 64) |
||||
buff.Write(bytesOut) |
||||
|
||||
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) { |
||||
t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes) |
||||
} |
||||
|
||||
// marshal dynamic bytes output empty
|
||||
err = abi.UnpackIntoInterface(&Bytes, "bytes", nil) |
||||
if err == nil { |
||||
t.Error("expected error") |
||||
} |
||||
|
||||
// marshal dynamic bytes length 5
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) |
||||
buff.Write(common.RightPadBytes([]byte("hello"), 32)) |
||||
|
||||
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
if !bytes.Equal(Bytes, []byte("hello")) { |
||||
t.Errorf("expected %x got %x", bytesOut, Bytes) |
||||
} |
||||
|
||||
// marshal dynamic bytes length 5
|
||||
buff.Reset() |
||||
buff.Write(common.RightPadBytes([]byte("hello"), 32)) |
||||
|
||||
var hash common.Hash |
||||
err = abi.UnpackIntoInterface(&hash, "fixed", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
|
||||
helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) |
||||
if hash != helloHash { |
||||
t.Errorf("Expected %x to equal %x", hash, helloHash) |
||||
} |
||||
|
||||
// marshal error
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) |
||||
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes()) |
||||
if err == nil { |
||||
t.Error("expected error") |
||||
} |
||||
|
||||
err = abi.UnpackIntoInterface(&Bytes, "multi", make([]byte, 64)) |
||||
if err == nil { |
||||
t.Error("expected error") |
||||
} |
||||
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) |
||||
// marshal int array
|
||||
var intArray [3]*big.Int |
||||
err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
var testAgainstIntArray [3]*big.Int |
||||
testAgainstIntArray[0] = big.NewInt(1) |
||||
testAgainstIntArray[1] = big.NewInt(2) |
||||
testAgainstIntArray[2] = big.NewInt(3) |
||||
|
||||
for i, Int := range intArray { |
||||
if Int.Cmp(testAgainstIntArray[i]) != 0 { |
||||
t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) |
||||
} |
||||
} |
||||
// marshal address slice
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) |
||||
|
||||
var outAddr []common.Address |
||||
err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes()) |
||||
if err != nil { |
||||
t.Fatal("didn't expect error:", err) |
||||
} |
||||
|
||||
if len(outAddr) != 1 { |
||||
t.Fatal("expected 1 item, got", len(outAddr)) |
||||
} |
||||
|
||||
if outAddr[0] != (common.Address{1}) { |
||||
t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) |
||||
} |
||||
|
||||
// marshal multiple address slice
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) |
||||
|
||||
var outAddrStruct struct { |
||||
A []common.Address |
||||
B []common.Address |
||||
} |
||||
err = abi.UnpackIntoInterface(&outAddrStruct, "addressSliceDouble", buff.Bytes()) |
||||
if err != nil { |
||||
t.Fatal("didn't expect error:", err) |
||||
} |
||||
|
||||
if len(outAddrStruct.A) != 1 { |
||||
t.Fatal("expected 1 item, got", len(outAddrStruct.A)) |
||||
} |
||||
|
||||
if outAddrStruct.A[0] != (common.Address{1}) { |
||||
t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) |
||||
} |
||||
|
||||
if len(outAddrStruct.B) != 2 { |
||||
t.Fatal("expected 1 item, got", len(outAddrStruct.B)) |
||||
} |
||||
|
||||
if outAddrStruct.B[0] != (common.Address{2}) { |
||||
t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) |
||||
} |
||||
if outAddrStruct.B[1] != (common.Address{3}) { |
||||
t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) |
||||
} |
||||
|
||||
// marshal invalid address slice
|
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) |
||||
|
||||
err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes()) |
||||
if err == nil { |
||||
t.Fatal("expected error:", err) |
||||
} |
||||
} |
||||
|
||||
func TestUnpackTuple(t *testing.T) { |
||||
const simpleTuple = `[{"name":"tuple","type":"function","outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]` |
||||
abi, err := JSON(strings.NewReader(simpleTuple)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff := new(bytes.Buffer) |
||||
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1
|
||||
buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1
|
||||
|
||||
// If the result is single tuple, use struct as return value container directly.
|
||||
type v struct { |
||||
A *big.Int |
||||
B *big.Int |
||||
} |
||||
type r struct { |
||||
Result v |
||||
} |
||||
var ret0 = new(r) |
||||
err = abi.UnpackIntoInterface(ret0, "tuple", buff.Bytes()) |
||||
|
||||
if err != nil { |
||||
t.Error(err) |
||||
} else { |
||||
if ret0.Result.A.Cmp(big.NewInt(1)) != 0 { |
||||
t.Errorf("unexpected value unpacked: want %x, got %x", 1, ret0.Result.A) |
||||
} |
||||
if ret0.Result.B.Cmp(big.NewInt(-1)) != 0 { |
||||
t.Errorf("unexpected value unpacked: want %x, got %x", -1, ret0.Result.B) |
||||
} |
||||
} |
||||
|
||||
// Test nested tuple
|
||||
const nestedTuple = `[{"name":"tuple","type":"function","outputs":[ |
||||
{"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]}, |
||||
{"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}, |
||||
{"type":"uint256","name":"a"} |
||||
]}]` |
||||
|
||||
abi, err = JSON(strings.NewReader(nestedTuple)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
buff.Reset() |
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset
|
||||
buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X
|
||||
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y
|
||||
|
||||
type T struct { |
||||
X *big.Int `abi:"x"` |
||||
Z *big.Int `abi:"y"` // Test whether the abi tag works.
|
||||
} |
||||
|
||||
type S struct { |
||||
A *big.Int |
||||
B []*big.Int |
||||
C []T |
||||
} |
||||
|
||||
type Ret struct { |
||||
FieldS S `abi:"s"` |
||||
FieldT T `abi:"t"` |
||||
A *big.Int |
||||
} |
||||
var ret Ret |
||||
var expected = Ret{ |
||||
FieldS: S{ |
||||
A: big.NewInt(1), |
||||
B: []*big.Int{big.NewInt(1), big.NewInt(2)}, |
||||
C: []T{ |
||||
{big.NewInt(1), big.NewInt(2)}, |
||||
{big.NewInt(2), big.NewInt(1)}, |
||||
}, |
||||
}, |
||||
FieldT: T{ |
||||
big.NewInt(0), big.NewInt(1), |
||||
}, |
||||
A: big.NewInt(1), |
||||
} |
||||
|
||||
err = abi.UnpackIntoInterface(&ret, "tuple", buff.Bytes()) |
||||
if err != nil { |
||||
t.Error(err) |
||||
} |
||||
if reflect.DeepEqual(ret, expected) { |
||||
t.Error("unexpected unpack value") |
||||
} |
||||
} |
||||
|
||||
func TestOOMMaliciousInput(t *testing.T) { |
||||
oomTests := []unpackTest{ |
||||
{ |
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
{ // Length larger than 64 bits
|
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
||||
"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
{ // Offset very large (over 64 bits)
|
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
{ // Offset very large (below 64 bits)
|
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
{ // Offset negative (as 64 bit)
|
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
|
||||
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
|
||||
{ // Negative length
|
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
||||
"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
{ // Very large length
|
||||
def: `[{"type": "uint8[]"}]`, |
||||
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
||||
"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
|
||||
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
||||
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
||||
}, |
||||
} |
||||
for i, test := range oomTests { |
||||
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def) |
||||
abi, err := JSON(strings.NewReader(def)) |
||||
if err != nil { |
||||
t.Fatalf("invalid ABI definition %s: %v", def, err) |
||||
} |
||||
encb, err := hex.DecodeString(test.enc) |
||||
if err != nil { |
||||
t.Fatalf("invalid hex: %s" + test.enc) |
||||
} |
||||
_, err = abi.Methods["method"].Outputs.UnpackValues(encb) |
||||
if err == nil { |
||||
t.Fatalf("Expected error on malicious input, test %d", i) |
||||
} |
||||
} |
||||
} |
@ -1,40 +0,0 @@ |
||||
// Copyright 2022 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package abi |
||||
|
||||
import "fmt" |
||||
|
||||
// ResolveNameConflict returns the next available name for a given thing.
|
||||
// This helper can be used for lots of purposes:
|
||||
//
|
||||
// - In solidity function overloading is supported, this function can fix
|
||||
// the name conflicts of overloaded functions.
|
||||
// - In golang binding generation, the parameter(in function, event, error,
|
||||
// and struct definition) name will be converted to camelcase style which
|
||||
// may eventually lead to name conflicts.
|
||||
//
|
||||
// Name conflicts are mostly resolved by adding number suffix. e.g. if the abi contains
|
||||
// Methods "send" and "send1", ResolveNameConflict would return "send2" for input "send".
|
||||
func ResolveNameConflict(rawName string, used func(string) bool) string { |
||||
name := rawName |
||||
ok := used(name) |
||||
for idx := 0; ok; idx++ { |
||||
name = fmt.Sprintf("%s%d", rawName, idx) |
||||
ok = used(name) |
||||
} |
||||
return name |
||||
} |
@ -1,226 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Package accounts implements high level Ethereum account management.
|
||||
package accounts |
||||
|
||||
import ( |
||||
"fmt" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
"golang.org/x/crypto/sha3" |
||||
) |
||||
|
||||
// Account represents an Ethereum account located at a specific location defined
|
||||
// by the optional URL field.
|
||||
type Account struct { |
||||
Address common.Address `json:"address"` // Ethereum account address derived from the key
|
||||
URL URL `json:"url"` // Optional resource locator within a backend
|
||||
} |
||||
|
||||
const ( |
||||
MimetypeDataWithValidator = "data/validator" |
||||
MimetypeTypedData = "data/typed" |
||||
MimetypeClique = "application/x-clique-header" |
||||
MimetypeTextPlain = "text/plain" |
||||
) |
||||
|
||||
// Wallet represents a software or hardware wallet that might contain one or more
|
||||
// accounts (derived from the same seed).
|
||||
type Wallet interface { |
||||
// URL retrieves the canonical path under which this wallet is reachable. It is
|
||||
// used by upper layers to define a sorting order over all wallets from multiple
|
||||
// backends.
|
||||
URL() URL |
||||
|
||||
// Status returns a textual status to aid the user in the current state of the
|
||||
// wallet. It also returns an error indicating any failure the wallet might have
|
||||
// encountered.
|
||||
Status() (string, error) |
||||
|
||||
// Open initializes access to a wallet instance. It is not meant to unlock or
|
||||
// decrypt account keys, rather simply to establish a connection to hardware
|
||||
// wallets and/or to access derivation seeds.
|
||||
//
|
||||
// The passphrase parameter may or may not be used by the implementation of a
|
||||
// particular wallet instance. The reason there is no passwordless open method
|
||||
// is to strive towards a uniform wallet handling, oblivious to the different
|
||||
// backend providers.
|
||||
//
|
||||
// Please note, if you open a wallet, you must close it to release any allocated
|
||||
// resources (especially important when working with hardware wallets).
|
||||
Open(passphrase string) error |
||||
|
||||
// Close releases any resources held by an open wallet instance.
|
||||
Close() error |
||||
|
||||
// Accounts retrieves the list of signing accounts the wallet is currently aware
|
||||
// of. For hierarchical deterministic wallets, the list will not be exhaustive,
|
||||
// rather only contain the accounts explicitly pinned during account derivation.
|
||||
Accounts() []Account |
||||
|
||||
// Contains returns whether an account is part of this particular wallet or not.
|
||||
Contains(account Account) bool |
||||
|
||||
// Derive attempts to explicitly derive a hierarchical deterministic account at
|
||||
// the specified derivation path. If requested, the derived account will be added
|
||||
// to the wallet's tracked account list.
|
||||
Derive(path DerivationPath, pin bool) (Account, error) |
||||
|
||||
// SelfDerive sets a base account derivation path from which the wallet attempts
|
||||
// to discover non zero accounts and automatically add them to list of tracked
|
||||
// accounts.
|
||||
//
|
||||
// Note, self derivation will increment the last component of the specified path
|
||||
// opposed to descending into a child path to allow discovering accounts starting
|
||||
// from non zero components.
|
||||
//
|
||||
// Some hardware wallets switched derivation paths through their evolution, so
|
||||
// this method supports providing multiple bases to discover old user accounts
|
||||
// too. Only the last base will be used to derive the next empty account.
|
||||
//
|
||||
// You can disable automatic account discovery by calling SelfDerive with a nil
|
||||
// chain state reader.
|
||||
SelfDerive(bases []DerivationPath, chain ethereum.ChainStateReader) |
||||
|
||||
// SignData requests the wallet to sign the hash of the given data
|
||||
// It looks up the account specified either solely via its address contained within,
|
||||
// or optionally with the aid of any location metadata from the embedded URL field.
|
||||
//
|
||||
// If the wallet requires additional authentication to sign the request (e.g.
|
||||
// a password to decrypt the account, or a PIN code to verify the transaction),
|
||||
// an AuthNeededError instance will be returned, containing infos for the user
|
||||
// about which fields or actions are needed. The user may retry by providing
|
||||
// the needed details via SignDataWithPassphrase, or by other means (e.g. unlock
|
||||
// the account in a keystore).
|
||||
SignData(account Account, mimeType string, data []byte) ([]byte, error) |
||||
|
||||
// SignDataWithPassphrase is identical to SignData, but also takes a password
|
||||
// NOTE: there's a chance that an erroneous call might mistake the two strings, and
|
||||
// supply password in the mimetype field, or vice versa. Thus, an implementation
|
||||
// should never echo the mimetype or return the mimetype in the error-response
|
||||
SignDataWithPassphrase(account Account, passphrase, mimeType string, data []byte) ([]byte, error) |
||||
|
||||
// SignText requests the wallet to sign the hash of a given piece of data, prefixed
|
||||
// by the Ethereum prefix scheme
|
||||
// It looks up the account specified either solely via its address contained within,
|
||||
// or optionally with the aid of any location metadata from the embedded URL field.
|
||||
//
|
||||
// If the wallet requires additional authentication to sign the request (e.g.
|
||||
// a password to decrypt the account, or a PIN code to verify the transaction),
|
||||
// an AuthNeededError instance will be returned, containing infos for the user
|
||||
// about which fields or actions are needed. The user may retry by providing
|
||||
// the needed details via SignTextWithPassphrase, or by other means (e.g. unlock
|
||||
// the account in a keystore).
|
||||
//
|
||||
// This method should return the signature in 'canonical' format, with v 0 or 1.
|
||||
SignText(account Account, text []byte) ([]byte, error) |
||||
|
||||
// SignTextWithPassphrase is identical to Signtext, but also takes a password
|
||||
SignTextWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error) |
||||
|
||||
// SignTx requests the wallet to sign the given transaction.
|
||||
//
|
||||
// It looks up the account specified either solely via its address contained within,
|
||||
// or optionally with the aid of any location metadata from the embedded URL field.
|
||||
//
|
||||
// If the wallet requires additional authentication to sign the request (e.g.
|
||||
// a password to decrypt the account, or a PIN code to verify the transaction),
|
||||
// an AuthNeededError instance will be returned, containing infos for the user
|
||||
// about which fields or actions are needed. The user may retry by providing
|
||||
// the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
|
||||
// the account in a keystore).
|
||||
SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) |
||||
|
||||
// SignTxWithPassphrase is identical to SignTx, but also takes a password
|
||||
SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) |
||||
} |
||||
|
||||
// Backend is a "wallet provider" that may contain a batch of accounts they can
|
||||
// sign transactions with and upon request, do so.
|
||||
type Backend interface { |
||||
// Wallets retrieves the list of wallets the backend is currently aware of.
|
||||
//
|
||||
// The returned wallets are not opened by default. For software HD wallets this
|
||||
// means that no base seeds are decrypted, and for hardware wallets that no actual
|
||||
// connection is established.
|
||||
//
|
||||
// The resulting wallet list will be sorted alphabetically based on its internal
|
||||
// URL assigned by the backend. Since wallets (especially hardware) may come and
|
||||
// go, the same wallet might appear at a different positions in the list during
|
||||
// subsequent retrievals.
|
||||
Wallets() []Wallet |
||||
|
||||
// Subscribe creates an async subscription to receive notifications when the
|
||||
// backend detects the arrival or departure of a wallet.
|
||||
Subscribe(sink chan<- WalletEvent) event.Subscription |
||||
} |
||||
|
||||
// TextHash is a helper function that calculates a hash for the given message that can be
|
||||
// safely used to calculate a signature from.
|
||||
//
|
||||
// The hash is calculated as
|
||||
//
|
||||
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
||||
//
|
||||
// This gives context to the signed message and prevents signing of transactions.
|
||||
func TextHash(data []byte) []byte { |
||||
hash, _ := TextAndHash(data) |
||||
return hash |
||||
} |
||||
|
||||
// TextAndHash is a helper function that calculates a hash for the given message that can be
|
||||
// safely used to calculate a signature from.
|
||||
//
|
||||
// The hash is calculated as
|
||||
//
|
||||
// keccak256("\x19Ethereum Signed Message:\n"${message length}${message}).
|
||||
//
|
||||
// This gives context to the signed message and prevents signing of transactions.
|
||||
func TextAndHash(data []byte) ([]byte, string) { |
||||
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), string(data)) |
||||
hasher := sha3.NewLegacyKeccak256() |
||||
hasher.Write([]byte(msg)) |
||||
return hasher.Sum(nil), msg |
||||
} |
||||
|
||||
// WalletEventType represents the different event types that can be fired by
|
||||
// the wallet subscription subsystem.
|
||||
type WalletEventType int |
||||
|
||||
const ( |
||||
// WalletArrived is fired when a new wallet is detected either via USB or via
|
||||
// a filesystem event in the keystore.
|
||||
WalletArrived WalletEventType = iota |
||||
|
||||
// WalletOpened is fired when a wallet is successfully opened with the purpose
|
||||
// of starting any background processes such as automatic key derivation.
|
||||
WalletOpened |
||||
|
||||
// WalletDropped
|
||||
WalletDropped |
||||
) |
||||
|
||||
// WalletEvent is an event fired by an account backend when a wallet arrival or
|
||||
// departure is detected.
|
||||
type WalletEvent struct { |
||||
Wallet Wallet // Wallet instance arrived or departed
|
||||
Kind WalletEventType // Event type that happened in the system
|
||||
} |
@ -1,32 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package accounts |
||||
|
||||
import ( |
||||
"bytes" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
) |
||||
|
||||
func TestTextHash(t *testing.T) { |
||||
hash := TextHash([]byte("Hello Joe")) |
||||
want := hexutil.MustDecode("0xa080337ae51c4e064c189e113edd0ba391df9206e2f49db658bb32cf2911730b") |
||||
if !bytes.Equal(hash, want) { |
||||
t.Fatalf("wrong hash: %x", hash) |
||||
} |
||||
} |
@ -1,67 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package accounts |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
) |
||||
|
||||
// ErrUnknownAccount is returned for any requested operation for which no backend
|
||||
// provides the specified account.
|
||||
var ErrUnknownAccount = errors.New("unknown account") |
||||
|
||||
// ErrUnknownWallet is returned for any requested operation for which no backend
|
||||
// provides the specified wallet.
|
||||
var ErrUnknownWallet = errors.New("unknown wallet") |
||||
|
||||
// ErrNotSupported is returned when an operation is requested from an account
|
||||
// backend that it does not support.
|
||||
var ErrNotSupported = errors.New("not supported") |
||||
|
||||
// ErrInvalidPassphrase is returned when a decryption operation receives a bad
|
||||
// passphrase.
|
||||
var ErrInvalidPassphrase = errors.New("invalid password") |
||||
|
||||
// ErrWalletAlreadyOpen is returned if a wallet is attempted to be opened the
|
||||
// second time.
|
||||
var ErrWalletAlreadyOpen = errors.New("wallet already open") |
||||
|
||||
// ErrWalletClosed is returned if a wallet is offline.
|
||||
var ErrWalletClosed = errors.New("wallet closed") |
||||
|
||||
// AuthNeededError is returned by backends for signing requests where the user
|
||||
// is required to provide further authentication before signing can succeed.
|
||||
//
|
||||
// This usually means either that a password needs to be supplied, or perhaps a
|
||||
// one time PIN code displayed by some hardware device.
|
||||
type AuthNeededError struct { |
||||
Needed string // Extra authentication the user needs to provide
|
||||
} |
||||
|
||||
// NewAuthNeededError creates a new authentication error with the extra details
|
||||
// about the needed fields set.
|
||||
func NewAuthNeededError(needed string) error { |
||||
return &AuthNeededError{ |
||||
Needed: needed, |
||||
} |
||||
} |
||||
|
||||
// Error implements the standard error interface.
|
||||
func (err *AuthNeededError) Error() string { |
||||
return fmt.Sprintf("authentication needed: %s", err.Needed) |
||||
} |
@ -1,269 +0,0 @@ |
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package external |
||||
|
||||
import ( |
||||
"fmt" |
||||
"math/big" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/hexutil" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
"github.com/ethereum/go-ethereum/signer/core/apitypes" |
||||
) |
||||
|
||||
type ExternalBackend struct { |
||||
signers []accounts.Wallet |
||||
} |
||||
|
||||
func (eb *ExternalBackend) Wallets() []accounts.Wallet { |
||||
return eb.signers |
||||
} |
||||
|
||||
func NewExternalBackend(endpoint string) (*ExternalBackend, error) { |
||||
signer, err := NewExternalSigner(endpoint) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &ExternalBackend{ |
||||
signers: []accounts.Wallet{signer}, |
||||
}, nil |
||||
} |
||||
|
||||
func (eb *ExternalBackend) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { |
||||
return event.NewSubscription(func(quit <-chan struct{}) error { |
||||
<-quit |
||||
return nil |
||||
}) |
||||
} |
||||
|
||||
// ExternalSigner provides an API to interact with an external signer (clef)
|
||||
// It proxies request to the external signer while forwarding relevant
|
||||
// request headers
|
||||
type ExternalSigner struct { |
||||
client *rpc.Client |
||||
endpoint string |
||||
status string |
||||
cacheMu sync.RWMutex |
||||
cache []accounts.Account |
||||
} |
||||
|
||||
func NewExternalSigner(endpoint string) (*ExternalSigner, error) { |
||||
client, err := rpc.Dial(endpoint) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
extsigner := &ExternalSigner{ |
||||
client: client, |
||||
endpoint: endpoint, |
||||
} |
||||
// Check if reachable
|
||||
version, err := extsigner.pingVersion() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
extsigner.status = fmt.Sprintf("ok [version=%v]", version) |
||||
return extsigner, nil |
||||
} |
||||
|
||||
func (api *ExternalSigner) URL() accounts.URL { |
||||
return accounts.URL{ |
||||
Scheme: "extapi", |
||||
Path: api.endpoint, |
||||
} |
||||
} |
||||
|
||||
func (api *ExternalSigner) Status() (string, error) { |
||||
return api.status, nil |
||||
} |
||||
|
||||
func (api *ExternalSigner) Open(passphrase string) error { |
||||
return fmt.Errorf("operation not supported on external signers") |
||||
} |
||||
|
||||
func (api *ExternalSigner) Close() error { |
||||
return fmt.Errorf("operation not supported on external signers") |
||||
} |
||||
|
||||
func (api *ExternalSigner) Accounts() []accounts.Account { |
||||
var accnts []accounts.Account |
||||
res, err := api.listAccounts() |
||||
if err != nil { |
||||
log.Error("account listing failed", "error", err) |
||||
return accnts |
||||
} |
||||
for _, addr := range res { |
||||
accnts = append(accnts, accounts.Account{ |
||||
URL: accounts.URL{ |
||||
Scheme: "extapi", |
||||
Path: api.endpoint, |
||||
}, |
||||
Address: addr, |
||||
}) |
||||
} |
||||
api.cacheMu.Lock() |
||||
api.cache = accnts |
||||
api.cacheMu.Unlock() |
||||
return accnts |
||||
} |
||||
|
||||
func (api *ExternalSigner) Contains(account accounts.Account) bool { |
||||
api.cacheMu.RLock() |
||||
defer api.cacheMu.RUnlock() |
||||
if api.cache == nil { |
||||
// If we haven't already fetched the accounts, it's time to do so now
|
||||
api.cacheMu.RUnlock() |
||||
api.Accounts() |
||||
api.cacheMu.RLock() |
||||
} |
||||
for _, a := range api.cache { |
||||
if a.Address == account.Address && (account.URL == (accounts.URL{}) || account.URL == api.URL()) { |
||||
return true |
||||
} |
||||
} |
||||
return false |
||||
} |
||||
|
||||
func (api *ExternalSigner) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { |
||||
return accounts.Account{}, fmt.Errorf("operation not supported on external signers") |
||||
} |
||||
|
||||
func (api *ExternalSigner) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) { |
||||
log.Error("operation SelfDerive not supported on external signers") |
||||
} |
||||
|
||||
// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed
|
||||
func (api *ExternalSigner) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) { |
||||
var res hexutil.Bytes |
||||
var signAddress = common.NewMixedcaseAddress(account.Address) |
||||
if err := api.client.Call(&res, "account_signData", |
||||
mimeType, |
||||
&signAddress, // Need to use the pointer here, because of how MarshalJSON is defined
|
||||
hexutil.Encode(data)); err != nil { |
||||
return nil, err |
||||
} |
||||
// If V is on 27/28-form, convert to 0/1 for Clique
|
||||
if mimeType == accounts.MimetypeClique && (res[64] == 27 || res[64] == 28) { |
||||
res[64] -= 27 // Transform V from 27/28 to 0/1 for Clique use
|
||||
} |
||||
return res, nil |
||||
} |
||||
|
||||
func (api *ExternalSigner) SignText(account accounts.Account, text []byte) ([]byte, error) { |
||||
var signature hexutil.Bytes |
||||
var signAddress = common.NewMixedcaseAddress(account.Address) |
||||
if err := api.client.Call(&signature, "account_signData", |
||||
accounts.MimetypeTextPlain, |
||||
&signAddress, // Need to use the pointer here, because of how MarshalJSON is defined
|
||||
hexutil.Encode(text)); err != nil { |
||||
return nil, err |
||||
} |
||||
if signature[64] == 27 || signature[64] == 28 { |
||||
// If clef is used as a backend, it may already have transformed
|
||||
// the signature to ethereum-type signature.
|
||||
signature[64] -= 27 // Transform V from Ethereum-legacy to 0/1
|
||||
} |
||||
return signature, nil |
||||
} |
||||
|
||||
// signTransactionResult represents the signinig result returned by clef.
|
||||
type signTransactionResult struct { |
||||
Raw hexutil.Bytes `json:"raw"` |
||||
Tx *types.Transaction `json:"tx"` |
||||
} |
||||
|
||||
// SignTx sends the transaction to the external signer.
|
||||
// If chainID is nil, or tx.ChainID is zero, the chain ID will be assigned
|
||||
// by the external signer. For non-legacy transactions, the chain ID of the
|
||||
// transaction overrides the chainID parameter.
|
||||
func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
||||
data := hexutil.Bytes(tx.Data()) |
||||
var to *common.MixedcaseAddress |
||||
if tx.To() != nil { |
||||
t := common.NewMixedcaseAddress(*tx.To()) |
||||
to = &t |
||||
} |
||||
args := &apitypes.SendTxArgs{ |
||||
Data: &data, |
||||
Nonce: hexutil.Uint64(tx.Nonce()), |
||||
Value: hexutil.Big(*tx.Value()), |
||||
Gas: hexutil.Uint64(tx.Gas()), |
||||
To: to, |
||||
From: common.NewMixedcaseAddress(account.Address), |
||||
} |
||||
switch tx.Type() { |
||||
case types.LegacyTxType, types.AccessListTxType: |
||||
args.GasPrice = (*hexutil.Big)(tx.GasPrice()) |
||||
case types.DynamicFeeTxType: |
||||
args.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap()) |
||||
args.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap()) |
||||
default: |
||||
return nil, fmt.Errorf("unsupported tx type %d", tx.Type()) |
||||
} |
||||
// We should request the default chain id that we're operating with
|
||||
// (the chain we're executing on)
|
||||
if chainID != nil && chainID.Sign() != 0 { |
||||
args.ChainID = (*hexutil.Big)(chainID) |
||||
} |
||||
if tx.Type() != types.LegacyTxType { |
||||
// However, if the user asked for a particular chain id, then we should
|
||||
// use that instead.
|
||||
if tx.ChainId().Sign() != 0 { |
||||
args.ChainID = (*hexutil.Big)(tx.ChainId()) |
||||
} |
||||
accessList := tx.AccessList() |
||||
args.AccessList = &accessList |
||||
} |
||||
var res signTransactionResult |
||||
if err := api.client.Call(&res, "account_signTransaction", args); err != nil { |
||||
return nil, err |
||||
} |
||||
return res.Tx, nil |
||||
} |
||||
|
||||
func (api *ExternalSigner) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) { |
||||
return []byte{}, fmt.Errorf("password-operations not supported on external signers") |
||||
} |
||||
|
||||
func (api *ExternalSigner) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
||||
return nil, fmt.Errorf("password-operations not supported on external signers") |
||||
} |
||||
func (api *ExternalSigner) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) { |
||||
return nil, fmt.Errorf("password-operations not supported on external signers") |
||||
} |
||||
|
||||
func (api *ExternalSigner) listAccounts() ([]common.Address, error) { |
||||
var res []common.Address |
||||
if err := api.client.Call(&res, "account_list"); err != nil { |
||||
return nil, err |
||||
} |
||||
return res, nil |
||||
} |
||||
|
||||
func (api *ExternalSigner) pingVersion() (string, error) { |
||||
var v string |
||||
if err := api.client.Call(&v, "account_version"); err != nil { |
||||
return "", err |
||||
} |
||||
return v, nil |
||||
} |
@ -1,180 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package accounts |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
"math" |
||||
"math/big" |
||||
"strings" |
||||
) |
||||
|
||||
// DefaultRootDerivationPath is the root path to which custom derivation endpoints
|
||||
// are appended. As such, the first account will be at m/44'/60'/0'/0, the second
|
||||
// at m/44'/60'/0'/1, etc.
|
||||
var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0} |
||||
|
||||
// DefaultBaseDerivationPath is the base path from which custom derivation endpoints
|
||||
// are incremented. As such, the first account will be at m/44'/60'/0'/0/0, the second
|
||||
// at m/44'/60'/0'/0/1, etc.
|
||||
var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0} |
||||
|
||||
// LegacyLedgerBaseDerivationPath is the legacy base path from which custom derivation
|
||||
// endpoints are incremented. As such, the first account will be at m/44'/60'/0'/0, the
|
||||
// second at m/44'/60'/0'/1, etc.
|
||||
var LegacyLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0} |
||||
|
||||
// DerivationPath represents the computer friendly version of a hierarchical
|
||||
// deterministic wallet account derivation path.
|
||||
//
|
||||
// The BIP-32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
|
||||
// defines derivation paths to be of the form:
|
||||
//
|
||||
// m / purpose' / coin_type' / account' / change / address_index
|
||||
//
|
||||
// The BIP-44 spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
||||
// defines that the `purpose` be 44' (or 0x8000002C) for crypto currencies, and
|
||||
// SLIP-44 https://github.com/satoshilabs/slips/blob/master/slip-0044.md assigns
|
||||
// the `coin_type` 60' (or 0x8000003C) to Ethereum.
|
||||
//
|
||||
// The root path for Ethereum is m/44'/60'/0'/0 according to the specification
|
||||
// from https://github.com/ethereum/EIPs/issues/84, albeit it's not set in stone
|
||||
// yet whether accounts should increment the last component or the children of
|
||||
// that. We will go with the simpler approach of incrementing the last component.
|
||||
type DerivationPath []uint32 |
||||
|
||||
// ParseDerivationPath converts a user specified derivation path string to the
|
||||
// internal binary representation.
|
||||
//
|
||||
// Full derivation paths need to start with the `m/` prefix, relative derivation
|
||||
// paths (which will get appended to the default root path) must not have prefixes
|
||||
// in front of the first element. Whitespace is ignored.
|
||||
func ParseDerivationPath(path string) (DerivationPath, error) { |
||||
var result DerivationPath |
||||
|
||||
// Handle absolute or relative paths
|
||||
components := strings.Split(path, "/") |
||||
switch { |
||||
case len(components) == 0: |
||||
return nil, errors.New("empty derivation path") |
||||
|
||||
case strings.TrimSpace(components[0]) == "": |
||||
return nil, errors.New("ambiguous path: use 'm/' prefix for absolute paths, or no leading '/' for relative ones") |
||||
|
||||
case strings.TrimSpace(components[0]) == "m": |
||||
components = components[1:] |
||||
|
||||
default: |
||||
result = append(result, DefaultRootDerivationPath...) |
||||
} |
||||
// All remaining components are relative, append one by one
|
||||
if len(components) == 0 { |
||||
return nil, errors.New("empty derivation path") // Empty relative paths
|
||||
} |
||||
for _, component := range components { |
||||
// Ignore any user added whitespace
|
||||
component = strings.TrimSpace(component) |
||||
var value uint32 |
||||
|
||||
// Handle hardened paths
|
||||
if strings.HasSuffix(component, "'") { |
||||
value = 0x80000000 |
||||
component = strings.TrimSpace(strings.TrimSuffix(component, "'")) |
||||
} |
||||
// Handle the non hardened component
|
||||
bigval, ok := new(big.Int).SetString(component, 0) |
||||
if !ok { |
||||
return nil, fmt.Errorf("invalid component: %s", component) |
||||
} |
||||
max := math.MaxUint32 - value |
||||
if bigval.Sign() < 0 || bigval.Cmp(big.NewInt(int64(max))) > 0 { |
||||
if value == 0 { |
||||
return nil, fmt.Errorf("component %v out of allowed range [0, %d]", bigval, max) |
||||
} |
||||
return nil, fmt.Errorf("component %v out of allowed hardened range [0, %d]", bigval, max) |
||||
} |
||||
value += uint32(bigval.Uint64()) |
||||
|
||||
// Append and repeat
|
||||
result = append(result, value) |
||||
} |
||||
return result, nil |
||||
} |
||||
|
||||
// String implements the stringer interface, converting a binary derivation path
|
||||
// to its canonical representation.
|
||||
func (path DerivationPath) String() string { |
||||
result := "m" |
||||
for _, component := range path { |
||||
var hardened bool |
||||
if component >= 0x80000000 { |
||||
component -= 0x80000000 |
||||
hardened = true |
||||
} |
||||
result = fmt.Sprintf("%s/%d", result, component) |
||||
if hardened { |
||||
result += "'" |
||||
} |
||||
} |
||||
return result |
||||
} |
||||
|
||||
// MarshalJSON turns a derivation path into its json-serialized string
|
||||
func (path DerivationPath) MarshalJSON() ([]byte, error) { |
||||
return json.Marshal(path.String()) |
||||
} |
||||
|
||||
// UnmarshalJSON a json-serialized string back into a derivation path
|
||||
func (path *DerivationPath) UnmarshalJSON(b []byte) error { |
||||
var dp string |
||||
var err error |
||||
if err = json.Unmarshal(b, &dp); err != nil { |
||||
return err |
||||
} |
||||
*path, err = ParseDerivationPath(dp) |
||||
return err |
||||
} |
||||
|
||||
// DefaultIterator creates a BIP-32 path iterator, which progresses by increasing the last component:
|
||||
// i.e. m/44'/60'/0'/0/0, m/44'/60'/0'/0/1, m/44'/60'/0'/0/2, ... m/44'/60'/0'/0/N.
|
||||
func DefaultIterator(base DerivationPath) func() DerivationPath { |
||||
path := make(DerivationPath, len(base)) |
||||
copy(path[:], base[:]) |
||||
// Set it back by one, so the first call gives the first result
|
||||
path[len(path)-1]-- |
||||
return func() DerivationPath { |
||||
path[len(path)-1]++ |
||||
return path |
||||
} |
||||
} |
||||
|
||||
// LedgerLiveIterator creates a bip44 path iterator for Ledger Live.
|
||||
// Ledger Live increments the third component rather than the fifth component
|
||||
// i.e. m/44'/60'/0'/0/0, m/44'/60'/1'/0/0, m/44'/60'/2'/0/0, ... m/44'/60'/N'/0/0.
|
||||
func LedgerLiveIterator(base DerivationPath) func() DerivationPath { |
||||
path := make(DerivationPath, len(base)) |
||||
copy(path[:], base[:]) |
||||
// Set it back by one, so the first call gives the first result
|
||||
path[2]-- |
||||
return func() DerivationPath { |
||||
// ledgerLivePathIterator iterates on the third component
|
||||
path[2]++ |
||||
return path |
||||
} |
||||
} |
@ -1,118 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package accounts |
||||
|
||||
import ( |
||||
"fmt" |
||||
"reflect" |
||||
"testing" |
||||
) |
||||
|
||||
// Tests that HD derivation paths can be correctly parsed into our internal binary
|
||||
// representation.
|
||||
func TestHDPathParsing(t *testing.T) { |
||||
tests := []struct { |
||||
input string |
||||
output DerivationPath |
||||
}{ |
||||
// Plain absolute derivation paths
|
||||
{"m/44'/60'/0'/0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}}, |
||||
{"m/44'/60'/0'/128", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 128}}, |
||||
{"m/44'/60'/0'/0'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}}, |
||||
{"m/44'/60'/0'/128'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 128}}, |
||||
{"m/2147483692/2147483708/2147483648/0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}}, |
||||
{"m/2147483692/2147483708/2147483648/2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}}, |
||||
|
||||
// Plain relative derivation paths
|
||||
{"0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}}, |
||||
{"128", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 128}}, |
||||
{"0'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}}, |
||||
{"128'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 128}}, |
||||
{"2147483648", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}}, |
||||
|
||||
// Hexadecimal absolute derivation paths
|
||||
{"m/0x2C'/0x3c'/0x00'/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}}, |
||||
{"m/0x2C'/0x3c'/0x00'/0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 128}}, |
||||
{"m/0x2C'/0x3c'/0x00'/0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}}, |
||||
{"m/0x2C'/0x3c'/0x00'/0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 128}}, |
||||
{"m/0x8000002C/0x8000003c/0x80000000/0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}}, |
||||
{"m/0x8000002C/0x8000003c/0x80000000/0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0x80000000 + 0}}, |
||||
|
||||
// Hexadecimal relative derivation paths
|
||||
{"0x00", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}}, |
||||
{"0x80", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 128}}, |
||||
{"0x00'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}}, |
||||
{"0x80'", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 128}}, |
||||
{"0x80000000", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0x80000000 + 0}}, |
||||
|
||||
// Weird inputs just to ensure they work
|
||||
{" m / 44 '\n/\n 60 \n\n\t' /\n0 ' /\t\t 0", DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}}, |
||||
|
||||
// Invalid derivation paths
|
||||
{"", nil}, // Empty relative derivation path
|
||||
{"m", nil}, // Empty absolute derivation path
|
||||
{"m/", nil}, // Missing last derivation component
|
||||
{"/44'/60'/0'/0", nil}, // Absolute path without m prefix, might be user error
|
||||
{"m/2147483648'", nil}, // Overflows 32 bit integer
|
||||
{"m/-1'", nil}, // Cannot contain negative number
|
||||
} |
||||
for i, tt := range tests { |
||||
if path, err := ParseDerivationPath(tt.input); !reflect.DeepEqual(path, tt.output) { |
||||
t.Errorf("test %d: parse mismatch: have %v (%v), want %v", i, path, err, tt.output) |
||||
} else if path == nil && err == nil { |
||||
t.Errorf("test %d: nil path and error: %v", i, err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func testDerive(t *testing.T, next func() DerivationPath, expected []string) { |
||||
t.Helper() |
||||
for i, want := range expected { |
||||
if have := next(); fmt.Sprintf("%v", have) != want { |
||||
t.Errorf("step %d, have %v, want %v", i, have, want) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func TestHdPathIteration(t *testing.T) { |
||||
testDerive(t, DefaultIterator(DefaultBaseDerivationPath), |
||||
[]string{ |
||||
"m/44'/60'/0'/0/0", "m/44'/60'/0'/0/1", |
||||
"m/44'/60'/0'/0/2", "m/44'/60'/0'/0/3", |
||||
"m/44'/60'/0'/0/4", "m/44'/60'/0'/0/5", |
||||
"m/44'/60'/0'/0/6", "m/44'/60'/0'/0/7", |
||||
"m/44'/60'/0'/0/8", "m/44'/60'/0'/0/9", |
||||
}) |
||||
|
||||
testDerive(t, DefaultIterator(LegacyLedgerBaseDerivationPath), |
||||
[]string{ |
||||
"m/44'/60'/0'/0", "m/44'/60'/0'/1", |
||||
"m/44'/60'/0'/2", "m/44'/60'/0'/3", |
||||
"m/44'/60'/0'/4", "m/44'/60'/0'/5", |
||||
"m/44'/60'/0'/6", "m/44'/60'/0'/7", |
||||
"m/44'/60'/0'/8", "m/44'/60'/0'/9", |
||||
}) |
||||
|
||||
testDerive(t, LedgerLiveIterator(DefaultBaseDerivationPath), |
||||
[]string{ |
||||
"m/44'/60'/0'/0/0", "m/44'/60'/1'/0/0", |
||||
"m/44'/60'/2'/0/0", "m/44'/60'/3'/0/0", |
||||
"m/44'/60'/4'/0/0", "m/44'/60'/5'/0/0", |
||||
"m/44'/60'/6'/0/0", "m/44'/60'/7'/0/0", |
||||
"m/44'/60'/8'/0/0", "m/44'/60'/9'/0/0", |
||||
}) |
||||
} |
@ -1,308 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"bufio" |
||||
"encoding/json" |
||||
"fmt" |
||||
"os" |
||||
"path/filepath" |
||||
"sort" |
||||
"strings" |
||||
"sync" |
||||
"time" |
||||
|
||||
mapset "github.com/deckarep/golang-set/v2" |
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
) |
||||
|
||||
// Minimum amount of time between cache reloads. This limit applies if the platform does
|
||||
// not support change notifications. It also applies if the keystore directory does not
|
||||
// exist yet, the code will attempt to create a watcher at most this often.
|
||||
const minReloadInterval = 2 * time.Second |
||||
|
||||
type accountsByURL []accounts.Account |
||||
|
||||
func (s accountsByURL) Len() int { return len(s) } |
||||
func (s accountsByURL) Less(i, j int) bool { return s[i].URL.Cmp(s[j].URL) < 0 } |
||||
func (s accountsByURL) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
||||
|
||||
// AmbiguousAddrError is returned when attempting to unlock
|
||||
// an address for which more than one file exists.
|
||||
type AmbiguousAddrError struct { |
||||
Addr common.Address |
||||
Matches []accounts.Account |
||||
} |
||||
|
||||
func (err *AmbiguousAddrError) Error() string { |
||||
files := "" |
||||
for i, a := range err.Matches { |
||||
files += a.URL.Path |
||||
if i < len(err.Matches)-1 { |
||||
files += ", " |
||||
} |
||||
} |
||||
return fmt.Sprintf("multiple keys match address (%s)", files) |
||||
} |
||||
|
||||
// accountCache is a live index of all accounts in the keystore.
|
||||
type accountCache struct { |
||||
keydir string |
||||
watcher *watcher |
||||
mu sync.Mutex |
||||
all accountsByURL |
||||
byAddr map[common.Address][]accounts.Account |
||||
throttle *time.Timer |
||||
notify chan struct{} |
||||
fileC fileCache |
||||
} |
||||
|
||||
func newAccountCache(keydir string) (*accountCache, chan struct{}) { |
||||
ac := &accountCache{ |
||||
keydir: keydir, |
||||
byAddr: make(map[common.Address][]accounts.Account), |
||||
notify: make(chan struct{}, 1), |
||||
fileC: fileCache{all: mapset.NewThreadUnsafeSet[string]()}, |
||||
} |
||||
ac.watcher = newWatcher(ac) |
||||
return ac, ac.notify |
||||
} |
||||
|
||||
func (ac *accountCache) accounts() []accounts.Account { |
||||
ac.maybeReload() |
||||
ac.mu.Lock() |
||||
defer ac.mu.Unlock() |
||||
cpy := make([]accounts.Account, len(ac.all)) |
||||
copy(cpy, ac.all) |
||||
return cpy |
||||
} |
||||
|
||||
func (ac *accountCache) hasAddress(addr common.Address) bool { |
||||
ac.maybeReload() |
||||
ac.mu.Lock() |
||||
defer ac.mu.Unlock() |
||||
return len(ac.byAddr[addr]) > 0 |
||||
} |
||||
|
||||
func (ac *accountCache) add(newAccount accounts.Account) { |
||||
ac.mu.Lock() |
||||
defer ac.mu.Unlock() |
||||
|
||||
i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Cmp(newAccount.URL) >= 0 }) |
||||
if i < len(ac.all) && ac.all[i] == newAccount { |
||||
return |
||||
} |
||||
// newAccount is not in the cache.
|
||||
ac.all = append(ac.all, accounts.Account{}) |
||||
copy(ac.all[i+1:], ac.all[i:]) |
||||
ac.all[i] = newAccount |
||||
ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount) |
||||
} |
||||
|
||||
// note: removed needs to be unique here (i.e. both File and Address must be set).
|
||||
func (ac *accountCache) delete(removed accounts.Account) { |
||||
ac.mu.Lock() |
||||
defer ac.mu.Unlock() |
||||
|
||||
ac.all = removeAccount(ac.all, removed) |
||||
if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 { |
||||
delete(ac.byAddr, removed.Address) |
||||
} else { |
||||
ac.byAddr[removed.Address] = ba |
||||
} |
||||
} |
||||
|
||||
// deleteByFile removes an account referenced by the given path.
|
||||
func (ac *accountCache) deleteByFile(path string) { |
||||
ac.mu.Lock() |
||||
defer ac.mu.Unlock() |
||||
i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Path >= path }) |
||||
|
||||
if i < len(ac.all) && ac.all[i].URL.Path == path { |
||||
removed := ac.all[i] |
||||
ac.all = append(ac.all[:i], ac.all[i+1:]...) |
||||
if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 { |
||||
delete(ac.byAddr, removed.Address) |
||||
} else { |
||||
ac.byAddr[removed.Address] = ba |
||||
} |
||||
} |
||||
} |
||||
|
||||
// watcherStarted returns true if the watcher loop started running (even if it
|
||||
// has since also ended).
|
||||
func (ac *accountCache) watcherStarted() bool { |
||||
ac.mu.Lock() |
||||
defer ac.mu.Unlock() |
||||
return ac.watcher.running || ac.watcher.runEnded |
||||
} |
||||
|
||||
func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account { |
||||
for i := range slice { |
||||
if slice[i] == elem { |
||||
return append(slice[:i], slice[i+1:]...) |
||||
} |
||||
} |
||||
return slice |
||||
} |
||||
|
||||
// find returns the cached account for address if there is a unique match.
|
||||
// The exact matching rules are explained by the documentation of accounts.Account.
|
||||
// Callers must hold ac.mu.
|
||||
func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) { |
||||
// Limit search to address candidates if possible.
|
||||
matches := ac.all |
||||
if (a.Address != common.Address{}) { |
||||
matches = ac.byAddr[a.Address] |
||||
} |
||||
if a.URL.Path != "" { |
||||
// If only the basename is specified, complete the path.
|
||||
if !strings.ContainsRune(a.URL.Path, filepath.Separator) { |
||||
a.URL.Path = filepath.Join(ac.keydir, a.URL.Path) |
||||
} |
||||
for i := range matches { |
||||
if matches[i].URL == a.URL { |
||||
return matches[i], nil |
||||
} |
||||
} |
||||
if (a.Address == common.Address{}) { |
||||
return accounts.Account{}, ErrNoMatch |
||||
} |
||||
} |
||||
switch len(matches) { |
||||
case 1: |
||||
return matches[0], nil |
||||
case 0: |
||||
return accounts.Account{}, ErrNoMatch |
||||
default: |
||||
err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))} |
||||
copy(err.Matches, matches) |
||||
sort.Sort(accountsByURL(err.Matches)) |
||||
return accounts.Account{}, err |
||||
} |
||||
} |
||||
|
||||
func (ac *accountCache) maybeReload() { |
||||
ac.mu.Lock() |
||||
|
||||
if ac.watcher.running { |
||||
ac.mu.Unlock() |
||||
return // A watcher is running and will keep the cache up-to-date.
|
||||
} |
||||
if ac.throttle == nil { |
||||
ac.throttle = time.NewTimer(0) |
||||
} else { |
||||
select { |
||||
case <-ac.throttle.C: |
||||
default: |
||||
ac.mu.Unlock() |
||||
return // The cache was reloaded recently.
|
||||
} |
||||
} |
||||
// No watcher running, start it.
|
||||
ac.watcher.start() |
||||
ac.throttle.Reset(minReloadInterval) |
||||
ac.mu.Unlock() |
||||
ac.scanAccounts() |
||||
} |
||||
|
||||
func (ac *accountCache) close() { |
||||
ac.mu.Lock() |
||||
ac.watcher.close() |
||||
if ac.throttle != nil { |
||||
ac.throttle.Stop() |
||||
} |
||||
if ac.notify != nil { |
||||
close(ac.notify) |
||||
ac.notify = nil |
||||
} |
||||
ac.mu.Unlock() |
||||
} |
||||
|
||||
// scanAccounts checks if any changes have occurred on the filesystem, and
|
||||
// updates the account cache accordingly
|
||||
func (ac *accountCache) scanAccounts() error { |
||||
// Scan the entire folder metadata for file changes
|
||||
creates, deletes, updates, err := ac.fileC.scan(ac.keydir) |
||||
if err != nil { |
||||
log.Debug("Failed to reload keystore contents", "err", err) |
||||
return err |
||||
} |
||||
if creates.Cardinality() == 0 && deletes.Cardinality() == 0 && updates.Cardinality() == 0 { |
||||
return nil |
||||
} |
||||
// Create a helper method to scan the contents of the key files
|
||||
var ( |
||||
buf = new(bufio.Reader) |
||||
key struct { |
||||
Address string `json:"address"` |
||||
} |
||||
) |
||||
readAccount := func(path string) *accounts.Account { |
||||
fd, err := os.Open(path) |
||||
if err != nil { |
||||
log.Trace("Failed to open keystore file", "path", path, "err", err) |
||||
return nil |
||||
} |
||||
defer fd.Close() |
||||
buf.Reset(fd) |
||||
// Parse the address.
|
||||
key.Address = "" |
||||
err = json.NewDecoder(buf).Decode(&key) |
||||
addr := common.HexToAddress(key.Address) |
||||
switch { |
||||
case err != nil: |
||||
log.Debug("Failed to decode keystore key", "path", path, "err", err) |
||||
case addr == common.Address{}: |
||||
log.Debug("Failed to decode keystore key", "path", path, "err", "missing or zero address") |
||||
default: |
||||
return &accounts.Account{ |
||||
Address: addr, |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: path}, |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
// Process all the file diffs
|
||||
start := time.Now() |
||||
|
||||
for _, path := range creates.ToSlice() { |
||||
if a := readAccount(path); a != nil { |
||||
ac.add(*a) |
||||
} |
||||
} |
||||
for _, path := range deletes.ToSlice() { |
||||
ac.deleteByFile(path) |
||||
} |
||||
for _, path := range updates.ToSlice() { |
||||
ac.deleteByFile(path) |
||||
if a := readAccount(path); a != nil { |
||||
ac.add(*a) |
||||
} |
||||
} |
||||
end := time.Now() |
||||
|
||||
select { |
||||
case ac.notify <- struct{}{}: |
||||
default: |
||||
} |
||||
log.Trace("Handled keystore changes", "time", end.Sub(start)) |
||||
return nil |
||||
} |
@ -1,407 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"fmt" |
||||
"math/rand" |
||||
"os" |
||||
"path/filepath" |
||||
"reflect" |
||||
"sort" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/cespare/cp" |
||||
"github.com/davecgh/go-spew/spew" |
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
var ( |
||||
cachetestDir, _ = filepath.Abs(filepath.Join("testdata", "keystore")) |
||||
cachetestAccounts = []accounts.Account{ |
||||
{ |
||||
Address: common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(cachetestDir, "UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8")}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("f466859ead1932d743d622cb74fc058882e8648a"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(cachetestDir, "aaa")}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("289d485d9771714cce91d3393d764e1311907acc"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(cachetestDir, "zzz")}, |
||||
}, |
||||
} |
||||
) |
||||
|
||||
// waitWatcherStarts waits up to 1s for the keystore watcher to start.
|
||||
func waitWatcherStart(ks *KeyStore) bool { |
||||
// On systems where file watch is not supported, just return "ok".
|
||||
if !ks.cache.watcher.enabled() { |
||||
return true |
||||
} |
||||
// The watcher should start, and then exit.
|
||||
for t0 := time.Now(); time.Since(t0) < 1*time.Second; time.Sleep(100 * time.Millisecond) { |
||||
if ks.cache.watcherStarted() { |
||||
return true |
||||
} |
||||
} |
||||
return false |
||||
} |
||||
|
||||
func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error { |
||||
var list []accounts.Account |
||||
for t0 := time.Now(); time.Since(t0) < 5*time.Second; time.Sleep(200 * time.Millisecond) { |
||||
list = ks.Accounts() |
||||
if reflect.DeepEqual(list, wantAccounts) { |
||||
// ks should have also received change notifications
|
||||
select { |
||||
case <-ks.changes: |
||||
default: |
||||
return fmt.Errorf("wasn't notified of new accounts") |
||||
} |
||||
return nil |
||||
} |
||||
} |
||||
return fmt.Errorf("\ngot %v\nwant %v", list, wantAccounts) |
||||
} |
||||
|
||||
func TestWatchNewFile(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
dir, ks := tmpKeyStore(t, false) |
||||
|
||||
// Ensure the watcher is started before adding any files.
|
||||
ks.Accounts() |
||||
if !waitWatcherStart(ks) { |
||||
t.Fatal("keystore watcher didn't start in time") |
||||
} |
||||
// Move in the files.
|
||||
wantAccounts := make([]accounts.Account, len(cachetestAccounts)) |
||||
for i := range cachetestAccounts { |
||||
wantAccounts[i] = accounts.Account{ |
||||
Address: cachetestAccounts[i].Address, |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(dir, filepath.Base(cachetestAccounts[i].URL.Path))}, |
||||
} |
||||
if err := cp.CopyFile(wantAccounts[i].URL.Path, cachetestAccounts[i].URL.Path); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
// ks should see the accounts.
|
||||
if err := waitForAccounts(wantAccounts, ks); err != nil { |
||||
t.Error(err) |
||||
} |
||||
} |
||||
|
||||
func TestWatchNoDir(t *testing.T) { |
||||
t.Parallel() |
||||
// Create ks but not the directory that it watches.
|
||||
rand.Seed(time.Now().UnixNano()) |
||||
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-watchnodir-test-%d-%d", os.Getpid(), rand.Int())) |
||||
ks := NewKeyStore(dir, LightScryptN, LightScryptP) |
||||
list := ks.Accounts() |
||||
if len(list) > 0 { |
||||
t.Error("initial account list not empty:", list) |
||||
} |
||||
// The watcher should start, and then exit.
|
||||
if !waitWatcherStart(ks) { |
||||
t.Fatal("keystore watcher didn't start in time") |
||||
} |
||||
// Create the directory and copy a key file into it.
|
||||
os.MkdirAll(dir, 0700) |
||||
defer os.RemoveAll(dir) |
||||
file := filepath.Join(dir, "aaa") |
||||
if err := cp.CopyFile(file, cachetestAccounts[0].URL.Path); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// ks should see the account.
|
||||
wantAccounts := []accounts.Account{cachetestAccounts[0]} |
||||
wantAccounts[0].URL = accounts.URL{Scheme: KeyStoreScheme, Path: file} |
||||
for d := 200 * time.Millisecond; d < 8*time.Second; d *= 2 { |
||||
list = ks.Accounts() |
||||
if reflect.DeepEqual(list, wantAccounts) { |
||||
// ks should have also received change notifications
|
||||
select { |
||||
case <-ks.changes: |
||||
default: |
||||
t.Fatalf("wasn't notified of new accounts") |
||||
} |
||||
return |
||||
} |
||||
time.Sleep(d) |
||||
} |
||||
t.Errorf("\ngot %v\nwant %v", list, wantAccounts) |
||||
} |
||||
|
||||
func TestCacheInitialReload(t *testing.T) { |
||||
cache, _ := newAccountCache(cachetestDir) |
||||
accounts := cache.accounts() |
||||
if !reflect.DeepEqual(accounts, cachetestAccounts) { |
||||
t.Fatalf("got initial accounts: %swant %s", spew.Sdump(accounts), spew.Sdump(cachetestAccounts)) |
||||
} |
||||
} |
||||
|
||||
func TestCacheAddDeleteOrder(t *testing.T) { |
||||
cache, _ := newAccountCache("testdata/no-such-dir") |
||||
cache.watcher.running = true // prevent unexpected reloads
|
||||
|
||||
accs := []accounts.Account{ |
||||
{ |
||||
Address: common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "-309830980"}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("2cac1adea150210703ba75ed097ddfe24e14f213"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "ggg"}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("8bda78331c916a08481428e4b07c96d3e916d165"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "zzzzzz-the-very-last-one.keyXXX"}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("d49ff4eeb0b2686ed89c0fc0f2b6ea533ddbbd5e"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "SOMETHING.key"}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("7ef5a6135f1fd6a02593eedc869c6d41d934aef8"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8"}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("f466859ead1932d743d622cb74fc058882e8648a"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "aaa"}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("289d485d9771714cce91d3393d764e1311907acc"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: "zzz"}, |
||||
}, |
||||
} |
||||
for _, a := range accs { |
||||
cache.add(a) |
||||
} |
||||
// Add some of them twice to check that they don't get reinserted.
|
||||
cache.add(accs[0]) |
||||
cache.add(accs[2]) |
||||
|
||||
// Check that the account list is sorted by filename.
|
||||
wantAccounts := make([]accounts.Account, len(accs)) |
||||
copy(wantAccounts, accs) |
||||
sort.Sort(accountsByURL(wantAccounts)) |
||||
list := cache.accounts() |
||||
if !reflect.DeepEqual(list, wantAccounts) { |
||||
t.Fatalf("got accounts: %s\nwant %s", spew.Sdump(accs), spew.Sdump(wantAccounts)) |
||||
} |
||||
for _, a := range accs { |
||||
if !cache.hasAddress(a.Address) { |
||||
t.Errorf("expected hasAccount(%x) to return true", a.Address) |
||||
} |
||||
} |
||||
if cache.hasAddress(common.HexToAddress("fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e")) { |
||||
t.Errorf("expected hasAccount(%x) to return false", common.HexToAddress("fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e")) |
||||
} |
||||
|
||||
// Delete a few keys from the cache.
|
||||
for i := 0; i < len(accs); i += 2 { |
||||
cache.delete(wantAccounts[i]) |
||||
} |
||||
cache.delete(accounts.Account{Address: common.HexToAddress("fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e"), URL: accounts.URL{Scheme: KeyStoreScheme, Path: "something"}}) |
||||
|
||||
// Check content again after deletion.
|
||||
wantAccountsAfterDelete := []accounts.Account{ |
||||
wantAccounts[1], |
||||
wantAccounts[3], |
||||
wantAccounts[5], |
||||
} |
||||
list = cache.accounts() |
||||
if !reflect.DeepEqual(list, wantAccountsAfterDelete) { |
||||
t.Fatalf("got accounts after delete: %s\nwant %s", spew.Sdump(list), spew.Sdump(wantAccountsAfterDelete)) |
||||
} |
||||
for _, a := range wantAccountsAfterDelete { |
||||
if !cache.hasAddress(a.Address) { |
||||
t.Errorf("expected hasAccount(%x) to return true", a.Address) |
||||
} |
||||
} |
||||
if cache.hasAddress(wantAccounts[0].Address) { |
||||
t.Errorf("expected hasAccount(%x) to return false", wantAccounts[0].Address) |
||||
} |
||||
} |
||||
|
||||
func TestCacheFind(t *testing.T) { |
||||
dir := filepath.Join("testdata", "dir") |
||||
cache, _ := newAccountCache(dir) |
||||
cache.watcher.running = true // prevent unexpected reloads
|
||||
|
||||
accs := []accounts.Account{ |
||||
{ |
||||
Address: common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(dir, "a.key")}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("2cac1adea150210703ba75ed097ddfe24e14f213"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(dir, "b.key")}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("d49ff4eeb0b2686ed89c0fc0f2b6ea533ddbbd5e"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(dir, "c.key")}, |
||||
}, |
||||
{ |
||||
Address: common.HexToAddress("d49ff4eeb0b2686ed89c0fc0f2b6ea533ddbbd5e"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(dir, "c2.key")}, |
||||
}, |
||||
} |
||||
for _, a := range accs { |
||||
cache.add(a) |
||||
} |
||||
|
||||
nomatchAccount := accounts.Account{ |
||||
Address: common.HexToAddress("f466859ead1932d743d622cb74fc058882e8648a"), |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Join(dir, "something")}, |
||||
} |
||||
tests := []struct { |
||||
Query accounts.Account |
||||
WantResult accounts.Account |
||||
WantError error |
||||
}{ |
||||
// by address
|
||||
{Query: accounts.Account{Address: accs[0].Address}, WantResult: accs[0]}, |
||||
// by file
|
||||
{Query: accounts.Account{URL: accs[0].URL}, WantResult: accs[0]}, |
||||
// by basename
|
||||
{Query: accounts.Account{URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Base(accs[0].URL.Path)}}, WantResult: accs[0]}, |
||||
// by file and address
|
||||
{Query: accs[0], WantResult: accs[0]}, |
||||
// ambiguous address, tie resolved by file
|
||||
{Query: accs[2], WantResult: accs[2]}, |
||||
// ambiguous address error
|
||||
{ |
||||
Query: accounts.Account{Address: accs[2].Address}, |
||||
WantError: &AmbiguousAddrError{ |
||||
Addr: accs[2].Address, |
||||
Matches: []accounts.Account{accs[2], accs[3]}, |
||||
}, |
||||
}, |
||||
// no match error
|
||||
{Query: nomatchAccount, WantError: ErrNoMatch}, |
||||
{Query: accounts.Account{URL: nomatchAccount.URL}, WantError: ErrNoMatch}, |
||||
{Query: accounts.Account{URL: accounts.URL{Scheme: KeyStoreScheme, Path: filepath.Base(nomatchAccount.URL.Path)}}, WantError: ErrNoMatch}, |
||||
{Query: accounts.Account{Address: nomatchAccount.Address}, WantError: ErrNoMatch}, |
||||
} |
||||
for i, test := range tests { |
||||
a, err := cache.find(test.Query) |
||||
if !reflect.DeepEqual(err, test.WantError) { |
||||
t.Errorf("test %d: error mismatch for query %v\ngot %q\nwant %q", i, test.Query, err, test.WantError) |
||||
continue |
||||
} |
||||
if a != test.WantResult { |
||||
t.Errorf("test %d: result mismatch for query %v\ngot %v\nwant %v", i, test.Query, a, test.WantResult) |
||||
continue |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestUpdatedKeyfileContents tests that updating the contents of a keystore file
|
||||
// is noticed by the watcher, and the account cache is updated accordingly
|
||||
func TestUpdatedKeyfileContents(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
// Create a temporary keystore to test with
|
||||
rand.Seed(time.Now().UnixNano()) |
||||
dir := filepath.Join(os.TempDir(), fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int())) |
||||
ks := NewKeyStore(dir, LightScryptN, LightScryptP) |
||||
|
||||
list := ks.Accounts() |
||||
if len(list) > 0 { |
||||
t.Error("initial account list not empty:", list) |
||||
} |
||||
if !waitWatcherStart(ks) { |
||||
t.Fatal("keystore watcher didn't start in time") |
||||
} |
||||
// Create the directory and copy a key file into it.
|
||||
os.MkdirAll(dir, 0700) |
||||
defer os.RemoveAll(dir) |
||||
file := filepath.Join(dir, "aaa") |
||||
|
||||
// Place one of our testfiles in there
|
||||
if err := cp.CopyFile(file, cachetestAccounts[0].URL.Path); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// ks should see the account.
|
||||
wantAccounts := []accounts.Account{cachetestAccounts[0]} |
||||
wantAccounts[0].URL = accounts.URL{Scheme: KeyStoreScheme, Path: file} |
||||
if err := waitForAccounts(wantAccounts, ks); err != nil { |
||||
t.Error(err) |
||||
return |
||||
} |
||||
// needed so that modTime of `file` is different to its current value after forceCopyFile
|
||||
time.Sleep(time.Second) |
||||
|
||||
// Now replace file contents
|
||||
if err := forceCopyFile(file, cachetestAccounts[1].URL.Path); err != nil { |
||||
t.Fatal(err) |
||||
return |
||||
} |
||||
wantAccounts = []accounts.Account{cachetestAccounts[1]} |
||||
wantAccounts[0].URL = accounts.URL{Scheme: KeyStoreScheme, Path: file} |
||||
if err := waitForAccounts(wantAccounts, ks); err != nil { |
||||
t.Errorf("First replacement failed") |
||||
t.Error(err) |
||||
return |
||||
} |
||||
|
||||
// needed so that modTime of `file` is different to its current value after forceCopyFile
|
||||
time.Sleep(time.Second) |
||||
|
||||
// Now replace file contents again
|
||||
if err := forceCopyFile(file, cachetestAccounts[2].URL.Path); err != nil { |
||||
t.Fatal(err) |
||||
return |
||||
} |
||||
wantAccounts = []accounts.Account{cachetestAccounts[2]} |
||||
wantAccounts[0].URL = accounts.URL{Scheme: KeyStoreScheme, Path: file} |
||||
if err := waitForAccounts(wantAccounts, ks); err != nil { |
||||
t.Errorf("Second replacement failed") |
||||
t.Error(err) |
||||
return |
||||
} |
||||
|
||||
// needed so that modTime of `file` is different to its current value after os.WriteFile
|
||||
time.Sleep(time.Second) |
||||
|
||||
// Now replace file contents with crap
|
||||
if err := os.WriteFile(file, []byte("foo"), 0600); err != nil { |
||||
t.Fatal(err) |
||||
return |
||||
} |
||||
if err := waitForAccounts([]accounts.Account{}, ks); err != nil { |
||||
t.Errorf("Emptying account file failed") |
||||
t.Error(err) |
||||
return |
||||
} |
||||
} |
||||
|
||||
// forceCopyFile is like cp.CopyFile, but doesn't complain if the destination exists.
|
||||
func forceCopyFile(dst, src string) error { |
||||
data, err := os.ReadFile(src) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return os.WriteFile(dst, data, 0644) |
||||
} |
@ -1,105 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"os" |
||||
"path/filepath" |
||||
"strings" |
||||
"sync" |
||||
"time" |
||||
|
||||
mapset "github.com/deckarep/golang-set/v2" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
) |
||||
|
||||
// fileCache is a cache of files seen during scan of keystore.
|
||||
type fileCache struct { |
||||
all mapset.Set[string] // Set of all files from the keystore folder
|
||||
lastMod time.Time // Last time instance when a file was modified
|
||||
mu sync.Mutex |
||||
} |
||||
|
||||
// scan performs a new scan on the given directory, compares against the already
|
||||
// cached filenames, and returns file sets: creates, deletes, updates.
|
||||
func (fc *fileCache) scan(keyDir string) (mapset.Set[string], mapset.Set[string], mapset.Set[string], error) { |
||||
t0 := time.Now() |
||||
|
||||
// List all the files from the keystore folder
|
||||
files, err := os.ReadDir(keyDir) |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
t1 := time.Now() |
||||
|
||||
fc.mu.Lock() |
||||
defer fc.mu.Unlock() |
||||
|
||||
// Iterate all the files and gather their metadata
|
||||
all := mapset.NewThreadUnsafeSet[string]() |
||||
mods := mapset.NewThreadUnsafeSet[string]() |
||||
|
||||
var newLastMod time.Time |
||||
for _, fi := range files { |
||||
path := filepath.Join(keyDir, fi.Name()) |
||||
// Skip any non-key files from the folder
|
||||
if nonKeyFile(fi) { |
||||
log.Trace("Ignoring file on account scan", "path", path) |
||||
continue |
||||
} |
||||
// Gather the set of all and freshly modified files
|
||||
all.Add(path) |
||||
|
||||
info, err := fi.Info() |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
modified := info.ModTime() |
||||
if modified.After(fc.lastMod) { |
||||
mods.Add(path) |
||||
} |
||||
if modified.After(newLastMod) { |
||||
newLastMod = modified |
||||
} |
||||
} |
||||
t2 := time.Now() |
||||
|
||||
// Update the tracked files and return the three sets
|
||||
deletes := fc.all.Difference(all) // Deletes = previous - current
|
||||
creates := all.Difference(fc.all) // Creates = current - previous
|
||||
updates := mods.Difference(creates) // Updates = modified - creates
|
||||
|
||||
fc.all, fc.lastMod = all, newLastMod |
||||
t3 := time.Now() |
||||
|
||||
// Report on the scanning stats and return
|
||||
log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2)) |
||||
return creates, deletes, updates, nil |
||||
} |
||||
|
||||
// nonKeyFile ignores editor backups, hidden files and folders/symlinks.
|
||||
func nonKeyFile(fi os.DirEntry) bool { |
||||
// Skip editor backups and UNIX-style hidden files.
|
||||
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") { |
||||
return true |
||||
} |
||||
// Skip misc special files, directories (yes, symlinks too).
|
||||
if fi.IsDir() || !fi.Type().IsRegular() { |
||||
return true |
||||
} |
||||
return false |
||||
} |
@ -1,237 +0,0 @@ |
||||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/ecdsa" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"fmt" |
||||
"io" |
||||
"os" |
||||
"path/filepath" |
||||
"strings" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/google/uuid" |
||||
) |
||||
|
||||
const ( |
||||
version = 3 |
||||
) |
||||
|
||||
type Key struct { |
||||
Id uuid.UUID // Version 4 "random" for unique id not derived from key data
|
||||
// to simplify lookups we also store the address
|
||||
Address common.Address |
||||
// we only store privkey as pubkey/address can be derived from it
|
||||
// privkey in this struct is always in plaintext
|
||||
PrivateKey *ecdsa.PrivateKey |
||||
} |
||||
|
||||
type keyStore interface { |
||||
// Loads and decrypts the key from disk.
|
||||
GetKey(addr common.Address, filename string, auth string) (*Key, error) |
||||
// Writes and encrypts the key.
|
||||
StoreKey(filename string, k *Key, auth string) error |
||||
// Joins filename with the key directory unless it is already absolute.
|
||||
JoinPath(filename string) string |
||||
} |
||||
|
||||
type plainKeyJSON struct { |
||||
Address string `json:"address"` |
||||
PrivateKey string `json:"privatekey"` |
||||
Id string `json:"id"` |
||||
Version int `json:"version"` |
||||
} |
||||
|
||||
type encryptedKeyJSONV3 struct { |
||||
Address string `json:"address"` |
||||
Crypto CryptoJSON `json:"crypto"` |
||||
Id string `json:"id"` |
||||
Version int `json:"version"` |
||||
} |
||||
|
||||
type encryptedKeyJSONV1 struct { |
||||
Address string `json:"address"` |
||||
Crypto CryptoJSON `json:"crypto"` |
||||
Id string `json:"id"` |
||||
Version string `json:"version"` |
||||
} |
||||
|
||||
type CryptoJSON struct { |
||||
Cipher string `json:"cipher"` |
||||
CipherText string `json:"ciphertext"` |
||||
CipherParams cipherparamsJSON `json:"cipherparams"` |
||||
KDF string `json:"kdf"` |
||||
KDFParams map[string]interface{} `json:"kdfparams"` |
||||
MAC string `json:"mac"` |
||||
} |
||||
|
||||
type cipherparamsJSON struct { |
||||
IV string `json:"iv"` |
||||
} |
||||
|
||||
func (k *Key) MarshalJSON() (j []byte, err error) { |
||||
jStruct := plainKeyJSON{ |
||||
hex.EncodeToString(k.Address[:]), |
||||
hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)), |
||||
k.Id.String(), |
||||
version, |
||||
} |
||||
j, err = json.Marshal(jStruct) |
||||
return j, err |
||||
} |
||||
|
||||
func (k *Key) UnmarshalJSON(j []byte) (err error) { |
||||
keyJSON := new(plainKeyJSON) |
||||
err = json.Unmarshal(j, &keyJSON) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
u := new(uuid.UUID) |
||||
*u, err = uuid.Parse(keyJSON.Id) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
k.Id = *u |
||||
addr, err := hex.DecodeString(keyJSON.Address) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
privkey, err := crypto.HexToECDSA(keyJSON.PrivateKey) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
k.Address = common.BytesToAddress(addr) |
||||
k.PrivateKey = privkey |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key { |
||||
id, err := uuid.NewRandom() |
||||
if err != nil { |
||||
panic(fmt.Sprintf("Could not create random uuid: %v", err)) |
||||
} |
||||
key := &Key{ |
||||
Id: id, |
||||
Address: crypto.PubkeyToAddress(privateKeyECDSA.PublicKey), |
||||
PrivateKey: privateKeyECDSA, |
||||
} |
||||
return key |
||||
} |
||||
|
||||
// NewKeyForDirectICAP generates a key whose address fits into < 155 bits so it can fit
|
||||
// into the Direct ICAP spec. for simplicity and easier compatibility with other libs, we
|
||||
// retry until the first byte is 0.
|
||||
func NewKeyForDirectICAP(rand io.Reader) *Key { |
||||
randBytes := make([]byte, 64) |
||||
_, err := rand.Read(randBytes) |
||||
if err != nil { |
||||
panic("key generation: could not read from random source: " + err.Error()) |
||||
} |
||||
reader := bytes.NewReader(randBytes) |
||||
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), reader) |
||||
if err != nil { |
||||
panic("key generation: ecdsa.GenerateKey failed: " + err.Error()) |
||||
} |
||||
key := newKeyFromECDSA(privateKeyECDSA) |
||||
if !strings.HasPrefix(key.Address.Hex(), "0x00") { |
||||
return NewKeyForDirectICAP(rand) |
||||
} |
||||
return key |
||||
} |
||||
|
||||
func newKey(rand io.Reader) (*Key, error) { |
||||
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), rand) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return newKeyFromECDSA(privateKeyECDSA), nil |
||||
} |
||||
|
||||
func storeNewKey(ks keyStore, rand io.Reader, auth string) (*Key, accounts.Account, error) { |
||||
key, err := newKey(rand) |
||||
if err != nil { |
||||
return nil, accounts.Account{}, err |
||||
} |
||||
a := accounts.Account{ |
||||
Address: key.Address, |
||||
URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.JoinPath(keyFileName(key.Address))}, |
||||
} |
||||
if err := ks.StoreKey(a.URL.Path, key, auth); err != nil { |
||||
zeroKey(key.PrivateKey) |
||||
return nil, a, err |
||||
} |
||||
return key, a, err |
||||
} |
||||
|
||||
func writeTemporaryKeyFile(file string, content []byte) (string, error) { |
||||
// Create the keystore directory with appropriate permissions
|
||||
// in case it is not present yet.
|
||||
const dirPerm = 0700 |
||||
if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil { |
||||
return "", err |
||||
} |
||||
// Atomic write: create a temporary hidden file first
|
||||
// then move it into place. TempFile assigns mode 0600.
|
||||
f, err := os.CreateTemp(filepath.Dir(file), "."+filepath.Base(file)+".tmp") |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
if _, err := f.Write(content); err != nil { |
||||
f.Close() |
||||
os.Remove(f.Name()) |
||||
return "", err |
||||
} |
||||
f.Close() |
||||
return f.Name(), nil |
||||
} |
||||
|
||||
func writeKeyFile(file string, content []byte) error { |
||||
name, err := writeTemporaryKeyFile(file, content) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return os.Rename(name, file) |
||||
} |
||||
|
||||
// keyFileName implements the naming convention for keyfiles:
|
||||
// UTC--<created_at UTC ISO8601>-<address hex>
|
||||
func keyFileName(keyAddr common.Address) string { |
||||
ts := time.Now().UTC() |
||||
return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), hex.EncodeToString(keyAddr[:])) |
||||
} |
||||
|
||||
func toISO8601(t time.Time) string { |
||||
var tz string |
||||
name, offset := t.Zone() |
||||
if name == "UTC" { |
||||
tz = "Z" |
||||
} else { |
||||
tz = fmt.Sprintf("%03d00", offset/3600) |
||||
} |
||||
return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s", |
||||
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz) |
||||
} |
@ -1,515 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// Package keystore implements encrypted storage of secp256k1 private keys.
|
||||
//
|
||||
// Keys are stored as encrypted JSON files according to the Web3 Secret Storage specification.
|
||||
// See https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition for more information.
|
||||
package keystore |
||||
|
||||
import ( |
||||
"crypto/ecdsa" |
||||
crand "crypto/rand" |
||||
"errors" |
||||
"math/big" |
||||
"os" |
||||
"path/filepath" |
||||
"reflect" |
||||
"runtime" |
||||
"sync" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
) |
||||
|
||||
var ( |
||||
ErrLocked = accounts.NewAuthNeededError("password or unlock") |
||||
ErrNoMatch = errors.New("no key for given address or file") |
||||
ErrDecrypt = errors.New("could not decrypt key with given password") |
||||
|
||||
// ErrAccountAlreadyExists is returned if an account attempted to import is
|
||||
// already present in the keystore.
|
||||
ErrAccountAlreadyExists = errors.New("account already exists") |
||||
) |
||||
|
||||
// KeyStoreType is the reflect type of a keystore backend.
|
||||
var KeyStoreType = reflect.TypeOf(&KeyStore{}) |
||||
|
||||
// KeyStoreScheme is the protocol scheme prefixing account and wallet URLs.
|
||||
const KeyStoreScheme = "keystore" |
||||
|
||||
// Maximum time between wallet refreshes (if filesystem notifications don't work).
|
||||
const walletRefreshCycle = 3 * time.Second |
||||
|
||||
// KeyStore manages a key storage directory on disk.
|
||||
type KeyStore struct { |
||||
storage keyStore // Storage backend, might be cleartext or encrypted
|
||||
cache *accountCache // In-memory account cache over the filesystem storage
|
||||
changes chan struct{} // Channel receiving change notifications from the cache
|
||||
unlocked map[common.Address]*unlocked // Currently unlocked account (decrypted private keys)
|
||||
|
||||
wallets []accounts.Wallet // Wallet wrappers around the individual key files
|
||||
updateFeed event.Feed // Event feed to notify wallet additions/removals
|
||||
updateScope event.SubscriptionScope // Subscription scope tracking current live listeners
|
||||
updating bool // Whether the event notification loop is running
|
||||
|
||||
mu sync.RWMutex |
||||
importMu sync.Mutex // Import Mutex locks the import to prevent two insertions from racing
|
||||
} |
||||
|
||||
type unlocked struct { |
||||
*Key |
||||
abort chan struct{} |
||||
} |
||||
|
||||
// NewKeyStore creates a keystore for the given directory.
|
||||
func NewKeyStore(keydir string, scryptN, scryptP int) *KeyStore { |
||||
keydir, _ = filepath.Abs(keydir) |
||||
ks := &KeyStore{storage: &keyStorePassphrase{keydir, scryptN, scryptP, false}} |
||||
ks.init(keydir) |
||||
return ks |
||||
} |
||||
|
||||
// NewPlaintextKeyStore creates a keystore for the given directory.
|
||||
// Deprecated: Use NewKeyStore.
|
||||
func NewPlaintextKeyStore(keydir string) *KeyStore { |
||||
keydir, _ = filepath.Abs(keydir) |
||||
ks := &KeyStore{storage: &keyStorePlain{keydir}} |
||||
ks.init(keydir) |
||||
return ks |
||||
} |
||||
|
||||
func (ks *KeyStore) init(keydir string) { |
||||
// Lock the mutex since the account cache might call back with events
|
||||
ks.mu.Lock() |
||||
defer ks.mu.Unlock() |
||||
|
||||
// Initialize the set of unlocked keys and the account cache
|
||||
ks.unlocked = make(map[common.Address]*unlocked) |
||||
ks.cache, ks.changes = newAccountCache(keydir) |
||||
|
||||
// TODO: In order for this finalizer to work, there must be no references
|
||||
// to ks. addressCache doesn't keep a reference but unlocked keys do,
|
||||
// so the finalizer will not trigger until all timed unlocks have expired.
|
||||
runtime.SetFinalizer(ks, func(m *KeyStore) { |
||||
m.cache.close() |
||||
}) |
||||
// Create the initial list of wallets from the cache
|
||||
accs := ks.cache.accounts() |
||||
ks.wallets = make([]accounts.Wallet, len(accs)) |
||||
for i := 0; i < len(accs); i++ { |
||||
ks.wallets[i] = &keystoreWallet{account: accs[i], keystore: ks} |
||||
} |
||||
} |
||||
|
||||
// Wallets implements accounts.Backend, returning all single-key wallets from the
|
||||
// keystore directory.
|
||||
func (ks *KeyStore) Wallets() []accounts.Wallet { |
||||
// Make sure the list of wallets is in sync with the account cache
|
||||
ks.refreshWallets() |
||||
|
||||
ks.mu.RLock() |
||||
defer ks.mu.RUnlock() |
||||
|
||||
cpy := make([]accounts.Wallet, len(ks.wallets)) |
||||
copy(cpy, ks.wallets) |
||||
return cpy |
||||
} |
||||
|
||||
// refreshWallets retrieves the current account list and based on that does any
|
||||
// necessary wallet refreshes.
|
||||
func (ks *KeyStore) refreshWallets() { |
||||
// Retrieve the current list of accounts
|
||||
ks.mu.Lock() |
||||
accs := ks.cache.accounts() |
||||
|
||||
// Transform the current list of wallets into the new one
|
||||
var ( |
||||
wallets = make([]accounts.Wallet, 0, len(accs)) |
||||
events []accounts.WalletEvent |
||||
) |
||||
|
||||
for _, account := range accs { |
||||
// Drop wallets while they were in front of the next account
|
||||
for len(ks.wallets) > 0 && ks.wallets[0].URL().Cmp(account.URL) < 0 { |
||||
events = append(events, accounts.WalletEvent{Wallet: ks.wallets[0], Kind: accounts.WalletDropped}) |
||||
ks.wallets = ks.wallets[1:] |
||||
} |
||||
// If there are no more wallets or the account is before the next, wrap new wallet
|
||||
if len(ks.wallets) == 0 || ks.wallets[0].URL().Cmp(account.URL) > 0 { |
||||
wallet := &keystoreWallet{account: account, keystore: ks} |
||||
|
||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived}) |
||||
wallets = append(wallets, wallet) |
||||
continue |
||||
} |
||||
// If the account is the same as the first wallet, keep it
|
||||
if ks.wallets[0].Accounts()[0] == account { |
||||
wallets = append(wallets, ks.wallets[0]) |
||||
ks.wallets = ks.wallets[1:] |
||||
continue |
||||
} |
||||
} |
||||
// Drop any leftover wallets and set the new batch
|
||||
for _, wallet := range ks.wallets { |
||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped}) |
||||
} |
||||
ks.wallets = wallets |
||||
ks.mu.Unlock() |
||||
|
||||
// Fire all wallet events and return
|
||||
for _, event := range events { |
||||
ks.updateFeed.Send(event) |
||||
} |
||||
} |
||||
|
||||
// Subscribe implements accounts.Backend, creating an async subscription to
|
||||
// receive notifications on the addition or removal of keystore wallets.
|
||||
func (ks *KeyStore) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { |
||||
// We need the mutex to reliably start/stop the update loop
|
||||
ks.mu.Lock() |
||||
defer ks.mu.Unlock() |
||||
|
||||
// Subscribe the caller and track the subscriber count
|
||||
sub := ks.updateScope.Track(ks.updateFeed.Subscribe(sink)) |
||||
|
||||
// Subscribers require an active notification loop, start it
|
||||
if !ks.updating { |
||||
ks.updating = true |
||||
go ks.updater() |
||||
} |
||||
return sub |
||||
} |
||||
|
||||
// updater is responsible for maintaining an up-to-date list of wallets stored in
|
||||
// the keystore, and for firing wallet addition/removal events. It listens for
|
||||
// account change events from the underlying account cache, and also periodically
|
||||
// forces a manual refresh (only triggers for systems where the filesystem notifier
|
||||
// is not running).
|
||||
func (ks *KeyStore) updater() { |
||||
for { |
||||
// Wait for an account update or a refresh timeout
|
||||
select { |
||||
case <-ks.changes: |
||||
case <-time.After(walletRefreshCycle): |
||||
} |
||||
// Run the wallet refresher
|
||||
ks.refreshWallets() |
||||
|
||||
// If all our subscribers left, stop the updater
|
||||
ks.mu.Lock() |
||||
if ks.updateScope.Count() == 0 { |
||||
ks.updating = false |
||||
ks.mu.Unlock() |
||||
return |
||||
} |
||||
ks.mu.Unlock() |
||||
} |
||||
} |
||||
|
||||
// HasAddress reports whether a key with the given address is present.
|
||||
func (ks *KeyStore) HasAddress(addr common.Address) bool { |
||||
return ks.cache.hasAddress(addr) |
||||
} |
||||
|
||||
// Accounts returns all key files present in the directory.
|
||||
func (ks *KeyStore) Accounts() []accounts.Account { |
||||
return ks.cache.accounts() |
||||
} |
||||
|
||||
// Delete deletes the key matched by account if the passphrase is correct.
|
||||
// If the account contains no filename, the address must match a unique key.
|
||||
func (ks *KeyStore) Delete(a accounts.Account, passphrase string) error { |
||||
// Decrypting the key isn't really necessary, but we do
|
||||
// it anyway to check the password and zero out the key
|
||||
// immediately afterwards.
|
||||
a, key, err := ks.getDecryptedKey(a, passphrase) |
||||
if key != nil { |
||||
zeroKey(key.PrivateKey) |
||||
} |
||||
if err != nil { |
||||
return err |
||||
} |
||||
// The order is crucial here. The key is dropped from the
|
||||
// cache after the file is gone so that a reload happening in
|
||||
// between won't insert it into the cache again.
|
||||
err = os.Remove(a.URL.Path) |
||||
if err == nil { |
||||
ks.cache.delete(a) |
||||
ks.refreshWallets() |
||||
} |
||||
return err |
||||
} |
||||
|
||||
// SignHash calculates a ECDSA signature for the given hash. The produced
|
||||
// signature is in the [R || S || V] format where V is 0 or 1.
|
||||
func (ks *KeyStore) SignHash(a accounts.Account, hash []byte) ([]byte, error) { |
||||
// Look up the key to sign with and abort if it cannot be found
|
||||
ks.mu.RLock() |
||||
defer ks.mu.RUnlock() |
||||
|
||||
unlockedKey, found := ks.unlocked[a.Address] |
||||
if !found { |
||||
return nil, ErrLocked |
||||
} |
||||
// Sign the hash using plain ECDSA operations
|
||||
return crypto.Sign(hash, unlockedKey.PrivateKey) |
||||
} |
||||
|
||||
// SignTx signs the given transaction with the requested account.
|
||||
func (ks *KeyStore) SignTx(a accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
||||
// Look up the key to sign with and abort if it cannot be found
|
||||
ks.mu.RLock() |
||||
defer ks.mu.RUnlock() |
||||
|
||||
unlockedKey, found := ks.unlocked[a.Address] |
||||
if !found { |
||||
return nil, ErrLocked |
||||
} |
||||
// Depending on the presence of the chain ID, sign with 2718 or homestead
|
||||
signer := types.LatestSignerForChainID(chainID) |
||||
return types.SignTx(tx, signer, unlockedKey.PrivateKey) |
||||
} |
||||
|
||||
// SignHashWithPassphrase signs hash if the private key matching the given address
|
||||
// can be decrypted with the given passphrase. The produced signature is in the
|
||||
// [R || S || V] format where V is 0 or 1.
|
||||
func (ks *KeyStore) SignHashWithPassphrase(a accounts.Account, passphrase string, hash []byte) (signature []byte, err error) { |
||||
_, key, err := ks.getDecryptedKey(a, passphrase) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer zeroKey(key.PrivateKey) |
||||
return crypto.Sign(hash, key.PrivateKey) |
||||
} |
||||
|
||||
// SignTxWithPassphrase signs the transaction if the private key matching the
|
||||
// given address can be decrypted with the given passphrase.
|
||||
func (ks *KeyStore) SignTxWithPassphrase(a accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
||||
_, key, err := ks.getDecryptedKey(a, passphrase) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer zeroKey(key.PrivateKey) |
||||
// Depending on the presence of the chain ID, sign with or without replay protection.
|
||||
signer := types.LatestSignerForChainID(chainID) |
||||
return types.SignTx(tx, signer, key.PrivateKey) |
||||
} |
||||
|
||||
// Unlock unlocks the given account indefinitely.
|
||||
func (ks *KeyStore) Unlock(a accounts.Account, passphrase string) error { |
||||
return ks.TimedUnlock(a, passphrase, 0) |
||||
} |
||||
|
||||
// Lock removes the private key with the given address from memory.
|
||||
func (ks *KeyStore) Lock(addr common.Address) error { |
||||
ks.mu.Lock() |
||||
if unl, found := ks.unlocked[addr]; found { |
||||
ks.mu.Unlock() |
||||
ks.expire(addr, unl, time.Duration(0)*time.Nanosecond) |
||||
} else { |
||||
ks.mu.Unlock() |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// TimedUnlock unlocks the given account with the passphrase. The account
|
||||
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
|
||||
// until the program exits. The account must match a unique key file.
|
||||
//
|
||||
// If the account address is already unlocked for a duration, TimedUnlock extends or
|
||||
// shortens the active unlock timeout. If the address was previously unlocked
|
||||
// indefinitely the timeout is not altered.
|
||||
func (ks *KeyStore) TimedUnlock(a accounts.Account, passphrase string, timeout time.Duration) error { |
||||
a, key, err := ks.getDecryptedKey(a, passphrase) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
ks.mu.Lock() |
||||
defer ks.mu.Unlock() |
||||
u, found := ks.unlocked[a.Address] |
||||
if found { |
||||
if u.abort == nil { |
||||
// The address was unlocked indefinitely, so unlocking
|
||||
// it with a timeout would be confusing.
|
||||
zeroKey(key.PrivateKey) |
||||
return nil |
||||
} |
||||
// Terminate the expire goroutine and replace it below.
|
||||
close(u.abort) |
||||
} |
||||
if timeout > 0 { |
||||
u = &unlocked{Key: key, abort: make(chan struct{})} |
||||
go ks.expire(a.Address, u, timeout) |
||||
} else { |
||||
u = &unlocked{Key: key} |
||||
} |
||||
ks.unlocked[a.Address] = u |
||||
return nil |
||||
} |
||||
|
||||
// Find resolves the given account into a unique entry in the keystore.
|
||||
func (ks *KeyStore) Find(a accounts.Account) (accounts.Account, error) { |
||||
ks.cache.maybeReload() |
||||
ks.cache.mu.Lock() |
||||
a, err := ks.cache.find(a) |
||||
ks.cache.mu.Unlock() |
||||
return a, err |
||||
} |
||||
|
||||
func (ks *KeyStore) getDecryptedKey(a accounts.Account, auth string) (accounts.Account, *Key, error) { |
||||
a, err := ks.Find(a) |
||||
if err != nil { |
||||
return a, nil, err |
||||
} |
||||
key, err := ks.storage.GetKey(a.Address, a.URL.Path, auth) |
||||
return a, key, err |
||||
} |
||||
|
||||
func (ks *KeyStore) expire(addr common.Address, u *unlocked, timeout time.Duration) { |
||||
t := time.NewTimer(timeout) |
||||
defer t.Stop() |
||||
select { |
||||
case <-u.abort: |
||||
// just quit
|
||||
case <-t.C: |
||||
ks.mu.Lock() |
||||
// only drop if it's still the same key instance that dropLater
|
||||
// was launched with. we can check that using pointer equality
|
||||
// because the map stores a new pointer every time the key is
|
||||
// unlocked.
|
||||
if ks.unlocked[addr] == u { |
||||
zeroKey(u.PrivateKey) |
||||
delete(ks.unlocked, addr) |
||||
} |
||||
ks.mu.Unlock() |
||||
} |
||||
} |
||||
|
||||
// NewAccount generates a new key and stores it into the key directory,
|
||||
// encrypting it with the passphrase.
|
||||
func (ks *KeyStore) NewAccount(passphrase string) (accounts.Account, error) { |
||||
_, account, err := storeNewKey(ks.storage, crand.Reader, passphrase) |
||||
if err != nil { |
||||
return accounts.Account{}, err |
||||
} |
||||
// Add the account to the cache immediately rather
|
||||
// than waiting for file system notifications to pick it up.
|
||||
ks.cache.add(account) |
||||
ks.refreshWallets() |
||||
return account, nil |
||||
} |
||||
|
||||
// Export exports as a JSON key, encrypted with newPassphrase.
|
||||
func (ks *KeyStore) Export(a accounts.Account, passphrase, newPassphrase string) (keyJSON []byte, err error) { |
||||
_, key, err := ks.getDecryptedKey(a, passphrase) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
var N, P int |
||||
if store, ok := ks.storage.(*keyStorePassphrase); ok { |
||||
N, P = store.scryptN, store.scryptP |
||||
} else { |
||||
N, P = StandardScryptN, StandardScryptP |
||||
} |
||||
return EncryptKey(key, newPassphrase, N, P) |
||||
} |
||||
|
||||
// Import stores the given encrypted JSON key into the key directory.
|
||||
func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (accounts.Account, error) { |
||||
key, err := DecryptKey(keyJSON, passphrase) |
||||
if key != nil && key.PrivateKey != nil { |
||||
defer zeroKey(key.PrivateKey) |
||||
} |
||||
if err != nil { |
||||
return accounts.Account{}, err |
||||
} |
||||
ks.importMu.Lock() |
||||
defer ks.importMu.Unlock() |
||||
|
||||
if ks.cache.hasAddress(key.Address) { |
||||
return accounts.Account{ |
||||
Address: key.Address, |
||||
}, ErrAccountAlreadyExists |
||||
} |
||||
return ks.importKey(key, newPassphrase) |
||||
} |
||||
|
||||
// ImportECDSA stores the given key into the key directory, encrypting it with the passphrase.
|
||||
func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (accounts.Account, error) { |
||||
ks.importMu.Lock() |
||||
defer ks.importMu.Unlock() |
||||
|
||||
key := newKeyFromECDSA(priv) |
||||
if ks.cache.hasAddress(key.Address) { |
||||
return accounts.Account{ |
||||
Address: key.Address, |
||||
}, ErrAccountAlreadyExists |
||||
} |
||||
return ks.importKey(key, passphrase) |
||||
} |
||||
|
||||
func (ks *KeyStore) importKey(key *Key, passphrase string) (accounts.Account, error) { |
||||
a := accounts.Account{Address: key.Address, URL: accounts.URL{Scheme: KeyStoreScheme, Path: ks.storage.JoinPath(keyFileName(key.Address))}} |
||||
if err := ks.storage.StoreKey(a.URL.Path, key, passphrase); err != nil { |
||||
return accounts.Account{}, err |
||||
} |
||||
ks.cache.add(a) |
||||
ks.refreshWallets() |
||||
return a, nil |
||||
} |
||||
|
||||
// Update changes the passphrase of an existing account.
|
||||
func (ks *KeyStore) Update(a accounts.Account, passphrase, newPassphrase string) error { |
||||
a, key, err := ks.getDecryptedKey(a, passphrase) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return ks.storage.StoreKey(a.URL.Path, key, newPassphrase) |
||||
} |
||||
|
||||
// ImportPreSaleKey decrypts the given Ethereum presale wallet and stores
|
||||
// a key file in the key directory. The key file is encrypted with the same passphrase.
|
||||
func (ks *KeyStore) ImportPreSaleKey(keyJSON []byte, passphrase string) (accounts.Account, error) { |
||||
a, _, err := importPreSaleKey(ks.storage, keyJSON, passphrase) |
||||
if err != nil { |
||||
return a, err |
||||
} |
||||
ks.cache.add(a) |
||||
ks.refreshWallets() |
||||
return a, nil |
||||
} |
||||
|
||||
// isUpdating returns whether the event notification loop is running.
|
||||
// This method is mainly meant for tests.
|
||||
func (ks *KeyStore) isUpdating() bool { |
||||
ks.mu.RLock() |
||||
defer ks.mu.RUnlock() |
||||
return ks.updating |
||||
} |
||||
|
||||
// zeroKey zeroes a private key in memory.
|
||||
func zeroKey(k *ecdsa.PrivateKey) { |
||||
b := k.D.Bits() |
||||
for i := range b { |
||||
b[i] = 0 |
||||
} |
||||
} |
@ -1,460 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"math/rand" |
||||
"os" |
||||
"runtime" |
||||
"sort" |
||||
"strings" |
||||
"sync" |
||||
"sync/atomic" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
) |
||||
|
||||
var testSigData = make([]byte, 32) |
||||
|
||||
func TestKeyStore(t *testing.T) { |
||||
dir, ks := tmpKeyStore(t, true) |
||||
|
||||
a, err := ks.NewAccount("foo") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !strings.HasPrefix(a.URL.Path, dir) { |
||||
t.Errorf("account file %s doesn't have dir prefix", a.URL) |
||||
} |
||||
stat, err := os.Stat(a.URL.Path) |
||||
if err != nil { |
||||
t.Fatalf("account file %s doesn't exist (%v)", a.URL, err) |
||||
} |
||||
if runtime.GOOS != "windows" && stat.Mode() != 0600 { |
||||
t.Fatalf("account file has wrong mode: got %o, want %o", stat.Mode(), 0600) |
||||
} |
||||
if !ks.HasAddress(a.Address) { |
||||
t.Errorf("HasAccount(%x) should've returned true", a.Address) |
||||
} |
||||
if err := ks.Update(a, "foo", "bar"); err != nil { |
||||
t.Errorf("Update error: %v", err) |
||||
} |
||||
if err := ks.Delete(a, "bar"); err != nil { |
||||
t.Errorf("Delete error: %v", err) |
||||
} |
||||
if common.FileExist(a.URL.Path) { |
||||
t.Errorf("account file %s should be gone after Delete", a.URL) |
||||
} |
||||
if ks.HasAddress(a.Address) { |
||||
t.Errorf("HasAccount(%x) should've returned true after Delete", a.Address) |
||||
} |
||||
} |
||||
|
||||
func TestSign(t *testing.T) { |
||||
_, ks := tmpKeyStore(t, true) |
||||
|
||||
pass := "" // not used but required by API
|
||||
a1, err := ks.NewAccount(pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if err := ks.Unlock(a1, ""); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if _, err := ks.SignHash(accounts.Account{Address: a1.Address}, testSigData); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
func TestSignWithPassphrase(t *testing.T) { |
||||
_, ks := tmpKeyStore(t, true) |
||||
|
||||
pass := "passwd" |
||||
acc, err := ks.NewAccount(pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
if _, unlocked := ks.unlocked[acc.Address]; unlocked { |
||||
t.Fatal("expected account to be locked") |
||||
} |
||||
|
||||
_, err = ks.SignHashWithPassphrase(acc, pass, testSigData) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
if _, unlocked := ks.unlocked[acc.Address]; unlocked { |
||||
t.Fatal("expected account to be locked") |
||||
} |
||||
|
||||
if _, err = ks.SignHashWithPassphrase(acc, "invalid passwd", testSigData); err == nil { |
||||
t.Fatal("expected SignHashWithPassphrase to fail with invalid password") |
||||
} |
||||
} |
||||
|
||||
func TestTimedUnlock(t *testing.T) { |
||||
t.Parallel() |
||||
_, ks := tmpKeyStore(t, true) |
||||
|
||||
pass := "foo" |
||||
a1, err := ks.NewAccount(pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// Signing without passphrase fails because account is locked
|
||||
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData) |
||||
if err != ErrLocked { |
||||
t.Fatal("Signing should've failed with ErrLocked before unlocking, got ", err) |
||||
} |
||||
|
||||
// Signing with passphrase works
|
||||
if err = ks.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// Signing without passphrase works because account is temp unlocked
|
||||
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData) |
||||
if err != nil { |
||||
t.Fatal("Signing shouldn't return an error after unlocking, got ", err) |
||||
} |
||||
|
||||
// Signing fails again after automatic locking
|
||||
time.Sleep(250 * time.Millisecond) |
||||
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData) |
||||
if err != ErrLocked { |
||||
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err) |
||||
} |
||||
} |
||||
|
||||
func TestOverrideUnlock(t *testing.T) { |
||||
t.Parallel() |
||||
_, ks := tmpKeyStore(t, false) |
||||
|
||||
pass := "foo" |
||||
a1, err := ks.NewAccount(pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// Unlock indefinitely.
|
||||
if err = ks.TimedUnlock(a1, pass, 5*time.Minute); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// Signing without passphrase works because account is temp unlocked
|
||||
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData) |
||||
if err != nil { |
||||
t.Fatal("Signing shouldn't return an error after unlocking, got ", err) |
||||
} |
||||
|
||||
// reset unlock to a shorter period, invalidates the previous unlock
|
||||
if err = ks.TimedUnlock(a1, pass, 100*time.Millisecond); err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
// Signing without passphrase still works because account is temp unlocked
|
||||
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData) |
||||
if err != nil { |
||||
t.Fatal("Signing shouldn't return an error after unlocking, got ", err) |
||||
} |
||||
|
||||
// Signing fails again after automatic locking
|
||||
time.Sleep(250 * time.Millisecond) |
||||
_, err = ks.SignHash(accounts.Account{Address: a1.Address}, testSigData) |
||||
if err != ErrLocked { |
||||
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err) |
||||
} |
||||
} |
||||
|
||||
// This test should fail under -race if signing races the expiration goroutine.
|
||||
func TestSignRace(t *testing.T) { |
||||
t.Parallel() |
||||
_, ks := tmpKeyStore(t, false) |
||||
|
||||
// Create a test account.
|
||||
a1, err := ks.NewAccount("") |
||||
if err != nil { |
||||
t.Fatal("could not create the test account", err) |
||||
} |
||||
|
||||
if err := ks.TimedUnlock(a1, "", 15*time.Millisecond); err != nil { |
||||
t.Fatal("could not unlock the test account", err) |
||||
} |
||||
end := time.Now().Add(500 * time.Millisecond) |
||||
for time.Now().Before(end) { |
||||
if _, err := ks.SignHash(accounts.Account{Address: a1.Address}, testSigData); err == ErrLocked { |
||||
return |
||||
} else if err != nil { |
||||
t.Errorf("Sign error: %v", err) |
||||
return |
||||
} |
||||
time.Sleep(1 * time.Millisecond) |
||||
} |
||||
t.Errorf("Account did not lock within the timeout") |
||||
} |
||||
|
||||
// waitForKsUpdating waits until the updating-status of the ks reaches the
|
||||
// desired wantStatus.
|
||||
// It waits for a maximum time of maxTime, and returns false if it does not
|
||||
// finish in time
|
||||
func waitForKsUpdating(t *testing.T, ks *KeyStore, wantStatus bool, maxTime time.Duration) bool { |
||||
t.Helper() |
||||
// Wait max 250 ms, then return false
|
||||
for t0 := time.Now(); time.Since(t0) < maxTime; { |
||||
if ks.isUpdating() == wantStatus { |
||||
return true |
||||
} |
||||
time.Sleep(25 * time.Millisecond) |
||||
} |
||||
return false |
||||
} |
||||
|
||||
// Tests that the wallet notifier loop starts and stops correctly based on the
|
||||
// addition and removal of wallet event subscriptions.
|
||||
func TestWalletNotifierLifecycle(t *testing.T) { |
||||
t.Parallel() |
||||
// Create a temporary keystore to test with
|
||||
_, ks := tmpKeyStore(t, false) |
||||
|
||||
// Ensure that the notification updater is not running yet
|
||||
time.Sleep(250 * time.Millisecond) |
||||
|
||||
if ks.isUpdating() { |
||||
t.Errorf("wallet notifier running without subscribers") |
||||
} |
||||
// Subscribe to the wallet feed and ensure the updater boots up
|
||||
updates := make(chan accounts.WalletEvent) |
||||
|
||||
subs := make([]event.Subscription, 2) |
||||
for i := 0; i < len(subs); i++ { |
||||
// Create a new subscription
|
||||
subs[i] = ks.Subscribe(updates) |
||||
if !waitForKsUpdating(t, ks, true, 250*time.Millisecond) { |
||||
t.Errorf("sub %d: wallet notifier not running after subscription", i) |
||||
} |
||||
} |
||||
// Close all but one sub
|
||||
for i := 0; i < len(subs)-1; i++ { |
||||
// Close an existing subscription
|
||||
subs[i].Unsubscribe() |
||||
} |
||||
// Check that it is still running
|
||||
time.Sleep(250 * time.Millisecond) |
||||
|
||||
if !ks.isUpdating() { |
||||
t.Fatal("event notifier stopped prematurely") |
||||
} |
||||
// Unsubscribe the last one and ensure the updater terminates eventually.
|
||||
subs[len(subs)-1].Unsubscribe() |
||||
if !waitForKsUpdating(t, ks, false, 4*time.Second) { |
||||
t.Errorf("wallet notifier didn't terminate after unsubscribe") |
||||
} |
||||
} |
||||
|
||||
type walletEvent struct { |
||||
accounts.WalletEvent |
||||
a accounts.Account |
||||
} |
||||
|
||||
// Tests that wallet notifications and correctly fired when accounts are added
|
||||
// or deleted from the keystore.
|
||||
func TestWalletNotifications(t *testing.T) { |
||||
_, ks := tmpKeyStore(t, false) |
||||
|
||||
// Subscribe to the wallet feed and collect events.
|
||||
var ( |
||||
events []walletEvent |
||||
updates = make(chan accounts.WalletEvent) |
||||
sub = ks.Subscribe(updates) |
||||
) |
||||
defer sub.Unsubscribe() |
||||
go func() { |
||||
for { |
||||
select { |
||||
case ev := <-updates: |
||||
events = append(events, walletEvent{ev, ev.Wallet.Accounts()[0]}) |
||||
case <-sub.Err(): |
||||
close(updates) |
||||
return |
||||
} |
||||
} |
||||
}() |
||||
|
||||
// Randomly add and remove accounts.
|
||||
var ( |
||||
live = make(map[common.Address]accounts.Account) |
||||
wantEvents []walletEvent |
||||
) |
||||
for i := 0; i < 1024; i++ { |
||||
if create := len(live) == 0 || rand.Int()%4 > 0; create { |
||||
// Add a new account and ensure wallet notifications arrives
|
||||
account, err := ks.NewAccount("") |
||||
if err != nil { |
||||
t.Fatalf("failed to create test account: %v", err) |
||||
} |
||||
live[account.Address] = account |
||||
wantEvents = append(wantEvents, walletEvent{accounts.WalletEvent{Kind: accounts.WalletArrived}, account}) |
||||
} else { |
||||
// Delete a random account.
|
||||
var account accounts.Account |
||||
for _, a := range live { |
||||
account = a |
||||
break |
||||
} |
||||
if err := ks.Delete(account, ""); err != nil { |
||||
t.Fatalf("failed to delete test account: %v", err) |
||||
} |
||||
delete(live, account.Address) |
||||
wantEvents = append(wantEvents, walletEvent{accounts.WalletEvent{Kind: accounts.WalletDropped}, account}) |
||||
} |
||||
} |
||||
|
||||
// Shut down the event collector and check events.
|
||||
sub.Unsubscribe() |
||||
for ev := range updates { |
||||
events = append(events, walletEvent{ev, ev.Wallet.Accounts()[0]}) |
||||
} |
||||
checkAccounts(t, live, ks.Wallets()) |
||||
checkEvents(t, wantEvents, events) |
||||
} |
||||
|
||||
// TestImportExport tests the import functionality of a keystore.
|
||||
func TestImportECDSA(t *testing.T) { |
||||
_, ks := tmpKeyStore(t, true) |
||||
key, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
t.Fatalf("failed to generate key: %v", key) |
||||
} |
||||
if _, err = ks.ImportECDSA(key, "old"); err != nil { |
||||
t.Errorf("importing failed: %v", err) |
||||
} |
||||
if _, err = ks.ImportECDSA(key, "old"); err == nil { |
||||
t.Errorf("importing same key twice succeeded") |
||||
} |
||||
if _, err = ks.ImportECDSA(key, "new"); err == nil { |
||||
t.Errorf("importing same key twice succeeded") |
||||
} |
||||
} |
||||
|
||||
// TestImportECDSA tests the import and export functionality of a keystore.
|
||||
func TestImportExport(t *testing.T) { |
||||
_, ks := tmpKeyStore(t, true) |
||||
acc, err := ks.NewAccount("old") |
||||
if err != nil { |
||||
t.Fatalf("failed to create account: %v", acc) |
||||
} |
||||
json, err := ks.Export(acc, "old", "new") |
||||
if err != nil { |
||||
t.Fatalf("failed to export account: %v", acc) |
||||
} |
||||
_, ks2 := tmpKeyStore(t, true) |
||||
if _, err = ks2.Import(json, "old", "old"); err == nil { |
||||
t.Errorf("importing with invalid password succeeded") |
||||
} |
||||
acc2, err := ks2.Import(json, "new", "new") |
||||
if err != nil { |
||||
t.Errorf("importing failed: %v", err) |
||||
} |
||||
if acc.Address != acc2.Address { |
||||
t.Error("imported account does not match exported account") |
||||
} |
||||
if _, err = ks2.Import(json, "new", "new"); err == nil { |
||||
t.Errorf("importing a key twice succeeded") |
||||
} |
||||
} |
||||
|
||||
// TestImportRace tests the keystore on races.
|
||||
// This test should fail under -race if importing races.
|
||||
func TestImportRace(t *testing.T) { |
||||
_, ks := tmpKeyStore(t, true) |
||||
acc, err := ks.NewAccount("old") |
||||
if err != nil { |
||||
t.Fatalf("failed to create account: %v", acc) |
||||
} |
||||
json, err := ks.Export(acc, "old", "new") |
||||
if err != nil { |
||||
t.Fatalf("failed to export account: %v", acc) |
||||
} |
||||
_, ks2 := tmpKeyStore(t, true) |
||||
var atom uint32 |
||||
var wg sync.WaitGroup |
||||
wg.Add(2) |
||||
for i := 0; i < 2; i++ { |
||||
go func() { |
||||
defer wg.Done() |
||||
if _, err := ks2.Import(json, "new", "new"); err != nil { |
||||
atomic.AddUint32(&atom, 1) |
||||
} |
||||
}() |
||||
} |
||||
wg.Wait() |
||||
if atom != 1 { |
||||
t.Errorf("Import is racy") |
||||
} |
||||
} |
||||
|
||||
// checkAccounts checks that all known live accounts are present in the wallet list.
|
||||
func checkAccounts(t *testing.T, live map[common.Address]accounts.Account, wallets []accounts.Wallet) { |
||||
if len(live) != len(wallets) { |
||||
t.Errorf("wallet list doesn't match required accounts: have %d, want %d", len(wallets), len(live)) |
||||
return |
||||
} |
||||
liveList := make([]accounts.Account, 0, len(live)) |
||||
for _, account := range live { |
||||
liveList = append(liveList, account) |
||||
} |
||||
sort.Sort(accountsByURL(liveList)) |
||||
for j, wallet := range wallets { |
||||
if accs := wallet.Accounts(); len(accs) != 1 { |
||||
t.Errorf("wallet %d: contains invalid number of accounts: have %d, want 1", j, len(accs)) |
||||
} else if accs[0] != liveList[j] { |
||||
t.Errorf("wallet %d: account mismatch: have %v, want %v", j, accs[0], liveList[j]) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// checkEvents checks that all events in 'want' are present in 'have'. Events may be present multiple times.
|
||||
func checkEvents(t *testing.T, want []walletEvent, have []walletEvent) { |
||||
for _, wantEv := range want { |
||||
nmatch := 0 |
||||
for ; len(have) > 0; nmatch++ { |
||||
if have[0].Kind != wantEv.Kind || have[0].a != wantEv.a { |
||||
break |
||||
} |
||||
have = have[1:] |
||||
} |
||||
if nmatch == 0 { |
||||
t.Fatalf("can't find event with Kind=%v for %x", wantEv.Kind, wantEv.a.Address) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func tmpKeyStore(t *testing.T, encrypted bool) (string, *KeyStore) { |
||||
d := t.TempDir() |
||||
newKs := NewPlaintextKeyStore |
||||
if encrypted { |
||||
newKs = func(kd string) *KeyStore { return NewKeyStore(kd, veryLightScryptN, veryLightScryptP) } |
||||
} |
||||
return d, newKs(d) |
||||
} |
@ -1,365 +0,0 @@ |
||||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/* |
||||
|
||||
This key store behaves as KeyStorePlain with the difference that |
||||
the private key is encrypted and on disk uses another JSON encoding. |
||||
|
||||
The crypto is documented at https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition
|
||||
|
||||
*/ |
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/aes" |
||||
"crypto/rand" |
||||
"crypto/sha256" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"fmt" |
||||
"io" |
||||
"os" |
||||
"path/filepath" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/common/math" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/google/uuid" |
||||
"golang.org/x/crypto/pbkdf2" |
||||
"golang.org/x/crypto/scrypt" |
||||
) |
||||
|
||||
const ( |
||||
keyHeaderKDF = "scrypt" |
||||
|
||||
// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
|
||||
// memory and taking approximately 1s CPU time on a modern processor.
|
||||
StandardScryptN = 1 << 18 |
||||
|
||||
// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
|
||||
// memory and taking approximately 1s CPU time on a modern processor.
|
||||
StandardScryptP = 1 |
||||
|
||||
// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
|
||||
// memory and taking approximately 100ms CPU time on a modern processor.
|
||||
LightScryptN = 1 << 12 |
||||
|
||||
// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
|
||||
// memory and taking approximately 100ms CPU time on a modern processor.
|
||||
LightScryptP = 6 |
||||
|
||||
scryptR = 8 |
||||
scryptDKLen = 32 |
||||
) |
||||
|
||||
type keyStorePassphrase struct { |
||||
keysDirPath string |
||||
scryptN int |
||||
scryptP int |
||||
// skipKeyFileVerification disables the security-feature which does
|
||||
// reads and decrypts any newly created keyfiles. This should be 'false' in all
|
||||
// cases except tests -- setting this to 'true' is not recommended.
|
||||
skipKeyFileVerification bool |
||||
} |
||||
|
||||
func (ks keyStorePassphrase) GetKey(addr common.Address, filename, auth string) (*Key, error) { |
||||
// Load the key from the keystore and decrypt its contents
|
||||
keyjson, err := os.ReadFile(filename) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
key, err := DecryptKey(keyjson, auth) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Make sure we're really operating on the requested key (no swap attacks)
|
||||
if key.Address != addr { |
||||
return nil, fmt.Errorf("key content mismatch: have account %x, want %x", key.Address, addr) |
||||
} |
||||
return key, nil |
||||
} |
||||
|
||||
// StoreKey generates a key, encrypts with 'auth' and stores in the given directory
|
||||
func StoreKey(dir, auth string, scryptN, scryptP int) (accounts.Account, error) { |
||||
_, a, err := storeNewKey(&keyStorePassphrase{dir, scryptN, scryptP, false}, rand.Reader, auth) |
||||
return a, err |
||||
} |
||||
|
||||
func (ks keyStorePassphrase) StoreKey(filename string, key *Key, auth string) error { |
||||
keyjson, err := EncryptKey(key, auth, ks.scryptN, ks.scryptP) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
// Write into temporary file
|
||||
tmpName, err := writeTemporaryKeyFile(filename, keyjson) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if !ks.skipKeyFileVerification { |
||||
// Verify that we can decrypt the file with the given password.
|
||||
_, err = ks.GetKey(key.Address, tmpName, auth) |
||||
if err != nil { |
||||
msg := "An error was encountered when saving and verifying the keystore file. \n" + |
||||
"This indicates that the keystore is corrupted. \n" + |
||||
"The corrupted file is stored at \n%v\n" + |
||||
"Please file a ticket at:\n\n" + |
||||
"https://github.com/ethereum/go-ethereum/issues." + |
||||
"The error was : %s" |
||||
//lint:ignore ST1005 This is a message for the user
|
||||
return fmt.Errorf(msg, tmpName, err) |
||||
} |
||||
} |
||||
return os.Rename(tmpName, filename) |
||||
} |
||||
|
||||
func (ks keyStorePassphrase) JoinPath(filename string) string { |
||||
if filepath.IsAbs(filename) { |
||||
return filename |
||||
} |
||||
return filepath.Join(ks.keysDirPath, filename) |
||||
} |
||||
|
||||
// Encryptdata encrypts the data given as 'data' with the password 'auth'.
|
||||
func EncryptDataV3(data, auth []byte, scryptN, scryptP int) (CryptoJSON, error) { |
||||
salt := make([]byte, 32) |
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil { |
||||
panic("reading from crypto/rand failed: " + err.Error()) |
||||
} |
||||
derivedKey, err := scrypt.Key(auth, salt, scryptN, scryptR, scryptP, scryptDKLen) |
||||
if err != nil { |
||||
return CryptoJSON{}, err |
||||
} |
||||
encryptKey := derivedKey[:16] |
||||
|
||||
iv := make([]byte, aes.BlockSize) // 16
|
||||
if _, err := io.ReadFull(rand.Reader, iv); err != nil { |
||||
panic("reading from crypto/rand failed: " + err.Error()) |
||||
} |
||||
cipherText, err := aesCTRXOR(encryptKey, data, iv) |
||||
if err != nil { |
||||
return CryptoJSON{}, err |
||||
} |
||||
mac := crypto.Keccak256(derivedKey[16:32], cipherText) |
||||
|
||||
scryptParamsJSON := make(map[string]interface{}, 5) |
||||
scryptParamsJSON["n"] = scryptN |
||||
scryptParamsJSON["r"] = scryptR |
||||
scryptParamsJSON["p"] = scryptP |
||||
scryptParamsJSON["dklen"] = scryptDKLen |
||||
scryptParamsJSON["salt"] = hex.EncodeToString(salt) |
||||
cipherParamsJSON := cipherparamsJSON{ |
||||
IV: hex.EncodeToString(iv), |
||||
} |
||||
|
||||
cryptoStruct := CryptoJSON{ |
||||
Cipher: "aes-128-ctr", |
||||
CipherText: hex.EncodeToString(cipherText), |
||||
CipherParams: cipherParamsJSON, |
||||
KDF: keyHeaderKDF, |
||||
KDFParams: scryptParamsJSON, |
||||
MAC: hex.EncodeToString(mac), |
||||
} |
||||
return cryptoStruct, nil |
||||
} |
||||
|
||||
// EncryptKey encrypts a key using the specified scrypt parameters into a json
|
||||
// blob that can be decrypted later on.
|
||||
func EncryptKey(key *Key, auth string, scryptN, scryptP int) ([]byte, error) { |
||||
keyBytes := math.PaddedBigBytes(key.PrivateKey.D, 32) |
||||
cryptoStruct, err := EncryptDataV3(keyBytes, []byte(auth), scryptN, scryptP) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
encryptedKeyJSONV3 := encryptedKeyJSONV3{ |
||||
hex.EncodeToString(key.Address[:]), |
||||
cryptoStruct, |
||||
key.Id.String(), |
||||
version, |
||||
} |
||||
return json.Marshal(encryptedKeyJSONV3) |
||||
} |
||||
|
||||
// DecryptKey decrypts a key from a json blob, returning the private key itself.
|
||||
func DecryptKey(keyjson []byte, auth string) (*Key, error) { |
||||
// Parse the json into a simple map to fetch the key version
|
||||
m := make(map[string]interface{}) |
||||
if err := json.Unmarshal(keyjson, &m); err != nil { |
||||
return nil, err |
||||
} |
||||
// Depending on the version try to parse one way or another
|
||||
var ( |
||||
keyBytes, keyId []byte |
||||
err error |
||||
) |
||||
if version, ok := m["version"].(string); ok && version == "1" { |
||||
k := new(encryptedKeyJSONV1) |
||||
if err := json.Unmarshal(keyjson, k); err != nil { |
||||
return nil, err |
||||
} |
||||
keyBytes, keyId, err = decryptKeyV1(k, auth) |
||||
} else { |
||||
k := new(encryptedKeyJSONV3) |
||||
if err := json.Unmarshal(keyjson, k); err != nil { |
||||
return nil, err |
||||
} |
||||
keyBytes, keyId, err = decryptKeyV3(k, auth) |
||||
} |
||||
// Handle any decryption errors and return the key
|
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
key := crypto.ToECDSAUnsafe(keyBytes) |
||||
id, err := uuid.FromBytes(keyId) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &Key{ |
||||
Id: id, |
||||
Address: crypto.PubkeyToAddress(key.PublicKey), |
||||
PrivateKey: key, |
||||
}, nil |
||||
} |
||||
|
||||
func DecryptDataV3(cryptoJson CryptoJSON, auth string) ([]byte, error) { |
||||
if cryptoJson.Cipher != "aes-128-ctr" { |
||||
return nil, fmt.Errorf("cipher not supported: %v", cryptoJson.Cipher) |
||||
} |
||||
mac, err := hex.DecodeString(cryptoJson.MAC) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
iv, err := hex.DecodeString(cryptoJson.CipherParams.IV) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
cipherText, err := hex.DecodeString(cryptoJson.CipherText) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
derivedKey, err := getKDFKey(cryptoJson, auth) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText) |
||||
if !bytes.Equal(calculatedMAC, mac) { |
||||
return nil, ErrDecrypt |
||||
} |
||||
|
||||
plainText, err := aesCTRXOR(derivedKey[:16], cipherText, iv) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return plainText, err |
||||
} |
||||
|
||||
func decryptKeyV3(keyProtected *encryptedKeyJSONV3, auth string) (keyBytes []byte, keyId []byte, err error) { |
||||
if keyProtected.Version != version { |
||||
return nil, nil, fmt.Errorf("version not supported: %v", keyProtected.Version) |
||||
} |
||||
keyUUID, err := uuid.Parse(keyProtected.Id) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
keyId = keyUUID[:] |
||||
plainText, err := DecryptDataV3(keyProtected.Crypto, auth) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
return plainText, keyId, err |
||||
} |
||||
|
||||
func decryptKeyV1(keyProtected *encryptedKeyJSONV1, auth string) (keyBytes []byte, keyId []byte, err error) { |
||||
keyUUID, err := uuid.Parse(keyProtected.Id) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
keyId = keyUUID[:] |
||||
mac, err := hex.DecodeString(keyProtected.Crypto.MAC) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
iv, err := hex.DecodeString(keyProtected.Crypto.CipherParams.IV) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
cipherText, err := hex.DecodeString(keyProtected.Crypto.CipherText) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
derivedKey, err := getKDFKey(keyProtected.Crypto, auth) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
calculatedMAC := crypto.Keccak256(derivedKey[16:32], cipherText) |
||||
if !bytes.Equal(calculatedMAC, mac) { |
||||
return nil, nil, ErrDecrypt |
||||
} |
||||
|
||||
plainText, err := aesCBCDecrypt(crypto.Keccak256(derivedKey[:16])[:16], cipherText, iv) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
return plainText, keyId, err |
||||
} |
||||
|
||||
func getKDFKey(cryptoJSON CryptoJSON, auth string) ([]byte, error) { |
||||
authArray := []byte(auth) |
||||
salt, err := hex.DecodeString(cryptoJSON.KDFParams["salt"].(string)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
dkLen := ensureInt(cryptoJSON.KDFParams["dklen"]) |
||||
|
||||
if cryptoJSON.KDF == keyHeaderKDF { |
||||
n := ensureInt(cryptoJSON.KDFParams["n"]) |
||||
r := ensureInt(cryptoJSON.KDFParams["r"]) |
||||
p := ensureInt(cryptoJSON.KDFParams["p"]) |
||||
return scrypt.Key(authArray, salt, n, r, p, dkLen) |
||||
} else if cryptoJSON.KDF == "pbkdf2" { |
||||
c := ensureInt(cryptoJSON.KDFParams["c"]) |
||||
prf := cryptoJSON.KDFParams["prf"].(string) |
||||
if prf != "hmac-sha256" { |
||||
return nil, fmt.Errorf("unsupported PBKDF2 PRF: %s", prf) |
||||
} |
||||
key := pbkdf2.Key(authArray, salt, c, dkLen, sha256.New) |
||||
return key, nil |
||||
} |
||||
|
||||
return nil, fmt.Errorf("unsupported KDF: %s", cryptoJSON.KDF) |
||||
} |
||||
|
||||
// TODO: can we do without this when unmarshalling dynamic JSON?
|
||||
// why do integers in KDF params end up as float64 and not int after
|
||||
// unmarshal?
|
||||
func ensureInt(x interface{}) int { |
||||
res, ok := x.(int) |
||||
if !ok { |
||||
res = int(x.(float64)) |
||||
} |
||||
return res |
||||
} |
@ -1,60 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"os" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
const ( |
||||
veryLightScryptN = 2 |
||||
veryLightScryptP = 1 |
||||
) |
||||
|
||||
// Tests that a json key file can be decrypted and encrypted in multiple rounds.
|
||||
func TestKeyEncryptDecrypt(t *testing.T) { |
||||
keyjson, err := os.ReadFile("testdata/very-light-scrypt.json") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
password := "" |
||||
address := common.HexToAddress("45dea0fb0bba44f4fcf290bba71fd57d7117cbb8") |
||||
|
||||
// Do a few rounds of decryption and encryption
|
||||
for i := 0; i < 3; i++ { |
||||
// Try a bad password first
|
||||
if _, err := DecryptKey(keyjson, password+"bad"); err == nil { |
||||
t.Errorf("test %d: json key decrypted with bad password", i) |
||||
} |
||||
// Decrypt with the correct password
|
||||
key, err := DecryptKey(keyjson, password) |
||||
if err != nil { |
||||
t.Fatalf("test %d: json key failed to decrypt: %v", i, err) |
||||
} |
||||
if key.Address != address { |
||||
t.Errorf("test %d: key address mismatch: have %x, want %x", i, key.Address, address) |
||||
} |
||||
// Recrypt with a new password and start over
|
||||
password += "new data appended" // nolint: gosec
|
||||
if keyjson, err = EncryptKey(key, password, veryLightScryptN, veryLightScryptP); err != nil { |
||||
t.Errorf("test %d: failed to recrypt key %v", i, err) |
||||
} |
||||
} |
||||
} |
@ -1,61 +0,0 @@ |
||||
// Copyright 2015 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"fmt" |
||||
"os" |
||||
"path/filepath" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
type keyStorePlain struct { |
||||
keysDirPath string |
||||
} |
||||
|
||||
func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (*Key, error) { |
||||
fd, err := os.Open(filename) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
defer fd.Close() |
||||
key := new(Key) |
||||
if err := json.NewDecoder(fd).Decode(key); err != nil { |
||||
return nil, err |
||||
} |
||||
if key.Address != addr { |
||||
return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.Address, addr) |
||||
} |
||||
return key, nil |
||||
} |
||||
|
||||
func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error { |
||||
content, err := json.Marshal(key) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return writeKeyFile(filename, content) |
||||
} |
||||
|
||||
func (ks keyStorePlain) JoinPath(filename string) string { |
||||
if filepath.IsAbs(filename) { |
||||
return filename |
||||
} |
||||
return filepath.Join(ks.keysDirPath, filename) |
||||
} |
@ -1,257 +0,0 @@ |
||||
// Copyright 2014 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"crypto/rand" |
||||
"encoding/hex" |
||||
"fmt" |
||||
"path/filepath" |
||||
"reflect" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
func tmpKeyStoreIface(t *testing.T, encrypted bool) (dir string, ks keyStore) { |
||||
d := t.TempDir() |
||||
if encrypted { |
||||
ks = &keyStorePassphrase{d, veryLightScryptN, veryLightScryptP, true} |
||||
} else { |
||||
ks = &keyStorePlain{d} |
||||
} |
||||
return d, ks |
||||
} |
||||
|
||||
func TestKeyStorePlain(t *testing.T) { |
||||
_, ks := tmpKeyStoreIface(t, false) |
||||
|
||||
pass := "" // not used but required by API
|
||||
k1, account, err := storeNewKey(ks, rand.Reader, pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
k2, err := ks.GetKey(k1.Address, account.URL.Path, pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(k1.Address, k2.Address) { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) { |
||||
t.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
func TestKeyStorePassphrase(t *testing.T) { |
||||
_, ks := tmpKeyStoreIface(t, true) |
||||
|
||||
pass := "foo" |
||||
k1, account, err := storeNewKey(ks, rand.Reader, pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
k2, err := ks.GetKey(k1.Address, account.URL.Path, pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(k1.Address, k2.Address) { |
||||
t.Fatal(err) |
||||
} |
||||
if !reflect.DeepEqual(k1.PrivateKey, k2.PrivateKey) { |
||||
t.Fatal(err) |
||||
} |
||||
} |
||||
|
||||
func TestKeyStorePassphraseDecryptionFail(t *testing.T) { |
||||
_, ks := tmpKeyStoreIface(t, true) |
||||
|
||||
pass := "foo" |
||||
k1, account, err := storeNewKey(ks, rand.Reader, pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if _, err = ks.GetKey(k1.Address, account.URL.Path, "bar"); err != ErrDecrypt { |
||||
t.Fatalf("wrong error for invalid password\ngot %q\nwant %q", err, ErrDecrypt) |
||||
} |
||||
} |
||||
|
||||
func TestImportPreSaleKey(t *testing.T) { |
||||
dir, ks := tmpKeyStoreIface(t, true) |
||||
|
||||
// file content of a presale key file generated with:
|
||||
// python pyethsaletool.py genwallet
|
||||
// with password "foo"
|
||||
fileContent := "{\"encseed\": \"26d87f5f2bf9835f9a47eefae571bc09f9107bb13d54ff12a4ec095d01f83897494cf34f7bed2ed34126ecba9db7b62de56c9d7cd136520a0427bfb11b8954ba7ac39b90d4650d3448e31185affcd74226a68f1e94b1108e6e0a4a91cdd83eba\", \"ethaddr\": \"d4584b5f6229b7be90727b0fc8c6b91bb427821f\", \"email\": \"gustav.simonsson@gmail.com\", \"btcaddr\": \"1EVknXyFC68kKNLkh6YnKzW41svSRoaAcx\"}" |
||||
pass := "foo" |
||||
account, _, err := importPreSaleKey(ks, []byte(fileContent), pass) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if account.Address != common.HexToAddress("d4584b5f6229b7be90727b0fc8c6b91bb427821f") { |
||||
t.Errorf("imported account has wrong address %x", account.Address) |
||||
} |
||||
if !strings.HasPrefix(account.URL.Path, dir) { |
||||
t.Errorf("imported account file not in keystore directory: %q", account.URL) |
||||
} |
||||
} |
||||
|
||||
// Test and utils for the key store tests in the Ethereum JSON tests;
|
||||
// testdataKeyStoreTests/basic_tests.json
|
||||
type KeyStoreTestV3 struct { |
||||
Json encryptedKeyJSONV3 |
||||
Password string |
||||
Priv string |
||||
} |
||||
|
||||
type KeyStoreTestV1 struct { |
||||
Json encryptedKeyJSONV1 |
||||
Password string |
||||
Priv string |
||||
} |
||||
|
||||
func TestV3_PBKDF2_1(t *testing.T) { |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) |
||||
testDecryptV3(tests["wikipage_test_vector_pbkdf2"], t) |
||||
} |
||||
|
||||
var testsSubmodule = filepath.Join("..", "..", "tests", "testdata", "KeyStoreTests") |
||||
|
||||
func skipIfSubmoduleMissing(t *testing.T) { |
||||
if !common.FileExist(testsSubmodule) { |
||||
t.Skipf("can't find JSON tests from submodule at %s", testsSubmodule) |
||||
} |
||||
} |
||||
|
||||
func TestV3_PBKDF2_2(t *testing.T) { |
||||
skipIfSubmoduleMissing(t) |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) |
||||
testDecryptV3(tests["test1"], t) |
||||
} |
||||
|
||||
func TestV3_PBKDF2_3(t *testing.T) { |
||||
skipIfSubmoduleMissing(t) |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) |
||||
testDecryptV3(tests["python_generated_test_with_odd_iv"], t) |
||||
} |
||||
|
||||
func TestV3_PBKDF2_4(t *testing.T) { |
||||
skipIfSubmoduleMissing(t) |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) |
||||
testDecryptV3(tests["evilnonce"], t) |
||||
} |
||||
|
||||
func TestV3_Scrypt_1(t *testing.T) { |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) |
||||
testDecryptV3(tests["wikipage_test_vector_scrypt"], t) |
||||
} |
||||
|
||||
func TestV3_Scrypt_2(t *testing.T) { |
||||
skipIfSubmoduleMissing(t) |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3(filepath.Join(testsSubmodule, "basic_tests.json"), t) |
||||
testDecryptV3(tests["test2"], t) |
||||
} |
||||
|
||||
func TestV1_1(t *testing.T) { |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV1("testdata/v1_test_vector.json", t) |
||||
testDecryptV1(tests["test1"], t) |
||||
} |
||||
|
||||
func TestV1_2(t *testing.T) { |
||||
t.Parallel() |
||||
ks := &keyStorePassphrase{"testdata/v1", LightScryptN, LightScryptP, true} |
||||
addr := common.HexToAddress("cb61d5a9c4896fb9658090b597ef0e7be6f7b67e") |
||||
file := "testdata/v1/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e/cb61d5a9c4896fb9658090b597ef0e7be6f7b67e" |
||||
k, err := ks.GetKey(addr, file, "g") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
privHex := hex.EncodeToString(crypto.FromECDSA(k.PrivateKey)) |
||||
expectedHex := "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d" |
||||
if privHex != expectedHex { |
||||
t.Fatal(fmt.Errorf("Unexpected privkey: %v, expected %v", privHex, expectedHex)) |
||||
} |
||||
} |
||||
|
||||
func testDecryptV3(test KeyStoreTestV3, t *testing.T) { |
||||
privBytes, _, err := decryptKeyV3(&test.Json, test.Password) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
privHex := hex.EncodeToString(privBytes) |
||||
if test.Priv != privHex { |
||||
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) |
||||
} |
||||
} |
||||
|
||||
func testDecryptV1(test KeyStoreTestV1, t *testing.T) { |
||||
privBytes, _, err := decryptKeyV1(&test.Json, test.Password) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
privHex := hex.EncodeToString(privBytes) |
||||
if test.Priv != privHex { |
||||
t.Fatal(fmt.Errorf("Decrypted bytes not equal to test, expected %v have %v", test.Priv, privHex)) |
||||
} |
||||
} |
||||
|
||||
func loadKeyStoreTestV3(file string, t *testing.T) map[string]KeyStoreTestV3 { |
||||
tests := make(map[string]KeyStoreTestV3) |
||||
err := common.LoadJSON(file, &tests) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
return tests |
||||
} |
||||
|
||||
func loadKeyStoreTestV1(file string, t *testing.T) map[string]KeyStoreTestV1 { |
||||
tests := make(map[string]KeyStoreTestV1) |
||||
err := common.LoadJSON(file, &tests) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
return tests |
||||
} |
||||
|
||||
func TestKeyForDirectICAP(t *testing.T) { |
||||
t.Parallel() |
||||
key := NewKeyForDirectICAP(rand.Reader) |
||||
if !strings.HasPrefix(key.Address.Hex(), "0x00") { |
||||
t.Errorf("Expected first address byte to be zero, have: %s", key.Address.Hex()) |
||||
} |
||||
} |
||||
|
||||
func TestV3_31_Byte_Key(t *testing.T) { |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) |
||||
testDecryptV3(tests["31_byte_key"], t) |
||||
} |
||||
|
||||
func TestV3_30_Byte_Key(t *testing.T) { |
||||
t.Parallel() |
||||
tests := loadKeyStoreTestV3("testdata/v3_test_vector.json", t) |
||||
testDecryptV3(tests["30_byte_key"], t) |
||||
} |
@ -1,150 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"crypto/aes" |
||||
"crypto/cipher" |
||||
"crypto/sha256" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/google/uuid" |
||||
"golang.org/x/crypto/pbkdf2" |
||||
) |
||||
|
||||
// creates a Key and stores that in the given KeyStore by decrypting a presale key JSON
|
||||
func importPreSaleKey(keyStore keyStore, keyJSON []byte, password string) (accounts.Account, *Key, error) { |
||||
key, err := decryptPreSaleKey(keyJSON, password) |
||||
if err != nil { |
||||
return accounts.Account{}, nil, err |
||||
} |
||||
key.Id, err = uuid.NewRandom() |
||||
if err != nil { |
||||
return accounts.Account{}, nil, err |
||||
} |
||||
a := accounts.Account{ |
||||
Address: key.Address, |
||||
URL: accounts.URL{ |
||||
Scheme: KeyStoreScheme, |
||||
Path: keyStore.JoinPath(keyFileName(key.Address)), |
||||
}, |
||||
} |
||||
err = keyStore.StoreKey(a.URL.Path, key, password) |
||||
return a, key, err |
||||
} |
||||
|
||||
func decryptPreSaleKey(fileContent []byte, password string) (key *Key, err error) { |
||||
preSaleKeyStruct := struct { |
||||
EncSeed string |
||||
EthAddr string |
||||
Email string |
||||
BtcAddr string |
||||
}{} |
||||
err = json.Unmarshal(fileContent, &preSaleKeyStruct) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
encSeedBytes, err := hex.DecodeString(preSaleKeyStruct.EncSeed) |
||||
if err != nil { |
||||
return nil, errors.New("invalid hex in encSeed") |
||||
} |
||||
if len(encSeedBytes) < 16 { |
||||
return nil, errors.New("invalid encSeed, too short") |
||||
} |
||||
iv := encSeedBytes[:16] |
||||
cipherText := encSeedBytes[16:] |
||||
/* |
||||
See https://github.com/ethereum/pyethsaletool
|
||||
|
||||
pyethsaletool generates the encryption key from password by |
||||
2000 rounds of PBKDF2 with HMAC-SHA-256 using password as salt (:(). |
||||
16 byte key length within PBKDF2 and resulting key is used as AES key |
||||
*/ |
||||
passBytes := []byte(password) |
||||
derivedKey := pbkdf2.Key(passBytes, passBytes, 2000, 16, sha256.New) |
||||
plainText, err := aesCBCDecrypt(derivedKey, cipherText, iv) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
ethPriv := crypto.Keccak256(plainText) |
||||
ecKey := crypto.ToECDSAUnsafe(ethPriv) |
||||
|
||||
key = &Key{ |
||||
Id: uuid.UUID{}, |
||||
Address: crypto.PubkeyToAddress(ecKey.PublicKey), |
||||
PrivateKey: ecKey, |
||||
} |
||||
derivedAddr := hex.EncodeToString(key.Address.Bytes()) // needed because .Hex() gives leading "0x"
|
||||
expectedAddr := preSaleKeyStruct.EthAddr |
||||
if derivedAddr != expectedAddr { |
||||
err = fmt.Errorf("decrypted addr '%s' not equal to expected addr '%s'", derivedAddr, expectedAddr) |
||||
} |
||||
return key, err |
||||
} |
||||
|
||||
func aesCTRXOR(key, inText, iv []byte) ([]byte, error) { |
||||
// AES-128 is selected due to size of encryptKey.
|
||||
aesBlock, err := aes.NewCipher(key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
stream := cipher.NewCTR(aesBlock, iv) |
||||
outText := make([]byte, len(inText)) |
||||
stream.XORKeyStream(outText, inText) |
||||
return outText, err |
||||
} |
||||
|
||||
func aesCBCDecrypt(key, cipherText, iv []byte) ([]byte, error) { |
||||
aesBlock, err := aes.NewCipher(key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
decrypter := cipher.NewCBCDecrypter(aesBlock, iv) |
||||
paddedPlaintext := make([]byte, len(cipherText)) |
||||
decrypter.CryptBlocks(paddedPlaintext, cipherText) |
||||
plaintext := pkcs7Unpad(paddedPlaintext) |
||||
if plaintext == nil { |
||||
return nil, ErrDecrypt |
||||
} |
||||
return plaintext, err |
||||
} |
||||
|
||||
// From https://leanpub.com/gocrypto/read#leanpub-auto-block-cipher-modes
|
||||
func pkcs7Unpad(in []byte) []byte { |
||||
if len(in) == 0 { |
||||
return nil |
||||
} |
||||
|
||||
padding := in[len(in)-1] |
||||
if int(padding) > len(in) || padding > aes.BlockSize { |
||||
return nil |
||||
} else if padding == 0 { |
||||
return nil |
||||
} |
||||
|
||||
for i := len(in) - 1; i > len(in)-int(padding)-1; i-- { |
||||
if in[i] != padding { |
||||
return nil |
||||
} |
||||
} |
||||
return in[:len(in)-int(padding)] |
||||
} |
@ -1 +0,0 @@ |
||||
{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"7ef5a6135f1fd6a02593eedc869c6d41d934aef8","crypto":{"cipher":"aes-128-ctr","ciphertext":"1d0839166e7a15b9c1333fc865d69858b22df26815ccf601b28219b6192974e1","cipherparams":{"iv":"8df6caa7ff1b00c4e871f002cb7921ed"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"e5e6ef3f4ea695f496b643ebd3f75c0aa58ef4070e90c80c5d3fb0241bf1595c"},"mac":"6d16dfde774845e4585357f24bce530528bc69f4f84e1e22880d34fa45c273e5"},"id":"950077c7-71e3-4c44-a4a1-143919141ed4","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} |
@ -1,21 +0,0 @@ |
||||
This directory contains accounts for testing. |
||||
The password that unlocks them is "foobar". |
||||
|
||||
The "good" key files which are supposed to be loadable are: |
||||
|
||||
- File: UTC--2016-03-22T12-57-55.920751759Z--7ef5a6135f1fd6a02593eedc869c6d41d934aef8 |
||||
Address: 0x7ef5a6135f1fd6a02593eedc869c6d41d934aef8 |
||||
- File: aaa |
||||
Address: 0xf466859ead1932d743d622cb74fc058882e8648a |
||||
- File: zzz |
||||
Address: 0x289d485d9771714cce91d3393d764e1311907acc |
||||
|
||||
The other files (including this README) are broken in various ways |
||||
and should not be picked up by package accounts: |
||||
|
||||
- File: no-address (missing address field, otherwise same as "aaa") |
||||
- File: garbage (file with random data) |
||||
- File: empty (file with no content) |
||||
- File: swapfile~ (should be skipped) |
||||
- File: .hiddenfile (should be skipped) |
||||
- File: foo/... (should be skipped because it is a directory) |
@ -1 +0,0 @@ |
||||
{"address":"7ef5a6135f1fd6a02593eedc869c6d41d934aef8","crypto":{"cipher":"aes-128-ctr","ciphertext":"1d0839166e7a15b9c1333fc865d69858b22df26815ccf601b28219b6192974e1","cipherparams":{"iv":"8df6caa7ff1b00c4e871f002cb7921ed"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"e5e6ef3f4ea695f496b643ebd3f75c0aa58ef4070e90c80c5d3fb0241bf1595c"},"mac":"6d16dfde774845e4585357f24bce530528bc69f4f84e1e22880d34fa45c273e5"},"id":"950077c7-71e3-4c44-a4a1-143919141ed4","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"f466859ead1932d743d622cb74fc058882e8648a","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"fd9bd350f08ee3c0c19b85a8e16114a11a60aa4e","crypto":{"cipher":"aes-128-ctr","ciphertext":"8124d5134aa4a927c79fd852989e4b5419397566f04b0936a1eb1d168c7c68a5","cipherparams":{"iv":"e2febe17176414dd2cda28287947eb2f"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":4096,"p":6,"r":8,"salt":"44b415ede89f3bdd6830390a21b78965f571b347a589d1d943029f016c5e8bd5"},"mac":"5e149ff25bfd9dd45746a84bb2bcd2f015f2cbca2b6d25c5de8c29617f71fe5b"},"id":"d6ac5452-2b2c-4d3c-ad80-4bf0327d971c","version":3} |
Binary file not shown.
@ -1 +0,0 @@ |
||||
{"crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"0000000000000000000000000000000000000000","crypto":{"cipher":"aes-128-ctr","ciphertext":"cb664472deacb41a2e995fa7f96fe29ce744471deb8d146a0e43c7898c9ddd4d","cipherparams":{"iv":"dfd9ee70812add5f4b8f89d0811c9158"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"0d6769bf016d45c479213990d6a08d938469c4adad8a02ce507b4a4e7b7739f1"},"mac":"bac9af994b15a45dd39669fc66f9aa8a3b9dd8c22cb16e4d8d7ea089d0f1a1a9"},"id":"472e8b3d-afb6-45b5-8111-72c89895099a","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"289d485d9771714cce91d3393d764e1311907acc","crypto":{"cipher":"aes-128-ctr","ciphertext":"faf32ca89d286b107f5e6d842802e05263c49b78d46eac74e6109e9a963378ab","cipherparams":{"iv":"558833eec4a665a8c55608d7d503407d"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":8,"p":16,"r":8,"salt":"d571fff447ffb24314f9513f5160246f09997b857ac71348b73e785aab40dc04"},"mac":"21edb85ff7d0dab1767b9bf498f2c3cb7be7609490756bd32300bb213b59effe"},"id":"3279afcf-55ba-43ff-8997-02dcc46a6525","version":3} |
@ -1 +0,0 @@ |
||||
{"address":"cb61d5a9c4896fb9658090b597ef0e7be6f7b67e","Crypto":{"cipher":"aes-128-cbc","ciphertext":"6143d3192db8b66eabd693d9c4e414dcfaee52abda451af79ccf474dafb35f1bfc7ea013aa9d2ee35969a1a2e8d752d0","cipherparams":{"iv":"35337770fc2117994ecdcad026bccff4"},"kdf":"scrypt","kdfparams":{"n":262144,"r":8,"p":1,"dklen":32,"salt":"9afcddebca541253a2f4053391c673ff9fe23097cd8555d149d929e4ccf1257f"},"mac":"3f3d5af884b17a100b0b3232c0636c230a54dc2ac8d986227219b0dd89197644","version":"1"},"id":"e25f7c1f-d318-4f29-b62c-687190d4d299","version":"1"} |
@ -1,28 +0,0 @@ |
||||
{ |
||||
"test1": { |
||||
"json": { |
||||
"Crypto": { |
||||
"cipher": "aes-128-cbc", |
||||
"cipherparams": { |
||||
"iv": "35337770fc2117994ecdcad026bccff4" |
||||
}, |
||||
"ciphertext": "6143d3192db8b66eabd693d9c4e414dcfaee52abda451af79ccf474dafb35f1bfc7ea013aa9d2ee35969a1a2e8d752d0", |
||||
"kdf": "scrypt", |
||||
"kdfparams": { |
||||
"dklen": 32, |
||||
"n": 262144, |
||||
"p": 1, |
||||
"r": 8, |
||||
"salt": "9afcddebca541253a2f4053391c673ff9fe23097cd8555d149d929e4ccf1257f" |
||||
}, |
||||
"mac": "3f3d5af884b17a100b0b3232c0636c230a54dc2ac8d986227219b0dd89197644", |
||||
"version": "1" |
||||
}, |
||||
"address": "cb61d5a9c4896fb9658090b597ef0e7be6f7b67e", |
||||
"id": "e25f7c1f-d318-4f29-b62c-687190d4d299", |
||||
"version": "1" |
||||
}, |
||||
"password": "g", |
||||
"priv": "d1b1178d3529626a1a93e073f65028370d14c7eb0936eb42abef05db6f37ad7d" |
||||
} |
||||
} |
@ -1,97 +0,0 @@ |
||||
{ |
||||
"wikipage_test_vector_scrypt": { |
||||
"json": { |
||||
"crypto" : { |
||||
"cipher" : "aes-128-ctr", |
||||
"cipherparams" : { |
||||
"iv" : "83dbcc02d8ccb40e466191a123791e0e" |
||||
}, |
||||
"ciphertext" : "d172bf743a674da9cdad04534d56926ef8358534d458fffccd4e6ad2fbde479c", |
||||
"kdf" : "scrypt", |
||||
"kdfparams" : { |
||||
"dklen" : 32, |
||||
"n" : 262144, |
||||
"r" : 1, |
||||
"p" : 8, |
||||
"salt" : "ab0c7876052600dd703518d6fc3fe8984592145b591fc8fb5c6d43190334ba19" |
||||
}, |
||||
"mac" : "2103ac29920d71da29f15d75b4a16dbe95cfd7ff8faea1056c33131d846e3097" |
||||
}, |
||||
"id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6", |
||||
"version" : 3 |
||||
}, |
||||
"password": "testpassword", |
||||
"priv": "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d" |
||||
}, |
||||
"wikipage_test_vector_pbkdf2": { |
||||
"json": { |
||||
"crypto" : { |
||||
"cipher" : "aes-128-ctr", |
||||
"cipherparams" : { |
||||
"iv" : "6087dab2f9fdbbfaddc31a909735c1e6" |
||||
}, |
||||
"ciphertext" : "5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46", |
||||
"kdf" : "pbkdf2", |
||||
"kdfparams" : { |
||||
"c" : 262144, |
||||
"dklen" : 32, |
||||
"prf" : "hmac-sha256", |
||||
"salt" : "ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd" |
||||
}, |
||||
"mac" : "517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2" |
||||
}, |
||||
"id" : "3198bc9c-6672-5ab3-d995-4942343ae5b6", |
||||
"version" : 3 |
||||
}, |
||||
"password": "testpassword", |
||||
"priv": "7a28b5ba57c53603b0b07b56bba752f7784bf506fa95edc395f5cf6c7514fe9d" |
||||
}, |
||||
"31_byte_key": { |
||||
"json": { |
||||
"crypto" : { |
||||
"cipher" : "aes-128-ctr", |
||||
"cipherparams" : { |
||||
"iv" : "e0c41130a323adc1446fc82f724bca2f" |
||||
}, |
||||
"ciphertext" : "9517cd5bdbe69076f9bf5057248c6c050141e970efa36ce53692d5d59a3984", |
||||
"kdf" : "scrypt", |
||||
"kdfparams" : { |
||||
"dklen" : 32, |
||||
"n" : 2, |
||||
"r" : 8, |
||||
"p" : 1, |
||||
"salt" : "711f816911c92d649fb4c84b047915679933555030b3552c1212609b38208c63" |
||||
}, |
||||
"mac" : "d5e116151c6aa71470e67a7d42c9620c75c4d23229847dcc127794f0732b0db5" |
||||
}, |
||||
"id" : "fecfc4ce-e956-48fd-953b-30f8b52ed66c", |
||||
"version" : 3 |
||||
}, |
||||
"password": "foo", |
||||
"priv": "fa7b3db73dc7dfdf8c5fbdb796d741e4488628c41fc4febd9160a866ba0f35" |
||||
}, |
||||
"30_byte_key": { |
||||
"json": { |
||||
"crypto" : { |
||||
"cipher" : "aes-128-ctr", |
||||
"cipherparams" : { |
||||
"iv" : "3ca92af36ad7c2cd92454c59cea5ef00" |
||||
}, |
||||
"ciphertext" : "108b7d34f3442fc26ab1ab90ca91476ba6bfa8c00975a49ef9051dc675aa", |
||||
"kdf" : "scrypt", |
||||
"kdfparams" : { |
||||
"dklen" : 32, |
||||
"n" : 2, |
||||
"r" : 8, |
||||
"p" : 1, |
||||
"salt" : "d0769e608fb86cda848065642a9c6fa046845c928175662b8e356c77f914cd3b" |
||||
}, |
||||
"mac" : "75d0e6759f7b3cefa319c3be41680ab6beea7d8328653474bd06706d4cc67420" |
||||
}, |
||||
"id" : "a37e1559-5955-450d-8075-7b8931b392b2", |
||||
"version" : 3 |
||||
}, |
||||
"password": "foo", |
||||
"priv": "81c29e8142bb6a81bef5a92bda7a8328a5c85bb2f9542e76f9b0f94fc018" |
||||
} |
||||
} |
@ -1 +0,0 @@ |
||||
{"address":"45dea0fb0bba44f4fcf290bba71fd57d7117cbb8","crypto":{"cipher":"aes-128-ctr","ciphertext":"b87781948a1befd247bff51ef4063f716cf6c2d3481163e9a8f42e1f9bb74145","cipherparams":{"iv":"dc4926b48a105133d2f16b96833abf1e"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":2,"p":1,"r":8,"salt":"004244bbdc51cadda545b1cfa43cff9ed2ae88e08c61f1479dbb45410722f8f0"},"mac":"39990c1684557447940d4c69e06b1b82b2aceacb43f284df65c956daf3046b85"},"id":"ce541d8d-c79b-40f8-9f8c-20f59616faba","version":3} |
@ -1,150 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum" |
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
) |
||||
|
||||
// keystoreWallet implements the accounts.Wallet interface for the original
|
||||
// keystore.
|
||||
type keystoreWallet struct { |
||||
account accounts.Account // Single account contained in this wallet
|
||||
keystore *KeyStore // Keystore where the account originates from
|
||||
} |
||||
|
||||
// URL implements accounts.Wallet, returning the URL of the account within.
|
||||
func (w *keystoreWallet) URL() accounts.URL { |
||||
return w.account.URL |
||||
} |
||||
|
||||
// Status implements accounts.Wallet, returning whether the account held by the
|
||||
// keystore wallet is unlocked or not.
|
||||
func (w *keystoreWallet) Status() (string, error) { |
||||
w.keystore.mu.RLock() |
||||
defer w.keystore.mu.RUnlock() |
||||
|
||||
if _, ok := w.keystore.unlocked[w.account.Address]; ok { |
||||
return "Unlocked", nil |
||||
} |
||||
return "Locked", nil |
||||
} |
||||
|
||||
// Open implements accounts.Wallet, but is a noop for plain wallets since there
|
||||
// is no connection or decryption step necessary to access the list of accounts.
|
||||
func (w *keystoreWallet) Open(passphrase string) error { return nil } |
||||
|
||||
// Close implements accounts.Wallet, but is a noop for plain wallets since there
|
||||
// is no meaningful open operation.
|
||||
func (w *keystoreWallet) Close() error { return nil } |
||||
|
||||
// Accounts implements accounts.Wallet, returning an account list consisting of
|
||||
// a single account that the plain keystore wallet contains.
|
||||
func (w *keystoreWallet) Accounts() []accounts.Account { |
||||
return []accounts.Account{w.account} |
||||
} |
||||
|
||||
// Contains implements accounts.Wallet, returning whether a particular account is
|
||||
// or is not wrapped by this wallet instance.
|
||||
func (w *keystoreWallet) Contains(account accounts.Account) bool { |
||||
return account.Address == w.account.Address && (account.URL == (accounts.URL{}) || account.URL == w.account.URL) |
||||
} |
||||
|
||||
// Derive implements accounts.Wallet, but is a noop for plain wallets since there
|
||||
// is no notion of hierarchical account derivation for plain keystore accounts.
|
||||
func (w *keystoreWallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { |
||||
return accounts.Account{}, accounts.ErrNotSupported |
||||
} |
||||
|
||||
// SelfDerive implements accounts.Wallet, but is a noop for plain wallets since
|
||||
// there is no notion of hierarchical account derivation for plain keystore accounts.
|
||||
func (w *keystoreWallet) SelfDerive(bases []accounts.DerivationPath, chain ethereum.ChainStateReader) { |
||||
} |
||||
|
||||
// signHash attempts to sign the given hash with
|
||||
// the given account. If the wallet does not wrap this particular account, an
|
||||
// error is returned to avoid account leakage (even though in theory we may be
|
||||
// able to sign via our shared keystore backend).
|
||||
func (w *keystoreWallet) signHash(account accounts.Account, hash []byte) ([]byte, error) { |
||||
// Make sure the requested account is contained within
|
||||
if !w.Contains(account) { |
||||
return nil, accounts.ErrUnknownAccount |
||||
} |
||||
// Account seems valid, request the keystore to sign
|
||||
return w.keystore.SignHash(account, hash) |
||||
} |
||||
|
||||
// SignData signs keccak256(data). The mimetype parameter describes the type of data being signed.
|
||||
func (w *keystoreWallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error) { |
||||
return w.signHash(account, crypto.Keccak256(data)) |
||||
} |
||||
|
||||
// SignDataWithPassphrase signs keccak256(data). The mimetype parameter describes the type of data being signed.
|
||||
func (w *keystoreWallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error) { |
||||
// Make sure the requested account is contained within
|
||||
if !w.Contains(account) { |
||||
return nil, accounts.ErrUnknownAccount |
||||
} |
||||
// Account seems valid, request the keystore to sign
|
||||
return w.keystore.SignHashWithPassphrase(account, passphrase, crypto.Keccak256(data)) |
||||
} |
||||
|
||||
// SignText implements accounts.Wallet, attempting to sign the hash of
|
||||
// the given text with the given account.
|
||||
func (w *keystoreWallet) SignText(account accounts.Account, text []byte) ([]byte, error) { |
||||
return w.signHash(account, accounts.TextHash(text)) |
||||
} |
||||
|
||||
// SignTextWithPassphrase implements accounts.Wallet, attempting to sign the
|
||||
// hash of the given text with the given account using passphrase as extra authentication.
|
||||
func (w *keystoreWallet) SignTextWithPassphrase(account accounts.Account, passphrase string, text []byte) ([]byte, error) { |
||||
// Make sure the requested account is contained within
|
||||
if !w.Contains(account) { |
||||
return nil, accounts.ErrUnknownAccount |
||||
} |
||||
// Account seems valid, request the keystore to sign
|
||||
return w.keystore.SignHashWithPassphrase(account, passphrase, accounts.TextHash(text)) |
||||
} |
||||
|
||||
// SignTx implements accounts.Wallet, attempting to sign the given transaction
|
||||
// with the given account. If the wallet does not wrap this particular account,
|
||||
// an error is returned to avoid account leakage (even though in theory we may
|
||||
// be able to sign via our shared keystore backend).
|
||||
func (w *keystoreWallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
||||
// Make sure the requested account is contained within
|
||||
if !w.Contains(account) { |
||||
return nil, accounts.ErrUnknownAccount |
||||
} |
||||
// Account seems valid, request the keystore to sign
|
||||
return w.keystore.SignTx(account, tx, chainID) |
||||
} |
||||
|
||||
// SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given
|
||||
// transaction with the given account using passphrase as extra authentication.
|
||||
func (w *keystoreWallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) { |
||||
// Make sure the requested account is contained within
|
||||
if !w.Contains(account) { |
||||
return nil, accounts.ErrUnknownAccount |
||||
} |
||||
// Account seems valid, request the keystore to sign
|
||||
return w.keystore.SignTxWithPassphrase(account, passphrase, tx, chainID) |
||||
} |
@ -1,131 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build (darwin && !ios && cgo) || freebsd || (linux && !arm64) || netbsd || solaris
|
||||
// +build darwin,!ios,cgo freebsd linux,!arm64 netbsd solaris
|
||||
|
||||
package keystore |
||||
|
||||
import ( |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/fsnotify/fsnotify" |
||||
) |
||||
|
||||
type watcher struct { |
||||
ac *accountCache |
||||
running bool // set to true when runloop begins
|
||||
runEnded bool // set to true when runloop ends
|
||||
starting bool // set to true prior to runloop starting
|
||||
quit chan struct{} |
||||
} |
||||
|
||||
func newWatcher(ac *accountCache) *watcher { |
||||
return &watcher{ |
||||
ac: ac, |
||||
quit: make(chan struct{}), |
||||
} |
||||
} |
||||
|
||||
// enabled returns false on systems not supported.
|
||||
func (*watcher) enabled() bool { return true } |
||||
|
||||
// starts the watcher loop in the background.
|
||||
// Start a watcher in the background if that's not already in progress.
|
||||
// The caller must hold w.ac.mu.
|
||||
func (w *watcher) start() { |
||||
if w.starting || w.running { |
||||
return |
||||
} |
||||
w.starting = true |
||||
go w.loop() |
||||
} |
||||
|
||||
func (w *watcher) close() { |
||||
close(w.quit) |
||||
} |
||||
|
||||
func (w *watcher) loop() { |
||||
defer func() { |
||||
w.ac.mu.Lock() |
||||
w.running = false |
||||
w.starting = false |
||||
w.runEnded = true |
||||
w.ac.mu.Unlock() |
||||
}() |
||||
logger := log.New("path", w.ac.keydir) |
||||
|
||||
// Create new watcher.
|
||||
watcher, err := fsnotify.NewWatcher() |
||||
if err != nil { |
||||
log.Error("Failed to start filesystem watcher", "err", err) |
||||
return |
||||
} |
||||
defer watcher.Close() |
||||
if err := watcher.Add(w.ac.keydir); err != nil { |
||||
logger.Warn("Failed to watch keystore folder", "err", err) |
||||
return |
||||
} |
||||
|
||||
logger.Trace("Started watching keystore folder", "folder", w.ac.keydir) |
||||
defer logger.Trace("Stopped watching keystore folder") |
||||
|
||||
w.ac.mu.Lock() |
||||
w.running = true |
||||
w.ac.mu.Unlock() |
||||
|
||||
// Wait for file system events and reload.
|
||||
// When an event occurs, the reload call is delayed a bit so that
|
||||
// multiple events arriving quickly only cause a single reload.
|
||||
var ( |
||||
debounceDuration = 500 * time.Millisecond |
||||
rescanTriggered = false |
||||
debounce = time.NewTimer(0) |
||||
) |
||||
// Ignore initial trigger
|
||||
if !debounce.Stop() { |
||||
<-debounce.C |
||||
} |
||||
defer debounce.Stop() |
||||
for { |
||||
select { |
||||
case <-w.quit: |
||||
return |
||||
case _, ok := <-watcher.Events: |
||||
if !ok { |
||||
return |
||||
} |
||||
// Trigger the scan (with delay), if not already triggered
|
||||
if !rescanTriggered { |
||||
debounce.Reset(debounceDuration) |
||||
rescanTriggered = true |
||||
} |
||||
// The fsnotify library does provide more granular event-info, it
|
||||
// would be possible to refresh individual affected files instead
|
||||
// of scheduling a full rescan. For most cases though, the
|
||||
// full rescan is quick and obviously simplest.
|
||||
case err, ok := <-watcher.Errors: |
||||
if !ok { |
||||
return |
||||
} |
||||
log.Info("Filsystem watcher error", "err", err) |
||||
case <-debounce.C: |
||||
w.ac.scanAccounts() |
||||
rescanTriggered = false |
||||
} |
||||
} |
||||
} |
@ -1,35 +0,0 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build (darwin && !cgo) || ios || (linux && arm64) || windows || (!darwin && !freebsd && !linux && !netbsd && !solaris)
|
||||
// +build darwin,!cgo ios linux,arm64 windows !darwin,!freebsd,!linux,!netbsd,!solaris
|
||||
|
||||
// This is the fallback implementation of directory watching.
|
||||
// It is used on unsupported platforms.
|
||||
|
||||
package keystore |
||||
|
||||
type watcher struct { |
||||
running bool |
||||
runEnded bool |
||||
} |
||||
|
||||
func newWatcher(*accountCache) *watcher { return new(watcher) } |
||||
func (*watcher) start() {} |
||||
func (*watcher) close() {} |
||||
|
||||
// enabled returns false on systems not supported.
|
||||
func (*watcher) enabled() bool { return false } |
@ -1,272 +0,0 @@ |
||||
// Copyright 2017 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package accounts |
||||
|
||||
import ( |
||||
"reflect" |
||||
"sort" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
) |
||||
|
||||
// managerSubBufferSize determines how many incoming wallet events
|
||||
// the manager will buffer in its channel.
|
||||
const managerSubBufferSize = 50 |
||||
|
||||
// Config contains the settings of the global account manager.
|
||||
//
|
||||
// TODO(rjl493456442, karalabe, holiman): Get rid of this when account management
|
||||
// is removed in favor of Clef.
|
||||
type Config struct { |
||||
InsecureUnlockAllowed bool // Whether account unlocking in insecure environment is allowed
|
||||
} |
||||
|
||||
// newBackendEvent lets the manager know it should
|
||||
// track the given backend for wallet updates.
|
||||
type newBackendEvent struct { |
||||
backend Backend |
||||
processed chan struct{} // Informs event emitter that backend has been integrated
|
||||
} |
||||
|
||||
// Manager is an overarching account manager that can communicate with various
|
||||
// backends for signing transactions.
|
||||
type Manager struct { |
||||
config *Config // Global account manager configurations
|
||||
backends map[reflect.Type][]Backend // Index of backends currently registered
|
||||
updaters []event.Subscription // Wallet update subscriptions for all backends
|
||||
updates chan WalletEvent // Subscription sink for backend wallet changes
|
||||
newBackends chan newBackendEvent // Incoming backends to be tracked by the manager
|
||||
wallets []Wallet // Cache of all wallets from all registered backends
|
||||
|
||||
feed event.Feed // Wallet feed notifying of arrivals/departures
|
||||
|
||||
quit chan chan error |
||||
term chan struct{} // Channel is closed upon termination of the update loop
|
||||
lock sync.RWMutex |
||||
} |
||||
|
||||
// NewManager creates a generic account manager to sign transaction via various
|
||||
// supported backends.
|
||||
func NewManager(config *Config, backends ...Backend) *Manager { |
||||
// Retrieve the initial list of wallets from the backends and sort by URL
|
||||
var wallets []Wallet |
||||
for _, backend := range backends { |
||||
wallets = merge(wallets, backend.Wallets()...) |
||||
} |
||||
// Subscribe to wallet notifications from all backends
|
||||
updates := make(chan WalletEvent, managerSubBufferSize) |
||||
|
||||
subs := make([]event.Subscription, len(backends)) |
||||
for i, backend := range backends { |
||||
subs[i] = backend.Subscribe(updates) |
||||
} |
||||
// Assemble the account manager and return
|
||||
am := &Manager{ |
||||
config: config, |
||||
backends: make(map[reflect.Type][]Backend), |
||||
updaters: subs, |
||||
updates: updates, |
||||
newBackends: make(chan newBackendEvent), |
||||
wallets: wallets, |
||||
quit: make(chan chan error), |
||||
term: make(chan struct{}), |
||||
} |
||||
for _, backend := range backends { |
||||
kind := reflect.TypeOf(backend) |
||||
am.backends[kind] = append(am.backends[kind], backend) |
||||
} |
||||
go am.update() |
||||
|
||||
return am |
||||
} |
||||
|
||||
// Close terminates the account manager's internal notification processes.
|
||||
func (am *Manager) Close() error { |
||||
errc := make(chan error) |
||||
am.quit <- errc |
||||
return <-errc |
||||
} |
||||
|
||||
// Config returns the configuration of account manager.
|
||||
func (am *Manager) Config() *Config { |
||||
return am.config |
||||
} |
||||
|
||||
// AddBackend starts the tracking of an additional backend for wallet updates.
|
||||
// cmd/geth assumes once this func returns the backends have been already integrated.
|
||||
func (am *Manager) AddBackend(backend Backend) { |
||||
done := make(chan struct{}) |
||||
am.newBackends <- newBackendEvent{backend, done} |
||||
<-done |
||||
} |
||||
|
||||
// update is the wallet event loop listening for notifications from the backends
|
||||
// and updating the cache of wallets.
|
||||
func (am *Manager) update() { |
||||
// Close all subscriptions when the manager terminates
|
||||
defer func() { |
||||
am.lock.Lock() |
||||
for _, sub := range am.updaters { |
||||
sub.Unsubscribe() |
||||
} |
||||
am.updaters = nil |
||||
am.lock.Unlock() |
||||
}() |
||||
|
||||
// Loop until termination
|
||||
for { |
||||
select { |
||||
case event := <-am.updates: |
||||
// Wallet event arrived, update local cache
|
||||
am.lock.Lock() |
||||
switch event.Kind { |
||||
case WalletArrived: |
||||
am.wallets = merge(am.wallets, event.Wallet) |
||||
case WalletDropped: |
||||
am.wallets = drop(am.wallets, event.Wallet) |
||||
} |
||||
am.lock.Unlock() |
||||
|
||||
// Notify any listeners of the event
|
||||
am.feed.Send(event) |
||||
case event := <-am.newBackends: |
||||
am.lock.Lock() |
||||
// Update caches
|
||||
backend := event.backend |
||||
am.wallets = merge(am.wallets, backend.Wallets()...) |
||||
am.updaters = append(am.updaters, backend.Subscribe(am.updates)) |
||||
kind := reflect.TypeOf(backend) |
||||
am.backends[kind] = append(am.backends[kind], backend) |
||||
am.lock.Unlock() |
||||
close(event.processed) |
||||
case errc := <-am.quit: |
||||
// Manager terminating, return
|
||||
errc <- nil |
||||
// Signals event emitters the loop is not receiving values
|
||||
// to prevent them from getting stuck.
|
||||
close(am.term) |
||||
return |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Backends retrieves the backend(s) with the given type from the account manager.
|
||||
func (am *Manager) Backends(kind reflect.Type) []Backend { |
||||
am.lock.RLock() |
||||
defer am.lock.RUnlock() |
||||
|
||||
return am.backends[kind] |
||||
} |
||||
|
||||
// Wallets returns all signer accounts registered under this account manager.
|
||||
func (am *Manager) Wallets() []Wallet { |
||||
am.lock.RLock() |
||||
defer am.lock.RUnlock() |
||||
|
||||
return am.walletsNoLock() |
||||
} |
||||
|
||||
// walletsNoLock returns all registered wallets. Callers must hold am.lock.
|
||||
func (am *Manager) walletsNoLock() []Wallet { |
||||
cpy := make([]Wallet, len(am.wallets)) |
||||
copy(cpy, am.wallets) |
||||
return cpy |
||||
} |
||||
|
||||
// Wallet retrieves the wallet associated with a particular URL.
|
||||
func (am *Manager) Wallet(url string) (Wallet, error) { |
||||
am.lock.RLock() |
||||
defer am.lock.RUnlock() |
||||
|
||||
parsed, err := parseURL(url) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
for _, wallet := range am.walletsNoLock() { |
||||
if wallet.URL() == parsed { |
||||
return wallet, nil |
||||
} |
||||
} |
||||
return nil, ErrUnknownWallet |
||||
} |
||||
|
||||
// Accounts returns all account addresses of all wallets within the account manager
|
||||
func (am *Manager) Accounts() []common.Address { |
||||
am.lock.RLock() |
||||
defer am.lock.RUnlock() |
||||
|
||||
addresses := make([]common.Address, 0) // return [] instead of nil if empty
|
||||
for _, wallet := range am.wallets { |
||||
for _, account := range wallet.Accounts() { |
||||
addresses = append(addresses, account.Address) |
||||
} |
||||
} |
||||
return addresses |
||||
} |
||||
|
||||
// Find attempts to locate the wallet corresponding to a specific account. Since
|
||||
// accounts can be dynamically added to and removed from wallets, this method has
|
||||
// a linear runtime in the number of wallets.
|
||||
func (am *Manager) Find(account Account) (Wallet, error) { |
||||
am.lock.RLock() |
||||
defer am.lock.RUnlock() |
||||
|
||||
for _, wallet := range am.wallets { |
||||
if wallet.Contains(account) { |
||||
return wallet, nil |
||||
} |
||||
} |
||||
return nil, ErrUnknownAccount |
||||
} |
||||
|
||||
// Subscribe creates an async subscription to receive notifications when the
|
||||
// manager detects the arrival or departure of a wallet from any of its backends.
|
||||
func (am *Manager) Subscribe(sink chan<- WalletEvent) event.Subscription { |
||||
return am.feed.Subscribe(sink) |
||||
} |
||||
|
||||
// merge is a sorted analogue of append for wallets, where the ordering of the
|
||||
// origin list is preserved by inserting new wallets at the correct position.
|
||||
//
|
||||
// The original slice is assumed to be already sorted by URL.
|
||||
func merge(slice []Wallet, wallets ...Wallet) []Wallet { |
||||
for _, wallet := range wallets { |
||||
n := sort.Search(len(slice), func(i int) bool { return slice[i].URL().Cmp(wallet.URL()) >= 0 }) |
||||
if n == len(slice) { |
||||
slice = append(slice, wallet) |
||||
continue |
||||
} |
||||
slice = append(slice[:n], append([]Wallet{wallet}, slice[n:]...)...) |
||||
} |
||||
return slice |
||||
} |
||||
|
||||
// drop is the counterpart of merge, which looks up wallets from within the sorted
|
||||
// cache and removes the ones specified.
|
||||
func drop(slice []Wallet, wallets ...Wallet) []Wallet { |
||||
for _, wallet := range wallets { |
||||
n := sort.Search(len(slice), func(i int) bool { return slice[i].URL().Cmp(wallet.URL()) >= 0 }) |
||||
if n == len(slice) { |
||||
// Wallet not found, may happen during startup
|
||||
continue |
||||
} |
||||
slice = append(slice[:n], slice[n+1:]...) |
||||
} |
||||
return slice |
||||
} |
@ -1,106 +0,0 @@ |
||||
# Using the smartcard wallet |
||||
|
||||
## Requirements |
||||
|
||||
* A USB smartcard reader |
||||
* A keycard that supports the status app |
||||
* PCSCD version 4.3 running on your system **Only version 4.3 is currently supported** |
||||
|
||||
## Preparing the smartcard |
||||
|
||||
**WARNING: FOILLOWING THESE INSTRUCTIONS WILL DESTROY THE MASTER KEY ON YOUR CARD. ONLY PROCEED IF NO FUNDS ARE ASSOCIATED WITH THESE ACCOUNTS** |
||||
|
||||
You can use status' [keycard-cli](https://github.com/status-im/keycard-cli) and you should get _at least_ version 2.1.1 of their [smartcard application](https://github.com/status-im/status-keycard/releases/download/2.2.1/keycard_v2.2.1.cap) |
||||
|
||||
You also need to make sure that the PCSC daemon is running on your system. |
||||
|
||||
Then, you can install the application to the card by typing: |
||||
|
||||
``` |
||||
keycard install -a keycard_v2.2.1.cap && keycard init |
||||
``` |
||||
|
||||
At the end of this process, you will be provided with a PIN, a PUK and a pairing password. Write them down, you'll need them shortly. |
||||
|
||||
Start `geth` with the `console` command. You will notice the following warning: |
||||
|
||||
``` |
||||
WARN [04-09|16:58:38.898] Failed to open wallet url=keycard://044def09 err="smartcard: pairing password needed" |
||||
``` |
||||
|
||||
Write down the URL (`keycard://044def09` in this example). Then ask `geth` to open the wallet: |
||||
|
||||
``` |
||||
> personal.openWallet("keycard://044def09", "pairing password") |
||||
``` |
||||
|
||||
The pairing password has been generated during the card initialization process. |
||||
|
||||
The process needs to be repeated once more with the PIN: |
||||
|
||||
``` |
||||
> personal.openWallet("keycard://044def09", "PIN number") |
||||
``` |
||||
|
||||
If everything goes well, you should see your new account when typing `personal` on the console: |
||||
|
||||
``` |
||||
> personal |
||||
WARN [04-09|17:02:07.330] Smartcard wallet account derivation failed url=keycard://044def09 err="Unexpected response status Cla=0x80, Ins=0xd1, Sw=0x6985" |
||||
{ |
||||
listAccounts: [], |
||||
listWallets: [{ |
||||
status: "Empty, waiting for initialization", |
||||
url: "keycard://044def09" |
||||
}], |
||||
... |
||||
} |
||||
``` |
||||
|
||||
So the communication with the card is working, but there is no key associated with this wallet. Let's create it: |
||||
|
||||
``` |
||||
> personal.initializeWallet("keycard://044def09") |
||||
"tilt ... impact" |
||||
``` |
||||
|
||||
You should get a list of words, this is your seed so write them down. Your wallet should now be initialized: |
||||
|
||||
``` |
||||
> personal.listWallets |
||||
[{ |
||||
accounts: [{ |
||||
address: "0x678b7cd55c61917defb23546a41803c5bfefbc7a", |
||||
url: "keycard://044d/m/44'/60'/0'/0/0" |
||||
}], |
||||
status: "Online", |
||||
url: "keycard://044def09" |
||||
}] |
||||
``` |
||||
|
||||
You're all set! |
||||
|
||||
## Usage |
||||
|
||||
1. Start `geth` with the `console` command |
||||
2. Check the card's URL by checking `personal.listWallets`: |
||||
|
||||
``` |
||||
listWallets: [{ |
||||
status: "Online, can derive public keys", |
||||
url: "keycard://a4d73015" |
||||
}] |
||||
``` |
||||
|
||||
3. Open the wallet, you will be prompted for your pairing password, then PIN: |
||||
|
||||
``` |
||||
personal.openWallet("keycard://a4d73015") |
||||
``` |
||||
|
||||
4. Check that creation was successful by typing e.g. `personal`. Then use it like a regular wallet. |
||||
|
||||
## Known issues |
||||
|
||||
* Starting geth with a valid card seems to make firefox crash. |
||||
* PCSC version 4.4 should work, but is currently untested |
@ -1,87 +0,0 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package scwallet |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/binary" |
||||
"fmt" |
||||
) |
||||
|
||||
// commandAPDU represents an application data unit sent to a smartcard.
|
||||
type commandAPDU struct { |
||||
Cla, Ins, P1, P2 uint8 // Class, Instruction, Parameter 1, Parameter 2
|
||||
Data []byte // Command data
|
||||
Le uint8 // Command data length
|
||||
} |
||||
|
||||
// serialize serializes a command APDU.
|
||||
func (ca commandAPDU) serialize() ([]byte, error) { |
||||
buf := new(bytes.Buffer) |
||||
|
||||
if err := binary.Write(buf, binary.BigEndian, ca.Cla); err != nil { |
||||
return nil, err |
||||
} |
||||
if err := binary.Write(buf, binary.BigEndian, ca.Ins); err != nil { |
||||
return nil, err |
||||
} |
||||
if err := binary.Write(buf, binary.BigEndian, ca.P1); err != nil { |
||||
return nil, err |
||||
} |
||||
if err := binary.Write(buf, binary.BigEndian, ca.P2); err != nil { |
||||
return nil, err |
||||
} |
||||
if len(ca.Data) > 0 { |
||||
if err := binary.Write(buf, binary.BigEndian, uint8(len(ca.Data))); err != nil { |
||||
return nil, err |
||||
} |
||||
if err := binary.Write(buf, binary.BigEndian, ca.Data); err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
if err := binary.Write(buf, binary.BigEndian, ca.Le); err != nil { |
||||
return nil, err |
||||
} |
||||
return buf.Bytes(), nil |
||||
} |
||||
|
||||
// responseAPDU represents an application data unit received from a smart card.
|
||||
type responseAPDU struct { |
||||
Data []byte // response data
|
||||
Sw1, Sw2 uint8 // status words 1 and 2
|
||||
} |
||||
|
||||
// deserialize deserializes a response APDU.
|
||||
func (ra *responseAPDU) deserialize(data []byte) error { |
||||
if len(data) < 2 { |
||||
return fmt.Errorf("can not deserialize data: payload too short (%d < 2)", len(data)) |
||||
} |
||||
|
||||
ra.Data = make([]byte, len(data)-2) |
||||
|
||||
buf := bytes.NewReader(data) |
||||
if err := binary.Read(buf, binary.BigEndian, &ra.Data); err != nil { |
||||
return err |
||||
} |
||||
if err := binary.Read(buf, binary.BigEndian, &ra.Sw1); err != nil { |
||||
return err |
||||
} |
||||
if err := binary.Read(buf, binary.BigEndian, &ra.Sw2); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
@ -1,302 +0,0 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
// This package implements support for smartcard-based hardware wallets such as
|
||||
// the one written by Status: https://github.com/status-im/hardware-wallet
|
||||
//
|
||||
// This implementation of smartcard wallets have a different interaction process
|
||||
// to other types of hardware wallet. The process works like this:
|
||||
//
|
||||
// 1. (First use with a given client) Establish a pairing between hardware
|
||||
// wallet and client. This requires a secret value called a 'pairing password'.
|
||||
// You can pair with an unpaired wallet with `personal.openWallet(URI, pairing password)`.
|
||||
// 2. (First use only) Initialize the wallet, which generates a keypair, stores
|
||||
// it on the wallet, and returns it so the user can back it up. You can
|
||||
// initialize a wallet with `personal.initializeWallet(URI)`.
|
||||
// 3. Connect to the wallet using the pairing information established in step 1.
|
||||
// You can connect to a paired wallet with `personal.openWallet(URI, PIN)`.
|
||||
// 4. Interact with the wallet as normal.
|
||||
|
||||
package scwallet |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io" |
||||
"os" |
||||
"path/filepath" |
||||
"sort" |
||||
"sync" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/event" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
pcsc "github.com/gballet/go-libpcsclite" |
||||
) |
||||
|
||||
// Scheme is the URI prefix for smartcard wallets.
|
||||
const Scheme = "keycard" |
||||
|
||||
// refreshCycle is the maximum time between wallet refreshes (if USB hotplug
|
||||
// notifications don't work).
|
||||
const refreshCycle = time.Second |
||||
|
||||
// refreshThrottling is the minimum time between wallet refreshes to avoid thrashing.
|
||||
const refreshThrottling = 500 * time.Millisecond |
||||
|
||||
// smartcardPairing contains information about a smart card we have paired with
|
||||
// or might pair with the hub.
|
||||
type smartcardPairing struct { |
||||
PublicKey []byte `json:"publicKey"` |
||||
PairingIndex uint8 `json:"pairingIndex"` |
||||
PairingKey []byte `json:"pairingKey"` |
||||
Accounts map[common.Address]accounts.DerivationPath `json:"accounts"` |
||||
} |
||||
|
||||
// Hub is a accounts.Backend that can find and handle generic PC/SC hardware wallets.
|
||||
type Hub struct { |
||||
scheme string // Protocol scheme prefixing account and wallet URLs.
|
||||
|
||||
context *pcsc.Client |
||||
datadir string |
||||
pairings map[string]smartcardPairing |
||||
|
||||
refreshed time.Time // Time instance when the list of wallets was last refreshed
|
||||
wallets map[string]*Wallet // Mapping from reader names to wallet instances
|
||||
updateFeed event.Feed // Event feed to notify wallet additions/removals
|
||||
updateScope event.SubscriptionScope // Subscription scope tracking current live listeners
|
||||
updating bool // Whether the event notification loop is running
|
||||
|
||||
quit chan chan error |
||||
|
||||
stateLock sync.RWMutex // Protects the internals of the hub from racey access
|
||||
} |
||||
|
||||
func (hub *Hub) readPairings() error { |
||||
hub.pairings = make(map[string]smartcardPairing) |
||||
pairingFile, err := os.Open(filepath.Join(hub.datadir, "smartcards.json")) |
||||
if err != nil { |
||||
if os.IsNotExist(err) { |
||||
return nil |
||||
} |
||||
return err |
||||
} |
||||
|
||||
pairingData, err := io.ReadAll(pairingFile) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
var pairings []smartcardPairing |
||||
if err := json.Unmarshal(pairingData, &pairings); err != nil { |
||||
return err |
||||
} |
||||
|
||||
for _, pairing := range pairings { |
||||
hub.pairings[string(pairing.PublicKey)] = pairing |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (hub *Hub) writePairings() error { |
||||
pairingFile, err := os.OpenFile(filepath.Join(hub.datadir, "smartcards.json"), os.O_RDWR|os.O_CREATE, 0755) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
defer pairingFile.Close() |
||||
|
||||
pairings := make([]smartcardPairing, 0, len(hub.pairings)) |
||||
for _, pairing := range hub.pairings { |
||||
pairings = append(pairings, pairing) |
||||
} |
||||
|
||||
pairingData, err := json.Marshal(pairings) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if _, err := pairingFile.Write(pairingData); err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (hub *Hub) pairing(wallet *Wallet) *smartcardPairing { |
||||
if pairing, ok := hub.pairings[string(wallet.PublicKey)]; ok { |
||||
return &pairing |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func (hub *Hub) setPairing(wallet *Wallet, pairing *smartcardPairing) error { |
||||
if pairing == nil { |
||||
delete(hub.pairings, string(wallet.PublicKey)) |
||||
} else { |
||||
hub.pairings[string(wallet.PublicKey)] = *pairing |
||||
} |
||||
return hub.writePairings() |
||||
} |
||||
|
||||
// NewHub creates a new hardware wallet manager for smartcards.
|
||||
func NewHub(daemonPath string, scheme string, datadir string) (*Hub, error) { |
||||
context, err := pcsc.EstablishContext(daemonPath, pcsc.ScopeSystem) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
hub := &Hub{ |
||||
scheme: scheme, |
||||
context: context, |
||||
datadir: datadir, |
||||
wallets: make(map[string]*Wallet), |
||||
quit: make(chan chan error), |
||||
} |
||||
if err := hub.readPairings(); err != nil { |
||||
return nil, err |
||||
} |
||||
hub.refreshWallets() |
||||
return hub, nil |
||||
} |
||||
|
||||
// Wallets implements accounts.Backend, returning all the currently tracked smart
|
||||
// cards that appear to be hardware wallets.
|
||||
func (hub *Hub) Wallets() []accounts.Wallet { |
||||
// Make sure the list of wallets is up to date
|
||||
hub.refreshWallets() |
||||
|
||||
hub.stateLock.RLock() |
||||
defer hub.stateLock.RUnlock() |
||||
|
||||
cpy := make([]accounts.Wallet, 0, len(hub.wallets)) |
||||
for _, wallet := range hub.wallets { |
||||
cpy = append(cpy, wallet) |
||||
} |
||||
sort.Sort(accounts.WalletsByURL(cpy)) |
||||
return cpy |
||||
} |
||||
|
||||
// refreshWallets scans the devices attached to the machine and updates the
|
||||
// list of wallets based on the found devices.
|
||||
func (hub *Hub) refreshWallets() { |
||||
// Don't scan the USB like crazy it the user fetches wallets in a loop
|
||||
hub.stateLock.RLock() |
||||
elapsed := time.Since(hub.refreshed) |
||||
hub.stateLock.RUnlock() |
||||
|
||||
if elapsed < refreshThrottling { |
||||
return |
||||
} |
||||
// Retrieve all the smart card reader to check for cards
|
||||
readers, err := hub.context.ListReaders() |
||||
if err != nil { |
||||
// This is a perverted hack, the scard library returns an error if no card
|
||||
// readers are present instead of simply returning an empty list. We don't
|
||||
// want to fill the user's log with errors, so filter those out.
|
||||
if err.Error() != "scard: Cannot find a smart card reader." { |
||||
log.Error("Failed to enumerate smart card readers", "err", err) |
||||
return |
||||
} |
||||
} |
||||
// Transform the current list of wallets into the new one
|
||||
hub.stateLock.Lock() |
||||
|
||||
events := []accounts.WalletEvent{} |
||||
seen := make(map[string]struct{}) |
||||
|
||||
for _, reader := range readers { |
||||
// Mark the reader as present
|
||||
seen[reader] = struct{}{} |
||||
|
||||
// If we already know about this card, skip to the next reader, otherwise clean up
|
||||
if wallet, ok := hub.wallets[reader]; ok { |
||||
if err := wallet.ping(); err == nil { |
||||
continue |
||||
} |
||||
wallet.Close() |
||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped}) |
||||
delete(hub.wallets, reader) |
||||
} |
||||
// New card detected, try to connect to it
|
||||
card, err := hub.context.Connect(reader, pcsc.ShareShared, pcsc.ProtocolAny) |
||||
if err != nil { |
||||
log.Debug("Failed to open smart card", "reader", reader, "err", err) |
||||
continue |
||||
} |
||||
wallet := NewWallet(hub, card) |
||||
if err = wallet.connect(); err != nil { |
||||
log.Debug("Failed to connect to smart card", "reader", reader, "err", err) |
||||
card.Disconnect(pcsc.LeaveCard) |
||||
continue |
||||
} |
||||
// Card connected, start tracking in amongs the wallets
|
||||
hub.wallets[reader] = wallet |
||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived}) |
||||
} |
||||
// Remove any wallets no longer present
|
||||
for reader, wallet := range hub.wallets { |
||||
if _, ok := seen[reader]; !ok { |
||||
wallet.Close() |
||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletDropped}) |
||||
delete(hub.wallets, reader) |
||||
} |
||||
} |
||||
hub.refreshed = time.Now() |
||||
hub.stateLock.Unlock() |
||||
|
||||
for _, event := range events { |
||||
hub.updateFeed.Send(event) |
||||
} |
||||
} |
||||
|
||||
// Subscribe implements accounts.Backend, creating an async subscription to
|
||||
// receive notifications on the addition or removal of smart card wallets.
|
||||
func (hub *Hub) Subscribe(sink chan<- accounts.WalletEvent) event.Subscription { |
||||
// We need the mutex to reliably start/stop the update loop
|
||||
hub.stateLock.Lock() |
||||
defer hub.stateLock.Unlock() |
||||
|
||||
// Subscribe the caller and track the subscriber count
|
||||
sub := hub.updateScope.Track(hub.updateFeed.Subscribe(sink)) |
||||
|
||||
// Subscribers require an active notification loop, start it
|
||||
if !hub.updating { |
||||
hub.updating = true |
||||
go hub.updater() |
||||
} |
||||
return sub |
||||
} |
||||
|
||||
// updater is responsible for maintaining an up-to-date list of wallets managed
|
||||
// by the smart card hub, and for firing wallet addition/removal events.
|
||||
func (hub *Hub) updater() { |
||||
for { |
||||
// TODO: Wait for a USB hotplug event (not supported yet) or a refresh timeout
|
||||
// <-hub.changes
|
||||
time.Sleep(refreshCycle) |
||||
|
||||
// Run the wallet refresher
|
||||
hub.refreshWallets() |
||||
|
||||
// If all our subscribers left, stop the updater
|
||||
hub.stateLock.Lock() |
||||
if hub.updateScope.Count() == 0 { |
||||
hub.updating = false |
||||
hub.stateLock.Unlock() |
||||
return |
||||
} |
||||
hub.stateLock.Unlock() |
||||
} |
||||
} |
@ -1,339 +0,0 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package scwallet |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/aes" |
||||
"crypto/cipher" |
||||
"crypto/elliptic" |
||||
"crypto/rand" |
||||
"crypto/sha256" |
||||
"crypto/sha512" |
||||
"fmt" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
pcsc "github.com/gballet/go-libpcsclite" |
||||
"golang.org/x/crypto/pbkdf2" |
||||
"golang.org/x/text/unicode/norm" |
||||
) |
||||
|
||||
const ( |
||||
maxPayloadSize = 223 |
||||
pairP1FirstStep = 0 |
||||
pairP1LastStep = 1 |
||||
|
||||
scSecretLength = 32 |
||||
scBlockSize = 16 |
||||
|
||||
insOpenSecureChannel = 0x10 |
||||
insMutuallyAuthenticate = 0x11 |
||||
insPair = 0x12 |
||||
insUnpair = 0x13 |
||||
|
||||
pairingSalt = "Keycard Pairing Password Salt" |
||||
) |
||||
|
||||
// SecureChannelSession enables secure communication with a hardware wallet.
|
||||
type SecureChannelSession struct { |
||||
card *pcsc.Card // A handle to the smartcard for communication
|
||||
secret []byte // A shared secret generated from our ECDSA keys
|
||||
publicKey []byte // Our own ephemeral public key
|
||||
PairingKey []byte // A permanent shared secret for a pairing, if present
|
||||
sessionEncKey []byte // The current session encryption key
|
||||
sessionMacKey []byte // The current session MAC key
|
||||
iv []byte // The current IV
|
||||
PairingIndex uint8 // The pairing index
|
||||
} |
||||
|
||||
// NewSecureChannelSession creates a new secure channel for the given card and public key.
|
||||
func NewSecureChannelSession(card *pcsc.Card, keyData []byte) (*SecureChannelSession, error) { |
||||
// Generate an ECDSA keypair for ourselves
|
||||
key, err := crypto.GenerateKey() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
cardPublic, err := crypto.UnmarshalPubkey(keyData) |
||||
if err != nil { |
||||
return nil, fmt.Errorf("could not unmarshal public key from card: %v", err) |
||||
} |
||||
secret, _ := key.Curve.ScalarMult(cardPublic.X, cardPublic.Y, key.D.Bytes()) |
||||
return &SecureChannelSession{ |
||||
card: card, |
||||
secret: secret.Bytes(), |
||||
publicKey: elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y), |
||||
}, nil |
||||
} |
||||
|
||||
// Pair establishes a new pairing with the smartcard.
|
||||
func (s *SecureChannelSession) Pair(pairingPassword []byte) error { |
||||
secretHash := pbkdf2.Key(norm.NFKD.Bytes(pairingPassword), norm.NFKD.Bytes([]byte(pairingSalt)), 50000, 32, sha256.New) |
||||
|
||||
challenge := make([]byte, 32) |
||||
if _, err := rand.Read(challenge); err != nil { |
||||
return err |
||||
} |
||||
|
||||
response, err := s.pair(pairP1FirstStep, challenge) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
md := sha256.New() |
||||
md.Write(secretHash[:]) |
||||
md.Write(challenge) |
||||
|
||||
expectedCryptogram := md.Sum(nil) |
||||
cardCryptogram := response.Data[:32] |
||||
cardChallenge := response.Data[32:64] |
||||
|
||||
if !bytes.Equal(expectedCryptogram, cardCryptogram) { |
||||
return fmt.Errorf("invalid card cryptogram %v != %v", expectedCryptogram, cardCryptogram) |
||||
} |
||||
|
||||
md.Reset() |
||||
md.Write(secretHash[:]) |
||||
md.Write(cardChallenge) |
||||
response, err = s.pair(pairP1LastStep, md.Sum(nil)) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
md.Reset() |
||||
md.Write(secretHash[:]) |
||||
md.Write(response.Data[1:]) |
||||
s.PairingKey = md.Sum(nil) |
||||
s.PairingIndex = response.Data[0] |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// Unpair disestablishes an existing pairing.
|
||||
func (s *SecureChannelSession) Unpair() error { |
||||
if s.PairingKey == nil { |
||||
return fmt.Errorf("cannot unpair: not paired") |
||||
} |
||||
|
||||
_, err := s.transmitEncrypted(claSCWallet, insUnpair, s.PairingIndex, 0, []byte{}) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
s.PairingKey = nil |
||||
// Close channel
|
||||
s.iv = nil |
||||
return nil |
||||
} |
||||
|
||||
// Open initializes the secure channel.
|
||||
func (s *SecureChannelSession) Open() error { |
||||
if s.iv != nil { |
||||
return fmt.Errorf("session already opened") |
||||
} |
||||
|
||||
response, err := s.open() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Generate the encryption/mac key by hashing our shared secret,
|
||||
// pairing key, and the first bytes returned from the Open APDU.
|
||||
md := sha512.New() |
||||
md.Write(s.secret) |
||||
md.Write(s.PairingKey) |
||||
md.Write(response.Data[:scSecretLength]) |
||||
keyData := md.Sum(nil) |
||||
s.sessionEncKey = keyData[:scSecretLength] |
||||
s.sessionMacKey = keyData[scSecretLength : scSecretLength*2] |
||||
|
||||
// The IV is the last bytes returned from the Open APDU.
|
||||
s.iv = response.Data[scSecretLength:] |
||||
|
||||
return s.mutuallyAuthenticate() |
||||
} |
||||
|
||||
// mutuallyAuthenticate is an internal method to authenticate both ends of the
|
||||
// connection.
|
||||
func (s *SecureChannelSession) mutuallyAuthenticate() error { |
||||
data := make([]byte, scSecretLength) |
||||
if _, err := rand.Read(data); err != nil { |
||||
return err |
||||
} |
||||
|
||||
response, err := s.transmitEncrypted(claSCWallet, insMutuallyAuthenticate, 0, 0, data) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if response.Sw1 != 0x90 || response.Sw2 != 0x00 { |
||||
return fmt.Errorf("got unexpected response from MUTUALLY_AUTHENTICATE: %#x%x", response.Sw1, response.Sw2) |
||||
} |
||||
|
||||
if len(response.Data) != scSecretLength { |
||||
return fmt.Errorf("response from MUTUALLY_AUTHENTICATE was %d bytes, expected %d", len(response.Data), scSecretLength) |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// open is an internal method that sends an open APDU.
|
||||
func (s *SecureChannelSession) open() (*responseAPDU, error) { |
||||
return transmit(s.card, &commandAPDU{ |
||||
Cla: claSCWallet, |
||||
Ins: insOpenSecureChannel, |
||||
P1: s.PairingIndex, |
||||
P2: 0, |
||||
Data: s.publicKey, |
||||
Le: 0, |
||||
}) |
||||
} |
||||
|
||||
// pair is an internal method that sends a pair APDU.
|
||||
func (s *SecureChannelSession) pair(p1 uint8, data []byte) (*responseAPDU, error) { |
||||
return transmit(s.card, &commandAPDU{ |
||||
Cla: claSCWallet, |
||||
Ins: insPair, |
||||
P1: p1, |
||||
P2: 0, |
||||
Data: data, |
||||
Le: 0, |
||||
}) |
||||
} |
||||
|
||||
// transmitEncrypted sends an encrypted message, and decrypts and returns the response.
|
||||
func (s *SecureChannelSession) transmitEncrypted(cla, ins, p1, p2 byte, data []byte) (*responseAPDU, error) { |
||||
if s.iv == nil { |
||||
return nil, fmt.Errorf("channel not open") |
||||
} |
||||
|
||||
data, err := s.encryptAPDU(data) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
meta := [16]byte{cla, ins, p1, p2, byte(len(data) + scBlockSize)} |
||||
if err = s.updateIV(meta[:], data); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
fulldata := make([]byte, len(s.iv)+len(data)) |
||||
copy(fulldata, s.iv) |
||||
copy(fulldata[len(s.iv):], data) |
||||
|
||||
response, err := transmit(s.card, &commandAPDU{ |
||||
Cla: cla, |
||||
Ins: ins, |
||||
P1: p1, |
||||
P2: p2, |
||||
Data: fulldata, |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
rmeta := [16]byte{byte(len(response.Data))} |
||||
rmac := response.Data[:len(s.iv)] |
||||
rdata := response.Data[len(s.iv):] |
||||
plainData, err := s.decryptAPDU(rdata) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if err = s.updateIV(rmeta[:], rdata); err != nil { |
||||
return nil, err |
||||
} |
||||
if !bytes.Equal(s.iv, rmac) { |
||||
return nil, fmt.Errorf("invalid MAC in response") |
||||
} |
||||
|
||||
rapdu := &responseAPDU{} |
||||
rapdu.deserialize(plainData) |
||||
|
||||
if rapdu.Sw1 != sw1Ok { |
||||
return nil, fmt.Errorf("unexpected response status Cla=%#x, Ins=%#x, Sw=%#x%x", cla, ins, rapdu.Sw1, rapdu.Sw2) |
||||
} |
||||
|
||||
return rapdu, nil |
||||
} |
||||
|
||||
// encryptAPDU is an internal method that serializes and encrypts an APDU.
|
||||
func (s *SecureChannelSession) encryptAPDU(data []byte) ([]byte, error) { |
||||
if len(data) > maxPayloadSize { |
||||
return nil, fmt.Errorf("payload of %d bytes exceeds maximum of %d", len(data), maxPayloadSize) |
||||
} |
||||
data = pad(data, 0x80) |
||||
|
||||
ret := make([]byte, len(data)) |
||||
|
||||
a, err := aes.NewCipher(s.sessionEncKey) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
crypter := cipher.NewCBCEncrypter(a, s.iv) |
||||
crypter.CryptBlocks(ret, data) |
||||
return ret, nil |
||||
} |
||||
|
||||
// pad applies message padding to a 16 byte boundary.
|
||||
func pad(data []byte, terminator byte) []byte { |
||||
padded := make([]byte, (len(data)/16+1)*16) |
||||
copy(padded, data) |
||||
padded[len(data)] = terminator |
||||
return padded |
||||
} |
||||
|
||||
// decryptAPDU is an internal method that decrypts and deserializes an APDU.
|
||||
func (s *SecureChannelSession) decryptAPDU(data []byte) ([]byte, error) { |
||||
a, err := aes.NewCipher(s.sessionEncKey) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
ret := make([]byte, len(data)) |
||||
|
||||
crypter := cipher.NewCBCDecrypter(a, s.iv) |
||||
crypter.CryptBlocks(ret, data) |
||||
return unpad(ret, 0x80) |
||||
} |
||||
|
||||
// unpad strips padding from a message.
|
||||
func unpad(data []byte, terminator byte) ([]byte, error) { |
||||
for i := 1; i <= 16; i++ { |
||||
switch data[len(data)-i] { |
||||
case 0: |
||||
continue |
||||
case terminator: |
||||
return data[:len(data)-i], nil |
||||
default: |
||||
return nil, fmt.Errorf("expected end of padding, got %d", data[len(data)-i]) |
||||
} |
||||
} |
||||
return nil, fmt.Errorf("expected end of padding, got 0") |
||||
} |
||||
|
||||
// updateIV is an internal method that updates the initialization vector after
|
||||
// each message exchanged.
|
||||
func (s *SecureChannelSession) updateIV(meta, data []byte) error { |
||||
data = pad(data, 0) |
||||
a, err := aes.NewCipher(s.sessionMacKey) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
crypter := cipher.NewCBCEncrypter(a, make([]byte, 16)) |
||||
crypter.CryptBlocks(meta, meta) |
||||
crypter.CryptBlocks(data, data) |
||||
// The first 16 bytes of the last block is the MAC
|
||||
s.iv = data[len(data)-32 : len(data)-16] |
||||
return nil |
||||
} |
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue