Skip to content

Commit 17e3dd3

Browse files
Fix Address Generation by Properly Left-Padding Public Key Coordinates (#569)
* fix: Address Generation by Properly Left-Padding Public Key Coordinates * fix: make gen-doc
1 parent 87477c6 commit 17e3dd3

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

cmd/wallet/wallet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func init() {
132132
inputPassword = WalletCmd.PersistentFlags().String("password", "", "Password used along with the mnemonic")
133133
inputPasswordFile = WalletCmd.PersistentFlags().String("password-file", "", "Password stored in a file used along with the mnemonic")
134134
inputMnemonic = WalletCmd.PersistentFlags().String("mnemonic", "", "A mnemonic phrase used to generate entropy")
135-
inputMnemonicFile = WalletCmd.PersistentFlags().String("mnemonic-file", "", "A mneomonic phrase written in a file used to generate entropy")
136-
inputUseRawEntropy = WalletCmd.PersistentFlags().Bool("raw-entropy", false, "substrate and polkda dot don't follow strict bip39 and use raw entropy")
135+
inputMnemonicFile = WalletCmd.PersistentFlags().String("mnemonic-file", "", "A mnemonic phrase written in a file used to generate entropy")
136+
inputUseRawEntropy = WalletCmd.PersistentFlags().Bool("raw-entropy", false, "substrate and polka dot don't follow strict bip39 and use raw entropy")
137137
inputRootOnly = WalletCmd.PersistentFlags().Bool("root-only", false, "don't produce HD accounts. Just produce a single wallet")
138138
}

doc/polycli_wallet.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ $ polycli wallet create --path "m/44'/0'/0'" --addresses 5
6060
--iterations uint Number of pbkdf2 iterations to perform (default 2048)
6161
--language string Which language to use [ChineseSimplified, ChineseTraditional, Czech, English, French, Italian, Japanese, Korean, Spanish] (default "english")
6262
--mnemonic string A mnemonic phrase used to generate entropy
63-
--mnemonic-file string A mneomonic phrase written in a file used to generate entropy
63+
--mnemonic-file string A mnemonic phrase written in a file used to generate entropy
6464
--password string Password used along with the mnemonic
6565
--password-file string Password stored in a file used along with the mnemonic
6666
--path string What would you like the derivation path to be (default "m/44'/60'/0'")
67-
--raw-entropy substrate and polkda dot don't follow strict bip39 and use raw entropy
67+
--raw-entropy substrate and polka dot don't follow strict bip39 and use raw entropy
6868
--root-only don't produce HD accounts. Just produce a single wallet
6969
--words int The number of words to use in the mnemonic (default 24)
7070
```

hdwallet/hdwallet.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,23 @@ func RawPubKeyToETHAddress(concat []byte) common.Address {
330330
b := h.Sum(nil)
331331
return common.BytesToAddress(b)
332332
}
333+
333334
func toUncompressedPubKey(prvKey *bip32.Key) []byte {
334-
// the GetPublicKey method returns a compressed key so we'll manually get the public key from the curve
335335
curve := secp256k1.S256()
336336
x1, y1 := curve.ScalarBaseMult(prvKey.Key)
337-
concat := append(x1.Bytes(), y1.Bytes()...)
337+
338+
// left-pad each coordinate to 32 bytes
339+
xBytes := x1.Bytes()
340+
yBytes := y1.Bytes()
341+
342+
paddedX := make([]byte, 32-len(xBytes), 32)
343+
paddedX = append(paddedX, xBytes...)
344+
345+
paddedY := make([]byte, 32-len(yBytes), 32)
346+
paddedY = append(paddedY, yBytes...)
347+
348+
// Then we just append them
349+
concat := append(paddedX, paddedY...)
338350
return concat
339351
}
340352

hdwallet/hdwallet_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,33 @@ func TestGetPublicKeyFromSeed(t *testing.T) {
315315
}
316316

317317
}
318+
319+
// https://github.com/0xPolygon/polygon-cli/issues/564
320+
func TestPaddedPublicKey(t *testing.T) {
321+
pw, err := NewPolyWallet("cancel panther badge spell bleak summer hair cup frozen gossip tell element", "")
322+
if err != nil {
323+
t.Errorf("Failed to create new poly wallet: %v", err)
324+
}
325+
err = pw.SetPath("m/44'/60'/0'")
326+
if err != nil {
327+
t.Errorf("Failed setting derivation path failed: %v", err)
328+
}
329+
err = pw.SetIterations(2048)
330+
if err != nil {
331+
t.Errorf("Failed to set iteration count: %v", err)
332+
}
333+
err = pw.SetUseRawEntropy(false)
334+
if err != nil {
335+
t.Errorf("Failed to set raw entropy: %v", err)
336+
}
337+
key, err := pw.ExportHDAddresses(2)
338+
if err != nil {
339+
t.Errorf("Failed to export HD address %v", err)
340+
}
341+
if len(key.Addresses) != 2 {
342+
t.Errorf("Expected 2 addresses to be exported and got %d", len(key.Addresses))
343+
}
344+
if key.Addresses[1].ETHAddress != "0x2CDfa87C022744CceABC525FaA8e85Df6984A60d" {
345+
t.Errorf("Unexpected address. Expected 0x2CDfa87C022744CceABC525FaA8e85Df6984A60d and Got %s", key.Addresses[1].ETHAddress)
346+
}
347+
}

0 commit comments

Comments
 (0)