Skip to content
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

bch: switch to testnet4 and use custom address stringer #1606

Merged
merged 2 commits into from
May 15, 2022

Conversation

chappjc
Copy link
Member

@chappjc chappjc commented May 6, 2022

Pulled out of a larger Electrum branch.

This does:

client/asset: custom address stringer

The exchange wallets had an address decoder function for converting
a string to a btcutil.Address, which is required for assets like BCH
with special address formats. However, it is incorrect to then use
the btcutil.Address String method go get a human readable address.

This adds a complementary helper function for converting from a
btcutil.Address to a correct string. Normally this is simply using
the Address as a Stringer, but for BCH it is the new EncodeCashAddress
function, which powers the existing RecodeCashAddress.

This updates the btc baseWallet methods to use the new stringAddr
to achieve the reverse of decodeAddr. This ensures that the string
address stored in the fundingCoins is correct for the asset. This
also ensures the recipient address in AuditContract is correct.
I believe the main issue resolved by this is providing the correct
address to the node.privKeyForAddress method when signing.
This also provides the correct string to the getaddressinfo RPC.
It is not clear if bitcoincashd was previously ok with the incorrect
address strings when handling RPCs.

This also removes the unused (*auditInfo).Recipient method, and
fixes the documentation for the other methods since there is no longer
an asset.AuditInfo *interface*, just a struct.

// Expiration returns the expiration time of the contract, which is the earliest
// time that a refund can be issued for an un-redeemed contract. Part of the
// asset.AuditInfo interface.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At one point there was an asset.AuditInfo interface, but it it became a concrete type at one point and these remained.

if err != nil {
return nil, nil, err
}
privKey, err := btc.node.privKeyForAddress(addrStr)
Copy link
Member Author

@chappjc chappjc May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but perhaps bitcoincashd (Bitcoin Cash Node) was able to pull out the pubkey from the bitcoin/legacy addresses and locate the private key. The chain params were the clone params with the correct bch address ID values, but this wasn't making Cash Addresses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's what it was doing.

$  ./beta getaddressinfo mgDzcoxdBrhu435knkdUEP5UGVYGKUJJgN
{
  "address": "bchreg:qqru9lns98ugq85h4cl3p8srf4y9w8cyt5eux55xmn",
  "scriptPubKey": "76a91407c2fe7029f8801e97ae3f109e034d48571f045d88ac",
  "ismine": true,
  "iswatchonly": false,
  "isscript": false,
  "pubkey": "0310892969dde0c7f6e177ce0f1a8ba807fbbbbc05135e9c47479f0aa1fa2555a5",
...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.

@buck54321
Copy link
Member

AddressStringer part is fixed in #1570 too.

@chappjc
Copy link
Member Author

chappjc commented May 11, 2022

AddressStringer part is fixed in #1570 too.

Ah, dang. Hopefully we did things similarly.
I guess I'll put the zcash pr at the top of my queue

@chappjc
Copy link
Member Author

chappjc commented May 11, 2022

We're close. I can minimize conflicts because having this fix separate makes sense IMO, but some points:

  • I can go with stringifyAddress if you prefer that to stringAddr
  • I'd prefer the EncodeCashAddress helper extracted from RecodeCashAddress because it avoids a needless encode->decode round trip before getting to the switch
  • Can you convince me that it's OK to ignore the error? It shouldn't happen I guess, and if it fails then it would be an empty string, and that could be caught or allowed to error in the next call I suppose
  • Good catch that BCHWallet no longer needs to exist.

@buck54321
Copy link
Member

Your way is better. I'll adapt.

@chappjc
Copy link
Member Author

chappjc commented May 14, 2022

Shall I merge this then?

@chappjc chappjc force-pushed the bcht4 branch 2 times, most recently from 29103e5 to 6b3c30c Compare May 14, 2022 15:23
@chappjc
Copy link
Member Author

chappjc commented May 14, 2022

Or just discard the stringer commit? Whatever

@buck54321
Copy link
Member

Any leads on getting some testnet4 BCH?

site: add bch explorer links
The exchange wallets had an address decoder function for converting
a string to a btcutil.Address, which is required for assets like BCH
with special address formats. However, it is incorrect to then use
the btcutil.Address String method go get a human readable address.

This adds a complementary helper function for converting from a
btcutil.Address to a correct string. Normally this is simply using
the Address as a Stringer, but for BCH it is the new EncodeCashAddress
function, which powers the existing RecodeCashAddress.

This updates the btc baseWallet methods to use the new stringAddr
to achieve the reverse of decodeAddr. This ensures that the string
address stored in the fundingCoins is correct for the asset. This
also ensures the recipient address in AuditContract is correct.
I believe the main issue resolved by this is providing the correct
address to the node.privKeyForAddress method when signing.
This also provides the correct string to the getaddressinfo RPC.
It is not clear if bitcoincashd was previously ok with the incorrect
address strings when handling RPCs.

This also removes the unused (*auditInfo).Recipient method, and
fixes the documentation for the other methods since there is no longer
an asset.AuditInfo *interface*, just a struct.
@chappjc chappjc merged commit f3924b6 into decred:master May 15, 2022
@chappjc chappjc deleted the bcht4 branch May 15, 2022 17:17
@chappjc chappjc added this to the 0.5 milestone May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants