Skip to content

Commit

Permalink
feat: update supported btc address formats
Browse files Browse the repository at this point in the history
  • Loading branch information
edu-stx committed Jan 10, 2023
1 parent a280c13 commit 0a8fd55
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const STACKING_CONTRACT_CALL_TX_BYTES = 260;

export const POOLED_STACKING_TX_SIZE_BYTES = 216;

export const SUPPORTED_BTC_ADDRESS_FORMATS = ['p2pkh', 'p2sh'] as const;
export const SUPPORTED_BTC_ADDRESS_FORMATS = ['p2pkh', 'p2sh', 'p2wpkh', 'p2tr'] as const;

export const LATEST_LEDGER_VERSION_MAJOR = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ const StackingAddressErrorExplainer = memo(() => (
<ExplainerLabel text="Legacy, or P2PKH, Bitcoin addresses begin with the number 1">
Legacy
</ExplainerLabel>
or
,
<ExplainerLabel text="SegWit (Segregated Witness), or P2SH, Bitcoin addresses begin with the number 3">
SegWit
</ExplainerLabel>
address.
<ExplainerLabel text="Native SegWit addresses begin with the letters bc">
,
<ExplainerLabel text='Native SegWit, or P2WPKH, Bitcoin addresses begin with "bc1q"'>
Native SegWit
</ExplainerLabel>
addresses are not supported.
or
<ExplainerLabel text='Taproot, or P2TR, Bitcoin addresses begin with "bc1p"'>
Taproot
</ExplainerLabel>
address.
<ExternalLink
href={STACKING_ADDRESS_FORMAT_HELP_URL}
textDecoration="underline"
Expand All @@ -52,8 +56,12 @@ export const ChooseBtcAddressField: FC = () => {

return (
<Step title="Bitcoin address">
<Description>Choose the address where you’d like to receive bitcoin.</Description>
<CryptoAddressInput fieldName="btcAddress" placeholder="Bitcoin address" {...field}>
<Description>Enter the Bitcoin address where you'd like to receive your rewards.</Description>
<CryptoAddressInput
fieldName="btcAddress"
placeholder="Bitcoin address (Legacy, Native SegWit or Taproot)"
{...field}
>
{meta.touched && errors}
</CryptoAddressInput>
</Step>
Expand Down
20 changes: 11 additions & 9 deletions app/utils/validators/btc-address-validator.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import * as yup from 'yup';
import { isMainnet, isTestnet } from '@utils/network-utils';
import validate, { getAddressInfo } from 'bitcoin-address-validation';
import { validate, getAddressInfo } from 'bitcoin-address-validation';
import { SUPPORTED_BTC_ADDRESS_FORMATS } from '@constants/index';

export function btcAddressSchema() {
return yup
.string()
.defined(`Enter the BTC address where you'd like to receive your rewards`)
.test({
name: 'slkdjfslk',
name: 'valid address',
message: `The BTC address you've entered is not valid`,
test(value: unknown) {
if (value === null || value === undefined) return false;
if (typeof value !== 'string') return false;
const isValid = validate(value);
if (!isValid) return this.createError({ message: 'Invalid BTC address' });
const validationReport = getAddressInfo(value);
if (!validationReport) return this.createError({ message: 'Invalid BTC address' });
if (isMainnet() && validationReport.network === 'testnet') {
const addressInfo = getAddressInfo(value);
if (!addressInfo) return this.createError({ message: 'Invalid BTC address' });
if (isMainnet() && addressInfo.network === 'testnet') {
return this.createError({ message: 'Testnet addresses not supported on Mainnet' });
}
if (isTestnet() && validationReport.network !== 'testnet') {
if (isTestnet() && addressInfo.network !== 'testnet') {
return this.createError({ message: 'Mainnet addresses not supported on Testnet' });
}
// https://github.com/blockstack/stacks-blockchain/issues/1902
if (!SUPPORTED_BTC_ADDRESS_FORMATS.includes(validationReport.type as any)) {
return this.createError({ message: 'is-bech32' });
if (!SUPPORTED_BTC_ADDRESS_FORMATS.includes(addressInfo.type as any)) {
return this.createError({
message:
'Unsupported address type. Please use a legacy (P2PKH), pay to script hash (P2SH), native segwit (P2WPKH) or taproot (P2TR) address',
});
}
return true;
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
"axios": "0.21.4",
"bignumber.js": "9.0.1",
"bip39": "3.0.3",
"bitcoin-address-validation": "2.0.1",
"bitcoin-address-validation": "2.2.1",
"bn.js": "https://github.com/kyranjamie/bn.js",
"buffer": "6.0.3",
"c32check": "1.1.2",
Expand Down
22 changes: 16 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5999,6 +5999,11 @@ base-x@^3.0.8:
dependencies:
safe-buffer "^5.0.1"

base58-js@^1.0.0:
version "1.0.5"
resolved "https://registry.yarnpkg.com/base58-js/-/base58-js-1.0.5.tgz#00697bff954aa85007fa45ce76b699b7960768cd"
integrity sha512-LkkAPP8Zu+c0SVNRTRVDyMfKVORThX+rCViget00xdgLRrKkClCTz1T7cIrpr69ShwV5XJuuoZvMvJ43yURwkA==

base64-js@^1.3.1, base64-js@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
Expand Down Expand Up @@ -6079,14 +6084,14 @@ bip39@3.0.3:
pbkdf2 "^3.0.9"
randombytes "^2.0.1"

bitcoin-address-validation@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/bitcoin-address-validation/-/bitcoin-address-validation-2.0.1.tgz#57864114687db11b7884a44224243c59a3f5f1e5"
integrity sha512-S3VEoqW4w/92QKKZhmraw84oUXc35i++hOknY9lxy9p10MXdwmhPjTNuKP37dm8YttYr5elUQE3jumc9l/PR5w==
bitcoin-address-validation@2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/bitcoin-address-validation/-/bitcoin-address-validation-2.2.1.tgz#049b5bc54687e4a28a5b7f599968986c5a9f98cb"
integrity sha512-f6LXNpvRKlTbHWb37N9tHoAbYGbshzM8FPWvCtloh++hxZ0/dmkokvKNVLz6HkG82zVwo8w6Sq4JmfO2timzyg==
dependencies:
base-x "^3.0.8"
base58-js "^1.0.0"
bech32 "^2.0.0"
sha.js "^2.4.11"
sha256-uint8array "^0.10.3"

bl@^4.1.0:
version "4.1.0"
Expand Down Expand Up @@ -13419,6 +13424,11 @@ sha.js@^2.4.0, sha.js@^2.4.11, sha.js@^2.4.8:
inherits "^2.0.1"
safe-buffer "^5.0.1"

sha256-uint8array@^0.10.3:
version "0.10.3"
resolved "https://registry.yarnpkg.com/sha256-uint8array/-/sha256-uint8array-0.10.3.tgz#322a91413f78f518e0b9c8f8e982cc2e629b59c7"
integrity sha512-SFTs87RfXVulKrhhP6B5/qcFruOKQZaKf6jY9V4PJ7NOG0qIlQP6XL4pQq5xagsuP/Wd55S7tUBJpRajEsDUEQ==

shallow-clone@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/shallow-clone/-/shallow-clone-3.0.1.tgz#8f2981ad92531f55035b01fb230769a40e02efa3"
Expand Down

0 comments on commit 0a8fd55

Please sign in to comment.