Skip to content

Commit

Permalink
feat: support formatting testnet addresses, closes #469
Browse files Browse the repository at this point in the history
chore(release): 4.0.0-beta.15 [skip ci]

* check update logic ([be666f2](be666f2))

* support formatting testnet addresses, closes [#469](#469) ([9eab299](9eab299))
  • Loading branch information
kyranjamie committed Jan 13, 2021
1 parent 6799aa1 commit 0158735
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 11 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# [4.0.0-beta.15](https://github.com/blockstack/stacks-wallet/compare/v4.0.0-beta.14...v4.0.0-beta.15) (2021-01-10)


### Bug Fixes

* check update logic ([be666f2](https://github.com/blockstack/stacks-wallet/commit/be666f275e49b722f2d1a5bf1986c625ea45f681))


### Features

* support formatting testnet addresses, closes [#469](https://github.com/blockstack/stacks-wallet/issues/469) ([9eab299](https://github.com/blockstack/stacks-wallet/commit/9eab299eb9adf1b77f9674a553fbd40d216aba4e))

# [4.0.0-beta.14](https://github.com/blockstack/stacks-wallet/compare/v4.0.0-beta.13...v4.0.0-beta.14) (2021-01-09)


Expand Down
6 changes: 3 additions & 3 deletions app/components/home/stacking-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { useSelector } from 'react-redux';
import { Flex, Box, Text } from '@blockstack/ui';

import { RootState } from '@store/index';
import { convertPoxAddressToBtc } from '@utils/stacking';
import { formatPoxAddressToNetwork } from '@utils/stacking';
import { WaffleChart } from '@components/chart/waffle-chart';
import {
selectStackerInfo,
selectNextCycleInfo,
selectPoxInfo,
} from '../../store/stacking/stacking.reducer';
} from '@store/stacking/stacking.reducer';

type StackingCardProps = any;

Expand Down Expand Up @@ -77,7 +77,7 @@ export const StackingCard: FC<StackingCardProps> = () => {
<Flex flexDirection="column" mt="tight" mb="base-loose">
<Text textStyle="body.small.medium">Reward to be paid to</Text>
<Text as="code" fontSize="13px" mt="tight" color="ink.600">
{convertPoxAddressToBtc(stackerInfo?.pox_address)}
{formatPoxAddressToNetwork(stackerInfo?.pox_address)}
</Text>
</Flex>
</Box>
Expand Down
3 changes: 3 additions & 0 deletions app/pages/stacking/step/choose-btc-address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const ChooseBtcAddressStep: FC<ChooseBtcAddressStepProps> = props => {
if (NETWORK === 'mainnet' && address.network === 'testnet') {
return { btcAddress: 'Testnet addresses not supported on Mainnet' };
}
if (NETWORK === 'testnet' && address.network !== 'testnet') {
return { btcAddress: 'Mainnet addresses not supported on Testnet' };
}
// https://github.com/blockstack/stacks-blockchain/issues/1902
if (!SUPPORTED_BTC_ADDRESS_FORMATS.includes(address.type as any)) {
return {
Expand Down
126 changes: 126 additions & 0 deletions app/utils/stacking.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { convertPoxAddressToBtc } from './stacking';

describe(convertPoxAddressToBtc.name, () => {
describe('P2PKH mainnet', () => {
test('it resolves address correctly', () => {
const btcAddress = '1Q9a1zGPfJ4oH5Xaz5wc7BdvWV21fSNkkr';
const version = Buffer.from([0]);
const hashbytes = Buffer.from([
253,
233,
200,
45,
123,
196,
63,
85,
233,
5,
68,
56,
71,
12,
60,
168,
214,
231,
35,
127,
]);
const result = convertPoxAddressToBtc('mainnet')({ version, hashbytes });
expect(result).toEqual(btcAddress);
});
});

describe('P2PKH testnet', () => {
test('it resolves address correctly', () => {
const btcAddress = 'mp4pEBdJiMh6aL5Uhs6nZX1XhyZ4V2xrzg';
const version = Buffer.from([0]);
const hashbytes = Buffer.from([
93,
199,
149,
82,
47,
129,
220,
183,
235,
119,
74,
11,
142,
132,
182,
18,
227,
237,
193,
65,
]);
const result = convertPoxAddressToBtc('testnet')({ version, hashbytes });
expect(result).toEqual(btcAddress);
});
});

describe('P2SH mainnet', () => {
test('it resolves address correctly', () => {
const btcAddress = '36Qwfsp8n6hmh3FQSuUrYtbg7izLqbVQgS';
const version = Buffer.from([1]);
const hashbytes = Buffer.from([
51,
206,
108,
100,
110,
83,
178,
164,
245,
191,
182,
240,
242,
153,
119,
229,
81,
161,
53,
105,
]);
const result = convertPoxAddressToBtc('mainnet')({ version, hashbytes });
expect(result).toEqual(btcAddress);
});
});
describe('P2SH testnet', () => {
test('it resolves address correctly', () => {
const btcAddress = '2MwjqTzEJodSaoehcxRSqfWrvJMGZHq4tdC';
const version = Buffer.from([1]);
const hashbytes = Buffer.from([
49,
73,
195,
235,
162,
210,
28,
253,
238,
165,
104,
148,
134,
107,
143,
76,
209,
27,
114,
173,
]);
const result = convertPoxAddressToBtc('testnet')({ version, hashbytes });
expect(result).toEqual(btcAddress);
});
});
});
28 changes: 21 additions & 7 deletions app/utils/stacking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@ import { AddressHashMode } from '@stacks/transactions';
import BN from 'bn.js';
import { address } from 'bitcoinjs-lib';

const poxKeyToVersionBytesMap = {
[AddressHashMode.SerializeP2PKH]: 0,
[AddressHashMode.SerializeP2SH]: 5,
import { NETWORK } from '@constants/index';

const poxKeyToVersionBytesMap: Record<'mainnet' | 'testnet', any> = {
mainnet: {
[AddressHashMode.SerializeP2PKH]: 0x00,
[AddressHashMode.SerializeP2SH]: 0x05,
},
testnet: {
[AddressHashMode.SerializeP2PKH]: 0x6f,
[AddressHashMode.SerializeP2SH]: 0xc4,
},
};

interface ConvertToPoxAddressBtc {
version: Buffer;
hashbytes: Buffer;
}
export function convertPoxAddressToBtc({ version, hashbytes }: ConvertToPoxAddressBtc) {
const ver = new BN(version).toNumber();
if (ver === AddressHashMode.SerializeP2WPKH || ver === AddressHashMode.SerializeP2WSH) return '';
return address.toBase58Check(hashbytes, (poxKeyToVersionBytesMap as any)[ver]);
export function convertPoxAddressToBtc(network: 'mainnet' | 'testnet') {
return ({ version, hashbytes }: ConvertToPoxAddressBtc) => {
const ver = new BN(version).toNumber() as AddressHashMode;
console.log({ ver });
if (ver === AddressHashMode.SerializeP2WPKH || ver === AddressHashMode.SerializeP2WSH)
return null;
return address.toBase58Check(hashbytes, poxKeyToVersionBytesMap[network][ver]);
};
}

export const formatPoxAddressToNetwork = convertPoxAddressToBtc(NETWORK);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stacks-wallet",
"version": "4.0.0-beta.14",
"version": "4.0.0-beta.15",
"description": "Stacks Wallet 2.0 — Stacking",
"prettier": "@stacks/prettier-config",
"homepage": "https://www.blockstack.org/testnet-wallet",
Expand Down

0 comments on commit 0158735

Please sign in to comment.