-
Notifications
You must be signed in to change notification settings - Fork 67
Try to fix PublicKey.compressed, add more tests #57
Conversation
Codecov Report
@@ Coverage Diff @@
## master #57 +/- ##
==========================================
+ Coverage 81.2% 81.27% +0.06%
==========================================
Files 74 74
Lines 2943 2953 +10
==========================================
+ Hits 2390 2400 +10
Misses 553 553
Continue to review full report at Codecov.
|
XCTAssertEqual(uncompressed2.compressed.legacyBitcoinAddress(prefix: 0).description, "1J7mdg5rbQyUHENYdx39WVWK7fsLpEoXZy") | ||
|
||
let uncompressed3 = PublicKey(data: Data(hexString: "042de45bea3dada528eee8a1e04142d3e04fad66119d971b6019b0e3c02266b79142158aa83469db1332a880a2d5f8ce0b3bba542b3e32df0740ccbfb01c275e42")!)! | ||
XCTAssertTrue(!uncompressed3.isCompressed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use XCTAssertFalse
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -38,7 +38,7 @@ public struct PublicKey: Hashable, CustomStringConvertible { | |||
if isCompressed { | |||
return self | |||
} | |||
let prefix: UInt8 = 0x02 | (data[1] & 0x01) | |||
let prefix: UInt8 = 0x02 | (data[64] & 0x01) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from:
https://bitcoin.stackexchange.com/questions/76181/bitcoin-public-keys-from-uncompressed-to-compressed-version-with-code-sample/76183#76183
Compression is very simply, if the Y value of the pubkey is odd, place an 0x03 byte. If it is even, place a 0x02 byte
And we been using 0x02 and 0x01, can you explain this part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&
|
are bitwise operators, here is for fast computingdata[64] & 0x01
tests if y is even or odd (you convert it to binary and do bitwise AND)0x02 |
does bitwise OR 0 / 1 (y) and results 0x02 | 0x03.
Tests/HDWalletTests.swift
Outdated
@@ -33,7 +33,8 @@ class HDWalletTests: XCTestCase { | |||
func testDeriveBitcoin() { | |||
let wallet = HDWallet(mnemonic: words, passphrase: passphrase) | |||
let key = wallet.getKey(at: Bitcoin().derivationPath(at: 0)) | |||
let publicKey = key.publicKey(compressed: true) | |||
let publicKey = key.publicKey().compressed | |||
XCTAssertEqual(publicKey.description, key.publicKey(compressed: true).description) | |||
|
|||
XCTAssertEqual("026fc80db3a34e7c6bfc6d7d9a53aeba9e706b309c9cf7ee96fa9c36ff7fd92a20", publicKey.description) | |||
XCTAssertEqual("3FJjnZNXC6FWQ2UJAaKL3Vme2EJavfgnXe", publicKey.bitcoinAddress(prefix: Bitcoin().payToScriptHashAddressPrefix).description) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
publicKey.bitcoinAddress(prefix: Bitcoin().payToScriptHashAddressPrefix)
=>
key.publicKey().bitcoinAddress(prefix: Bitcoin().payToScriptHashAddressPrefix)
Can we do it this way? Our goal to make sure it will be using compressed version without specifying it previously
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would break others if publicKey() is compressed by default, e.g ethereum.
check y is even or odd