Skip to content

Commit 6a14b30

Browse files
committed
Backend/Bitcoin: add recipient address checksum validation
1 parent f7c33d4 commit 6a14b30

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

src/GWallet.Backend/Account.fs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,14 @@ module Account =
9696
() // TODO: do something in this case??
9797
}
9898

99+
// TODO: add tests for these (just in case address validation breaks after upgrading our dependencies)
99100
let ValidateAddress (currency: Currency) (address: string) =
100101
match currency with
101102
| Currency.ETH | Currency.ETC ->
102103
Ether.Account.ValidateAddress currency address
103-
104104
| Currency.BTC ->
105105
Bitcoin.Account.ValidateAddress address
106106

107-
// FIXME: add bitcoin checksum algorithm?
108-
()
109-
110-
111107
let EstimateFee account amount destination: IBlockchainFeeInfo =
112108
let currency = (account:>IAccount).Currency
113109
match currency with

src/GWallet.Backend/Bitcoin/BitcoinAccount.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,9 @@ module internal Account =
401401
if (address.Length < BITCOIN_MIN_ADDRESSES_LENGTH) then
402402
raise (AddressWithInvalidLength(BITCOIN_MIN_ADDRESSES_LENGTH))
403403

404-
// FIXME: add bitcoin checksum algorithm?
404+
try
405+
BitcoinAddress.Create(address, Config.BitcoinNet) |> ignore
406+
with
407+
// TODO: propose to NBitcoin upstream to generate an NBitcoin exception instead
408+
| :? FormatException ->
409+
raise (AddressWithInvalidChecksum None)

src/GWallet.Backend/Ether/EtherAccount.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module internal Account =
5454

5555
if (not (addressUtil.IsChecksumAddress(address))) then
5656
let validCheckSumAddress = addressUtil.ConvertToChecksumAddress(address)
57-
raise (AddressWithInvalidChecksum(validCheckSumAddress))
57+
raise (AddressWithInvalidChecksum(Some validCheckSumAddress))
5858

5959
let EstimateFee (currency: Currency): MinerFee =
6060
let gasPrice = Ether.Server.GetGasPrice currency

src/GWallet.Backend/Exceptions.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ exception InvalidPassword
55
exception DestinationEqualToOrigin
66
exception AddressMissingProperPrefix of seq<string>
77
exception AddressWithInvalidLength of int
8-
exception AddressWithInvalidChecksum of string
8+
exception AddressWithInvalidChecksum of Option<string>
99
exception AccountAlreadyAdded

src/GWallet.Frontend.Console/UserInteraction.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,19 @@ module UserInteraction =
321321
failwith (sprintf "Address introduced '%s' gave a length error with a limit that matches its length: %d=%d"
322322
publicAddress lengthLimitViolated publicAddress.Length)
323323
AskPublicAddress currency askText
324-
| AddressWithInvalidChecksum(addressWithValidChecksum) ->
324+
| AddressWithInvalidChecksum maybeAddressWithValidChecksum ->
325325
Console.Error.WriteLine "WARNING: the address provided didn't pass the checksum, are you sure you copied it properly?"
326-
Console.Error.WriteLine "(If you used the clipboard, you're likely copying it from a service that doesn't have checksum validation.)"
327326
Console.Error.WriteLine "(If you copied it by hand or somebody dictated it to you, you probably made a spelling mistake.)"
328-
let continueWithoutChecksum = AskYesNo "Continue with this address?"
329-
if (continueWithoutChecksum) then
330-
addressWithValidChecksum
331-
else
327+
match maybeAddressWithValidChecksum with
328+
| None ->
332329
AskPublicAddress currency askText
330+
| Some addressWithValidChecksum ->
331+
Console.Error.WriteLine "(If you used the clipboard, you're likely copying it from a service that doesn't have checksum validation.)"
332+
let continueWithoutChecksum = AskYesNo "Continue with this address?"
333+
if (continueWithoutChecksum) then
334+
addressWithValidChecksum
335+
else
336+
AskPublicAddress currency askText
333337
validatedAddress
334338

335339
type private AmountOption =

0 commit comments

Comments
 (0)