- Notifications
You must be signed in to change notification settings - Fork 483
Fix Base58 Decoding #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yaroslavyaroslav merged 5 commits into web3swift-team:develop from mloit:feature/fix-base58-decode Mar 31, 2022
Merged
Fix Base58 Decoding #504
Changes from all commits
Commits
Show all changes
5 commits Select commit Hold shift + click to select a range
d24e487 Fix Base58 decoding, fixes #424
mloit c2f3334 reworded comment a bit to try and make it less confusing
mloit 18c059b Add tests for base58 encode and decode
mloit bd1afcd Merge branch 'skywinder:develop' into feature/fix-base58-decode
mloit 5def075 Merge branch 'develop' into feature/fix-base58-decode
mloit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions 102 Tests/web3swiftTests/localTests/DataConversionTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Package: web3swift | ||
| // Created by Alex Vlasov. | ||
| // Copyright © 2018 Alex Vlasov. All rights reserved. | ||
| // | ||
| // Base58 tests by Mark Loit 2022 | ||
| // | ||
| | ||
| import XCTest | ||
| | ||
| @testable import web3swift | ||
| | ||
| // | ||
| // This Test suite is intended to hold various tests for our data conversion code | ||
| // that don't seem to fit elsewhere | ||
| // | ||
| | ||
| // Some base58 test vectors pulled from: https://tools.ietf.org/id/draft-msporny-base58-01.html | ||
| // note that one of the return values is incorrect in the reference above, it is corrected here | ||
| class DataConversionTests: XCTestCase { | ||
| // test an empty input for the base58 decoder & decoder | ||
| func testBase58() throws { | ||
| let vector = "" | ||
| | ||
| print("Testing Base58 Decode \"\(vector)\"") | ||
| guard let resultDecoded = vector.base58DecodedData else { return XCTFail("base58 decode unexpectedly returned nil") } | ||
| XCTAssert(resultDecoded.count == 0) | ||
| | ||
| print("Testing Base58 Encode \"\(vector)\"") | ||
| let resultEncoded1 = vector.base58EncodedString | ||
| XCTAssert(resultEncoded1 == vector) | ||
| | ||
| print("Testing Base58 Encode [empty]") | ||
| let arr = resultDecoded.withUnsafeBytes { Array($0) } | ||
| let resultEncoded2 = arr.base58EncodedString | ||
| XCTAssert(resultEncoded2 == vector) | ||
| } | ||
| | ||
| // test a reference string "Hello World!" | ||
| func testBase58HelloWorld() throws { | ||
| let vector = "2NEpo7TZRRrLZSi2U" | ||
| let expected = "Hello World!" | ||
| | ||
| print("Testing Base58 Decode \"\(vector)\"") | ||
| guard let resultDecoded = vector.base58DecodedData else { return XCTFail("base58 decode unexpectedly returned nil") } | ||
| let arr = resultDecoded.withUnsafeBytes { Array($0) } | ||
| let str = String(bytes: arr, encoding: .utf8) | ||
| XCTAssert(str == expected) | ||
| | ||
| print("Testing Base58 Encode \"\(expected)\"") | ||
| let resultEncoded = expected.base58EncodedString | ||
| XCTAssert(resultEncoded == vector) | ||
| } | ||
| | ||
| // test a reference string "The quick brown fox jumps over the lazy dog." | ||
| func testBase58LazyFox() throws { | ||
| let vector = "USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z" | ||
| let expected = "The quick brown fox jumps over the lazy dog." | ||
| | ||
| print("Testing Base58 Decode \"\( vector )\"") | ||
| guard let resultDecoded = vector.base58DecodedData else { return XCTFail("base58 decode unexpectedly returned nil") } | ||
| let arr = resultDecoded.withUnsafeBytes { Array($0) } | ||
| let str = String(bytes: arr, encoding: .utf8) | ||
| XCTAssert(str == expected) | ||
| | ||
| print("Testing Base58 Encode \"\(expected)\"") | ||
| let resultEncoded = expected.base58EncodedString | ||
| XCTAssert(resultEncoded == vector) | ||
| } | ||
| | ||
| // test a reference binary value "0x000000287fb4cd" (tested as a hex string to validate length) **corrected from ref document | ||
| func testBase58HexData() throws { | ||
| let vector = "111233QC4" | ||
| let expected = "0x000000287fb4cd" | ||
| | ||
| print("Testing Base58 Decode \"\(vector)\"") | ||
| guard let resultDecoded = vector.base58DecodedData else { return XCTFail("base58 decode unexpectedly returned nil") } | ||
| let str = resultDecoded.toHexString().addHexPrefix() | ||
| XCTAssert(str == expected) | ||
| | ||
| print("Testing Base58 Encode \(expected)") | ||
| let arr = resultDecoded.withUnsafeBytes { Array($0) } | ||
| let resultEncoded = arr.base58EncodedString | ||
| XCTAssert(resultEncoded == vector) | ||
| } | ||
| | ||
| // test all zero encoded data from issue 424 | ||
| func testBase58Zero() throws { | ||
| let vector = "11111111111111111111111111111111" | ||
| let expected = "0x0000000000000000000000000000000000000000000000000000000000000000" | ||
| | ||
| print("Testing Base58 Decode \"\(vector)\"") | ||
| guard let resultDecoded = vector.base58DecodedData else { return XCTFail("base58 decode unexpectedly returned nil") } | ||
| let str = resultDecoded.toHexString().addHexPrefix() | ||
| XCTAssert(str == expected) | ||
| | ||
| print("Testing Base58 Encode \(expected)") | ||
| let arr = resultDecoded.withUnsafeBytes { Array($0) } | ||
| let resultEncoded = arr.base58EncodedString | ||
| XCTAssert(resultEncoded == vector) | ||
| } | ||
| | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.