-
Notifications
You must be signed in to change notification settings - Fork 147
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
Bug: the function 'deriveAddress' from a public key with ecdsa #1822
Comments
I cannot comment on tx-wrapper and their utils, however the See common/packages/util-crypto/src/address/derive.ts Lines 17 to 23 in e5cb0ba
As for Substrate ecdsa, the 33-byte (compressed) publicKey is passed through blake2 to generate the address which is then encoded into ss58. |
Thanks for your comment. Please let me know if my understanding below is wrong somewhere. About: import { deriveAddress } from "@substrate/txwrapper-polkadot"
But, as you see the code above, the function deriveAddress heavily depends on the function encodeAddress(publicKey, ss58Format) which is shown on https://github.com/polkadot-js/common/blob/master/packages/util-crypto/src/address/encode.ts I will also leave this report on https://github.com/paritytech/txwrapper-core Thanks |
As you can see above in the code posted by you - it encodes the publicKey as ss58. As per the keyring implementation, for ecdsa this is not the case, it follows a different path. If you wish to re-implement the keyring pair address functionality with the lower level functions, you would need to apply the logic yourself. For any address re-encoding, you always need to start with the address, not the publicKey. (Because anything ecdsa has hashing applied to get to the ss58 - so address decode and the recode is correct) So in the keyring pairs, the encode functionality is the following - common/packages/keyring/src/pair/index.ts Lines 114 to 120 in e5cb0ba
Where the common/packages/keyring/src/pair/index.ts Lines 44 to 49 in e5cb0ba
Hence always starting from the |
@jacogr Thanks for supplying the info to @drhanlondon. It's much appreciated, the code from our library was a bit of some legacy code that lacked the full support of all schemes so i fixed it up. Thanks again. I used the /**
* Copyright 2023 via polkadot-js/common
*
* The slightly modified below logic is copyrighted from polkadot-js/common . The exact path to the code can be seen here:
* https://github.com/polkadot-js/common/blob/e5cb0ba2b4a6b5817626cc964b4f66334f2410e4/packages/keyring/src/pair/index.ts#L44-L49
*/
const TYPE_ADDRESS = {
ecdsa: (p: Uint8Array) => (p.length > 32 ? blake2AsU8a(p) : p),
ed25519: (p: Uint8Array) => p,
sr25519: (p: Uint8Array) => p,
}; |
All good. A link is always appreciated. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query. |
Hello,
Question 1: On Substrate-default (Westend testnet),
To derive an address from a publicKey with "sr25519", it works as below
The result is
But, next, to derive from a public key with "ecdsa", I did in the same way above with a different seed phrase
The result is
We can see that unexpectedly the returned value "KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH" is not an address (ss58), but just a public key (ss58).
By using Subkey tool, we can confirm that "KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH" is a public key(ss58).
#########################################################
from seed phrase
$ subkey inspect "asthma stereo budget skill frequent sunny enemy train kiwi word hold evil" --scheme Ecdsa
Secret phrase: asthma stereo budget skill frequent sunny enemy train kiwi word hold evil
Network ID: substrate
Secret seed: 0x67c9fddc7e706f33e3e20e4918e7e68abd986caade7c1d35e7596df4be9ca5bd
Public key (hex): 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc
Account ID: 0xfe8995168a62071594194772c371a72f1c57149273235556e2711f3a76dab5e0
Public key (SS58): KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
SS58 Address: 5HpStbV2wnRhqiy8zsxeKYxJRLyKrsbH3Pyq9nrXtTeWXwcq
###################
from secret seed
$ subkey inspect 0x67c9fddc7e706f33e3e20e4918e7e68abd986caade7c1d35e7596df4be9ca5bd --scheme Ecdsa
Secret Key URI
0x67c9fddc7e706f33e3e20e4918e7e68abd986caade7c1d35e7596df4be9ca5bd
is account:Network ID: substrate
Secret seed: 0x67c9fddc7e706f33e3e20e4918e7e68abd986caade7c1d35e7596df4be9ca5bd
Public key (hex): 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc
Account ID: 0xfe8995168a62071594194772c371a72f1c57149273235556e2711f3a76dab5e0
Public key (SS58): KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
SS58 Address: 5HpStbV2wnRhqiy8zsxeKYxJRLyKrsbH3Pyq9nrXtTeWXwcq
####################
from public key(hex)
$ subkey inspect --public 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc --scheme Ecdsa
Network ID/Version: substrate
Public key (hex): 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc
Account ID: 0xfe8995168a62071594194772c371a72f1c57149273235556e2711f3a76dab5e0
Public key (SS58): KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
SS58 Address: KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
#####################
from public key (ss58)
$ subkey inspect KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH --scheme Ecdsa
Public Key URI
KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
is account:Network ID/Version: substrate
Public key (hex): 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc
Account ID: 0xfe8995168a62071594194772c371a72f1c57149273235556e2711f3a76dab5e0
Public key (SS58): KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
SS58 Address: KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
############################
even with Substrate
$ cargo run --release -p subkey -- inspect "KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH" --scheme Ecdsa
Public Key URI
KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
is account:Network ID/Version: substrate
Public key (hex): 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc
Account ID: 0xfe8995168a62071594194772c371a72f1c57149273235556e2711f3a76dab5e0
Public key (SS58): KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
SS58 Address: KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
##############################
$ cargo run --release -p subkey -- inspect "asthma stereo budget skill frequent sunny enemy train kiwi word hold evil" --scheme Ecdsa
Finished release [optimized] target(s) in 0.60s
Running
target/release/subkey inspect 'asthma stereo budget skill frequent sunny enemy train kiwi word hold evil' --scheme Ecdsa
Secret phrase: asthma stereo budget skill frequent sunny enemy train kiwi word hold evil
Network ID: substrate
Secret seed: 0x67c9fddc7e706f33e3e20e4918e7e68abd986caade7c1d35e7596df4be9ca5bd
Public key (hex): 0x02e59f872a5a49b7d4807f4f52db82c6d62ce11ba6ce1d13e490cb6cab302874fc
Account ID: 0xfe8995168a62071594194772c371a72f1c57149273235556e2711f3a76dab5e0
Public key (SS58): KW87j6aqqJ8heTCTBeoxMLH1Yg2YmkeTz4Xwg4yHAmhmVX7wH
SS58 Address: 5HpStbV2wnRhqiy8zsxeKYxJRLyKrsbH3Pyq9nrXtTeWXwcq
##############################
As we see above, deriveAddress() returns a public key (ss58) in case of "ecdsa and Westend(42)" although we expect an address "5HpStbV2wnRhqiy8zsxeKYxJRLyKrsbH3Pyq9nrXtTeWXwcq"
But, this bug does not occur on Polkadot with "ecdsa"
I would like to know whether this is a bug or not on Westend.
Question 2: as we see the above tests with Subkey and Substrate, I would like to know why "SS58 Address" does not show a proper ss58 address when inspecting with either a public key(hex) or a public key (ss58).
Thank you
The text was updated successfully, but these errors were encountered: