Which BitcoinAddress should I use? #2053
Answered
by
optout21
AlleniCode
asked this question in
Q&A
-
let wallet = HDWallet(mnemonic: "away season lucky syrup indoor ketchup market april upset race tube fruit", passphrase: "")!
let privateKey = wallet.getKeyForCoin(coin: .bitcoin)
print(privateKey.data.hexString)
let privateKey1 = wallet.getKey(coin: .bitcoin, derivationPath: "m/84'/0'/0'/0/0")
print(privateKey1.data.hexString)
let privateKey2 = wallet.getDerivedKey(coin: .bitcoin, account: 0, change: 0, address: 0)
print(privateKey2.data.hexString)
let publicKey = privateKey.getPublicKeySecp256k1(compressed: true)
print(publicKey.data.hexString)
let publicKey1 = privateKey1.getPublicKeySecp256k1(compressed: true)
print(publicKey1.data.hexString)
let publicKey2 = privateKey2.getPublicKeySecp256k1(compressed: true)
print(publicKey2.data.hexString)
let address = BitcoinAddress(publicKey: publicKey, prefix: 0x0)
print(address!.description)
let address1 = BitcoinAddress(publicKey: publicKey1, prefix: 0x0)
print(address1!.description)
let address2 = BitcoinAddress(publicKey: publicKey2, prefix: 0x0)
print(address2!.description)
print("========")
print(wallet.getAddressForCoin(coin: .bitcoin))
print(CoinType.bitcoin.deriveAddressFromPublicKey(publicKey: publicKey))
print(CoinType.bitcoin.deriveAddressFromPublicKey(publicKey: publicKey1))
print(CoinType.bitcoin.deriveAddressFromPublicKey(publicKey: publicKey2))
print("========")
The private keys are same, but the address not same. |
Beta Was this translation helpful? Give feedback.
Answered by
optout21
Mar 3, 2022
Replies: 1 comment 1 reply
-
There are two kinds of Bitcoin addresses: the Segwit and the so called P2PK addresses. deriveAddressFromPublicKey uses the default SegwitAddress. You can use it directly using BitcoinSegwitAddress. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
optout21
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are two kinds of Bitcoin addresses: the Segwit and the so called P2PK addresses.
Segwit addresses start with 'bc1' and are more modern, TrustWallet uses segwit addresses (if you create a new wallet).
P2PK addresses start with '1', are legacy, TW supports them as legacy (e.g. as destination address).
deriveAddressFromPublicKey uses the default SegwitAddress. You can use it directly using BitcoinSegwitAddress.