Skip to content

Commit

Permalink
Merge pull request #316 from argentlabs/develop
Browse files Browse the repository at this point in the history
Sync to master (1.5.0)
  • Loading branch information
DarthMike authored Feb 16, 2023
2 parents 3c4d4a5 + 1ad8cc4 commit 2bbb2f2
Show file tree
Hide file tree
Showing 86 changed files with 2,472 additions and 1,527 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
name: CI

on:
pull_request:
pull_request_target:
branches: [ develop ]
workflow_dispatch:

jobs:
lint:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Lint
run: ./scripts/runSwiftFormat.sh -l
macos:
needs: lint
runs-on: macos-latest
env:
TESTS_PRIVATEKEY: ${{ secrets.TESTS_PRIVATEKEY }}
steps:
- uses: actions/checkout@v3
- name: SetupKey
run: ./scripts/setupKey.sh "${{ secrets.TESTS_PRIVATEKEY }}"
- name: Build
run: swift build -v
- name: Tests
run: swift test -v
linux:
needs: lint
runs-on: ubuntu-latest
container:
image: swift:5.5-bionic
env:
TESTS_PRIVATEKEY: ${{ secrets.TESTS_PRIVATEKEY }}
steps:
- uses: actions/checkout@v3
- name: SetupKey
run: ./scripts/setupKey.sh "${{ secrets.TESTS_PRIVATEKEY }}"
- name: Build
run: swift build -v
- name: Tests
run: swift test -v
run: swift test -v
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output

scripts/bin/*
TestConfig_private.swift
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GEM
specs:
CFPropertyList (3.0.5)
rexml
activesupport (6.1.5)
activesupport (6.1.7.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -54,7 +54,7 @@ GEM
netrc (~> 0.11)
cocoapods-try (1.2.0)
colored2 (3.1.2)
concurrent-ruby (1.1.10)
concurrent-ruby (1.2.0)
escape (0.0.4)
ethon (0.15.0)
ffi (>= 1.15.0)
Expand All @@ -63,10 +63,10 @@ GEM
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
httpclient (2.8.3)
i18n (1.10.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
json (2.6.1)
minitest (5.15.0)
minitest (5.17.0)
molinillo (0.8.0)
nanaimo (0.3.0)
nap (1.1.0)
Expand All @@ -76,7 +76,7 @@ GEM
ruby-macho (2.5.1)
typhoeus (1.4.0)
ethon (>= 0.9.0)
tzinfo (2.0.4)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
xcodeproj (1.21.0)
CFPropertyList (>= 2.3.3, < 4.0)
Expand All @@ -85,7 +85,7 @@ GEM
colored2 (~> 3.1)
nanaimo (~> 0.3.0)
rexml (~> 3.2.4)
zeitwerk (2.5.4)
zeitwerk (2.6.6)

PLATFORMS
ruby
Expand Down
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Create an instance of `EthereumHttpClient` or `EthereumWebSocketClient`. This wi
`EthereumHttpClient`
```swift
guard let clientUrl = URL(string: "https://an-infura-or-similar-url.com/123") else { return }
let client = EthereumClient(url: clientUrl)
let client = EthereumHttpClient(url: clientUrl)
```

OR
Expand Down Expand Up @@ -117,13 +117,18 @@ If using `async/await` you can `await` on the result
let txHash = try await client.eth_sendRawTransaction(transaction, withAccount: account)
```

## Generating ABI from a smart contract ABI file
Currently we don't support code generation as making it properly is a bigger project, and should possibly live outside of this repository.

You can try this project instead: [imanrep/swiftabigen](https://github.com/imanrep/swiftabigen)

### Data types

The library provides some types and helpers to make interacting with web3 and Ethereum easier.

- `EthereumAddress`: For representation of addresses, including checksum support.
- `BigInt` and `BigUInt`: Using [BigInt](https://github.com/attaswift/BigInt) library
- `EthereumBlock`: Represents the block, either number of RPC-specific defintions like 'Earliest' or 'Latest'
- `EthereumBlock`: Represents the block, either number of RPC-specific definitions like 'Earliest' or 'Latest'
- `EthereumTransaction`: Wraps a transaction. Encoders and decoders can work with it to generate proper `data` fields.

#### Conversion from and to Foundation types
Expand Down Expand Up @@ -159,7 +164,9 @@ We support querying ERC721 token data via the `ERC721` struct. Including:

### Running Tests

The tests will all pass when running against Goerli. You will need to provide a URL for the blockchain proxy (e.g. on Infura), and a key-pair in `TestConfig.swift`. Some of the account signing tests will fail, given the signature assertions are against a specific (unprovided) key.
Some of the tests require a private key, which is not stored in the repository. You can ignore these while testing locally, as CI will use the encrypted secret key from Github.

It's better to run only the tests you need, instead of the whole test suite while developing. If you ever need to set up the key locally, take a look at `TestConfig.swift` where you can manually set it up. Alternatively you can set it up by calling the script `setupKey.sh` and passing the value (adding 0x) so it's written to an ignored file.

## Dependencies

Expand All @@ -169,17 +176,14 @@ We built web3.swift to be as lightweight as possible. However, given the cryptog
- [Tiny AES](https://github.com/kokke/tiny-AES-c): A small and portable implementation of the AES ECB, CTR and CBC encryption algorithms.
- [secp256k1.swift](https://github.com/Boilertalk/secp256k1.swift)

We also use Apple's own CommonCrypto (via [this](https://github.com/sergejp/CommonCrypto) method) and [BigInt](https://github.com/attaswift/BigInt) via CocoaPod dependency.

## Todos

There are some features that have yet to be fully implemented! Not every RPC method is currently supported, and here's some other suggestions we would like to see in the future:

Package dependencies:
- [BigInt](https://github.com/attaswift/BigInt)
- [GenericJSON](https://github.com/iwill/generic-json-swift)
- [secp256k1](https://github.com/GigaBitcoin/secp256k1.swift.git)
- [Vapor Websocket](https://github.com/vapor/websocket-kit.git)
- [Apple Swift-log](https://github.com/apple/swift-log.git)

- Batch support for JSONRPC interface
- Use a Hex struct for values to be more explicit in expected types
- Use [Truffle](https://github.com/trufflesuite/ganache-cli) for running tests
- Bloom Filter support
Also for Linux build, we can't se Apple crypto APIs, so we embedded a small subset of CryptoSwift (instead of importing the whole library). Credit to [Marcin Krzyżanowski](https://github.com/krzyzanowskim/CryptoSwift)

## Contributors

Expand Down
7 changes: 7 additions & 0 deletions scripts/prepareForPush.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

SWIFT_VERSION=5.3

cd "$(dirname "$0")"

./runSwiftFormat.sh
63 changes: 63 additions & 0 deletions scripts/runSwiftFormat.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/sh

SWIFT_VERSION=5.3

cd "$(dirname "$0")"

function downloadAndUnzip {
curl -L -o tool.zip $2
unzip -o -d $1/ tool.zip
rm tool.zip
}

if ! which bin/swiftformat >/dev/null; then
echo "warning: SwiftFormat not installed, installing..."

mkdir -p -- "bin"
cd bin
rm -r ./*

downloadAndUnzip "SwiftFormatTmp" "https://github.com/nicklockwood/SwiftFormat/releases/download/0.50.3/swiftformat.artifactbundle.zip"
mv -f ./SwiftFormatTmp/swiftformat.artifactbundle/swiftformat-0.50.3-macos/bin/swiftformat .
find . -name "*Tmp" -type d -prune -exec rm -rf '{}' +
for entry in ./*
do
chmod +x "$entry"
done
cd ../
fi

cleanup() {
exit_code=$?
if [[ ${exit_code} -eq 0 ]]; then
exit 0
else
echo "Need to run scripts/prepareForPush.sh script to prepare the code before a PullRequest."
exit 1
fi
}

format() {
bin/swiftformat ../web3swift/src/ --config "swiftformat.yml" --swiftversion $SWIFT_VERSION
cleanup
}

lint() {
bin/swiftformat --lint ../web3swift/src/ --config "swiftformat.yml" --swiftversion $SWIFT_VERSION
cleanup
}

while getopts "fl" o; do
case "${o}" in
f)
format;
exit;;
l)
lint;
exit;;
*)
exit;;
esac
done

format
4 changes: 4 additions & 0 deletions scripts/setupKey.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

# write first argument to a Test
echo "extension TestConfig {\nstatic let privateKey = \"$1\"\nstatic let publicKey = \"0xE78e5ecb061fE3DD1672dDDA7b5116213B23B99A\"\n}" > web3sTests/TestConfig_private.swift
79 changes: 79 additions & 0 deletions scripts/swiftformat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--acronyms ID,URL,UUID
--allman false
--assetliterals visual-width
--beforemarks
--binarygrouping 4,8
--categorymark "MARK: %c"
--classthreshold 0
--closingparen balanced
--closurevoid remove
--commas always
--conflictmarkers reject
--decimalgrouping 3,6
--elseposition same-line
--emptybraces no-space
--enumnamespaces always
--enumthreshold 0
--exponentcase lowercase
--exponentgrouping disabled
--extensionacl on-extension
--extensionlength 0
--extensionmark "MARK: - %t + %c"
--fractiongrouping disabled
--fragment false
--funcattributes prev-line
--generictypes
--groupedextension "MARK: %c"
--guardelse same-line
--header ignore
--hexgrouping 4,8
--hexliteralcase uppercase
--ifdef indent
--importgrouping length
--indent 4
--indentcase false
--indentstrings false
--lifecycle
--lineaftermarks false
--linebreaks lf
--markcategories true
--markextensions always
--marktypes always
--maxwidth none
--modifierorder
--nevertrailing
--nospaceoperators
--nowrapoperators
--octalgrouping 4,8
--operatorfunc spaced
--organizetypes actor,class,enum,struct
--patternlet hoist
--ranges spaced
--redundanttype infer-locals-only
--self remove
--selfrequired
--semicolons inline
--shortoptionals always
--smarttabs enabled
--someAny true
--stripunusedargs always
--structthreshold 0
--tabwidth unspecified
--trailingclosures
--trimwhitespace always
--typeattributes prev-line
--typeblanklines remove
--typemark "MARK: - %t"
--varattributes same-line
--voidtype void
--wraparguments before-first
--wrapcollections before-first
--wrapconditions preserve
--wrapparameters before-first
--wrapreturntype preserve
--wrapternary default
--wraptypealiases preserve
--xcodeindentation disabled
--yodaswap always
--disable enumNamespaces,extensionAccessControl,fileHeader,genericExtensions,modifierOrder,numberFormatting,opaqueGenericParameters,preferKeyPath,redundantBackticks,redundantExtensionACL,redundantFileprivate,redundantPattern,redundantRawValues,redundantSelf,sortDeclarations,spaceAroundGenerics,strongOutlets,trailingClosures,trailingCommas,unusedArguments,wrap,wrapMultilineStatementBraces,wrapSingleLineComments,yodaConditions
--enable blankLineAfterImports,blankLinesBetweenImports,isEmpty,wrapConditionalBodies
2 changes: 1 addition & 1 deletion web3.swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'web3.swift'
s.version = '1.4.1'
s.version = '1.5.0'
s.license = 'MIT'
s.summary = 'Ethereum API for Swift'
s.homepage = 'https://github.com/argentlabs/web3.swift'
Expand Down
Loading

0 comments on commit 2bbb2f2

Please sign in to comment.