-
Notifications
You must be signed in to change notification settings - Fork 56
fix(swift-sdk): stop applying the account derivation path twice #4334
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
Closed
Closed
Changes from all commits
Commits
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
90 changes: 90 additions & 0 deletions
90
packages/swift-sdk/SwiftTests/SwiftDashSDKTests/AccountDerivationPathTests.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,90 @@ | ||
| import XCTest | ||
|
|
||
| @testable import SwiftDashSDK | ||
|
|
||
| /// `Account.derivePrivateKeyWIF(wallet:index:)` must apply the account's | ||
| /// derivation path exactly once. | ||
| /// | ||
| /// The FFI resolves `Account::derivation_path()` itself — the full path from | ||
| /// the wallet master — so the wrapper has to hand it the master. It used to | ||
| /// take a `masterPath`, pre-derive that, and pass the result, which applied the | ||
| /// account path twice: provider voting keys came back from | ||
| /// `m/9'/5'/3'/1'/9'/5'/3'/1'/index` instead of `m/9'/5'/3'/1'/index`. | ||
| /// | ||
| /// Nothing failed locally when that happened. The key was well-formed and | ||
| /// derived deterministically, so it round-tripped through WIF parsing and | ||
| /// signing without complaint — it simply wasn't the account's key at that | ||
| /// index. Only a counterparty holding the real key could tell: a masternode | ||
| /// vote signed with it is rejected as having no voter identity, because the | ||
| /// voter identity is derived from the signing key's own hash160. | ||
| /// | ||
| /// So these pin account-based derivation against the explicit DIP-3 path, | ||
| /// which is the thing the doubling silently broke. | ||
| final class AccountDerivationPathTests: XCTestCase { | ||
| /// Standard BIP39 test vector, matching the other wallet tests here. | ||
| private let mnemonic = | ||
| "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" | ||
|
|
||
| private func assertAccountDerivationMatchesPath( | ||
| accountType: AccountType, | ||
| pathPrefix: String, | ||
| network: Network, | ||
| file: StaticString = #filePath, | ||
| line: UInt = #line | ||
| ) throws { | ||
| let wallet = try Wallet(mnemonic: mnemonic, network: network) | ||
| let account = try wallet.getAccount(type: accountType) | ||
|
|
||
| // Indexes beyond 0 matter: the doubled path agreed with nothing, but a | ||
| // single wrong level could still coincide at index 0 in principle. | ||
| for index in [UInt32(0), 1, 19] { | ||
| let viaAccount = try account.derivePrivateKeyWIF(wallet: wallet, index: index) | ||
| let viaPath = try wallet.derivePrivateKey(path: "\(pathPrefix)/\(index)") | ||
| XCTAssertEqual( | ||
| viaAccount, viaPath, | ||
| """ | ||
| account-based derivation at index \(index) disagrees with \ | ||
| \(pathPrefix)/\(index) — the account path is being applied the \ | ||
| wrong number of times | ||
| """, | ||
| file: file, line: line) | ||
| } | ||
| } | ||
|
|
||
| func testProviderVotingKeysDeriveAtTheDIP3Path() throws { | ||
| try assertAccountDerivationMatchesPath( | ||
| accountType: .providerVotingKeys, | ||
| pathPrefix: "m/9'/5'/3'/1'", | ||
| network: .mainnet) | ||
| } | ||
|
|
||
| func testProviderOwnerKeysDeriveAtTheDIP3Path() throws { | ||
| try assertAccountDerivationMatchesPath( | ||
| accountType: .providerOwnerKeys, | ||
| pathPrefix: "m/9'/5'/3'/2'", | ||
| network: .mainnet) | ||
| } | ||
|
|
||
| /// The account resolves its own path, including coin type, so testnet must | ||
| /// land on `1'` without the caller saying so. | ||
| func testProviderVotingKeysUseTheTestnetCoinType() throws { | ||
| try assertAccountDerivationMatchesPath( | ||
| accountType: .providerVotingKeys, | ||
| pathPrefix: "m/9'/1'/3'/1'", | ||
| network: .testnet) | ||
| } | ||
|
|
||
| /// The specific regression: deriving against the account root rather than | ||
| /// the master must NOT reproduce account-based derivation. If these ever | ||
| /// match, the wrapper has gone back to pre-deriving the account path. | ||
| func testDoubledAccountPathIsNotWhatWeDerive() throws { | ||
| let wallet = try Wallet(mnemonic: mnemonic, network: .mainnet) | ||
| let account = try wallet.getAccount(type: .providerVotingKeys) | ||
|
|
||
| let correct = try account.derivePrivateKeyWIF(wallet: wallet, index: 19) | ||
| let doubled = try wallet.derivePrivateKey(path: "m/9'/5'/3'/1'/9'/5'/3'/1'/19") | ||
| XCTAssertNotEqual( | ||
| correct, doubled, | ||
| "account-based derivation is applying the account path twice again") | ||
| } | ||
| } |
Oops, something went wrong.
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.