Skip to content

Commit 71c54c7

Browse files
werner-stacksgitbook-bot
authored andcommitted
GITBOOK-53: @stacks/bitcoin-staking 7.6.0 reference: remaining 165 export pages
1 parent 774317c commit 71c54c7

178 files changed

Lines changed: 13137 additions & 16 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/reference/SUMMARY.md‎

Lines changed: 176 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# BtcAddress
2+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# BtcAddress.parse
2+
3+
Parses a Bitcoin address string into a [BtcAddressRepr](btcaddressrepr.md): its [PoXAddressVersion](../constants/poxaddressversion.md) and hash bytes. Pure computation, no network call. Exported in the `BtcAddress` namespace.
4+
5+
***
6+
7+
### Usage
8+
9+
```ts
10+
import { BtcAddress } from '@stacks/bitcoin-staking';
11+
12+
const repr = BtcAddress.parse('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', 'mainnet');
13+
repr.version; // 4 (PoXAddressVersion.P2WPKH)
14+
repr.data; // Uint8Array(20), 751e76e8199196d454941c45d1b3a323f1433bd6
15+
16+
BtcAddress.parse('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', 'testnet');
17+
// throws: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4' is not a valid testnet P2PKH/P2SH/P2WPKH/P2WSH/P2TR address
18+
```
19+
20+
#### Notes
21+
22+
* Accepted forms: base58 P2PKH and P2SH addresses, bech32 segwit v0 addresses with a 20-byte (P2WPKH) or 32-byte (P2WSH) program, and bech32m segwit v1 addresses with a 32-byte program (P2TR). Segwit prefixes can be `bc`, `tb` or `bcrt`.
23+
* Every base58 script-hash address parses as `P2SH`. The result is never `P2SHP2WPKH` or `P2SHP2WSH`.
24+
* Pass `network` to reject an address from another network. The base58 version byte must then be that network's P2PKH or P2SH byte, and the segwit prefix must be `bc` for `'mainnet'`, `tb` for `'testnet'`, or `bcrt` for `'devnet'` and `'mocknet'`. Without `network`, any of them is accepted and the result does not record which.
25+
* An invalid address throws an `Error` with the message `'<address>' is not a valid P2PKH/P2SH/P2WPKH/P2WSH/P2TR address`. If the address starts like a base58 or segwit address, the message names the network when one was passed, and the error's `cause` holds the specific reason, such as a bad checksum or a wrong network.
26+
* A `StacksNetwork` object that matches no known network throws `networkNameFrom: unrecognized network object` before the address is read.
27+
28+
[**Reference Link**](https://github.com/stx-labs/stacks.js/blob/6101c99efe5a9616ce7e16cef68e28fd10676e7e/packages/bitcoin-staking/src/btc-address.ts#L141-L194)
29+
30+
***
31+
32+
### Signature
33+
34+
```ts
35+
function parse(
36+
btcAddress: string,
37+
network?: StacksNetworkName | StacksNetwork
38+
): BtcAddressRepr;
39+
```
40+
41+
***
42+
43+
### Returns
44+
45+
`BtcAddressRepr`
46+
47+
The address as a [BtcAddressRepr](btcaddressrepr.md).
48+
49+
***
50+
51+
### Parameters
52+
53+
#### btcAddress (required)
54+
55+
* **Type**: `string`
56+
57+
The Bitcoin address to parse.
58+
59+
#### network (optional)
60+
61+
* **Type**: `StacksNetworkName | StacksNetwork`
62+
63+
The network the address must belong to: a name such as `'mainnet'` or `'testnet'`, or a [StacksNetwork](../../stacks-network/network/StacksNetwork.md) object. When omitted, the network is not checked.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# BtcAddress.stringify
2+
3+
Encodes a [BtcAddressRepr](btcaddressrepr.md), or a Clarity `{ version, hashbytes }` tuple, as a Bitcoin address string for a given network. Pure computation, no network call. Exported in the `BtcAddress` namespace.
4+
5+
***
6+
7+
### Usage
8+
9+
```ts
10+
import { BtcAddress, PoXAddressVersion } from '@stacks/bitcoin-staking';
11+
import { hexToBytes } from '@stacks/common';
12+
import { Cl } from '@stacks/transactions';
13+
14+
const data = hexToBytes('751e76e8199196d454941c45d1b3a323f1433bd6');
15+
16+
BtcAddress.stringify({ version: PoXAddressVersion.P2WPKH, data }, 'mainnet');
17+
// 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'
18+
19+
BtcAddress.stringify({ version: PoXAddressVersion.P2WPKH, data }, 'testnet');
20+
// 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx'
21+
22+
// Same address from a Clarity tuple
23+
const tuple = Cl.tuple({ version: Cl.buffer(Uint8Array.of(0x04)), hashbytes: Cl.buffer(data) });
24+
BtcAddress.stringify(tuple, 'mainnet');
25+
// 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'
26+
```
27+
28+
#### Notes
29+
30+
* Versions `0x00` to `0x03` encode as base58 addresses, with `P2SHP2WPKH` and `P2SHP2WSH` written as P2SH addresses. `0x04` and `0x05` encode as bech32, `0x06` as bech32m.
31+
* The network picks the prefix: `bc` and base58 bytes `0x00`/`0x05` for `'mainnet'`, `tb` and `0x6f`/`0xc4` for `'testnet'`, `bcrt` and `0x6f`/`0xc4` for `'devnet'` and `'mocknet'`.
32+
* An argument with a `type` field is read as a Clarity tuple. A tuple without buffer `version` and `hashbytes` entries throws an `Error` starting with `Invalid argument`.
33+
* An unknown version throws `Unexpected PoX address version: <n>`. Hash bytes of the wrong length throw `Invalid PoX address hashbytes: version <v> requires <n> bytes, got <m>`.
34+
* A `StacksNetwork` object that matches no known network throws `networkNameFrom: unrecognized network object`.
35+
36+
[**Reference Link**](https://github.com/stx-labs/stacks.js/blob/6101c99efe5a9616ce7e16cef68e28fd10676e7e/packages/bitcoin-staking/src/btc-address.ts#L196-L242)
37+
38+
***
39+
40+
### Signature
41+
42+
```ts
43+
function stringify(
44+
address: BtcAddressRepr | TupleCV,
45+
network: StacksNetworkName | StacksNetwork
46+
): string;
47+
```
48+
49+
***
50+
51+
### Returns
52+
53+
`string`
54+
55+
The Bitcoin address.
56+
57+
***
58+
59+
### Parameters
60+
61+
#### address (required)
62+
63+
* **Type**: `BtcAddressRepr | TupleCV`
64+
65+
The address to encode: a [BtcAddressRepr](btcaddressrepr.md), or a Clarity tuple with one-byte buffer `version` and buffer `hashbytes`, such as one built with `Cl.tuple`.
66+
67+
#### network (required)
68+
69+
* **Type**: `StacksNetworkName | StacksNetwork`
70+
71+
The network to encode for: a name such as `'mainnet'` or `'testnet'`, or a [StacksNetwork](../../stacks-network/network/StacksNetwork.md) object.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# BtcAddressRepr
2+
3+
A parsed Bitcoin address: a [PoXAddressVersion](../constants/poxaddressversion.md) and the hash or witness program bytes. It carries no network. [BtcAddress.parse](btcaddress-parse.md) and [parseSignerCalldata](../signer/parsesignercalldata.md) return it, and [BtcAddress.stringify](btcaddress-stringify.md) and [buildSignerCalldata](../signer/buildsignercalldata.md) accept it.
4+
5+
***
6+
7+
### Usage
8+
9+
```ts
10+
import { BtcAddress } from '@stacks/bitcoin-staking';
11+
12+
const repr = BtcAddress.parse('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
13+
// { version: PoXAddressVersion.P2WPKH, data: Uint8Array(20) } (hash 751e76e8199196d454941c45d1b3a323f1433bd6)
14+
15+
BtcAddress.stringify(repr, 'testnet'); // 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx'
16+
```
17+
18+
[**Reference Link**](https://github.com/stx-labs/stacks.js/blob/6101c99efe5a9616ce7e16cef68e28fd10676e7e/packages/bitcoin-staking/src/btc-address.ts#L20-L26)
19+
20+
***
21+
22+
### Definition
23+
24+
```ts
25+
export interface BtcAddressRepr {
26+
/** PoX address version byte. */
27+
version: PoXAddressVersion;
28+
/** Hash / witness-program bytes. */
29+
data: Uint8Array;
30+
}
31+
```
32+
33+
***
34+
35+
### Properties
36+
37+
| Property | Type | Description |
38+
| --------- | ------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- |
39+
| `version` | [PoXAddressVersion](../constants/poxaddressversion.md) | Address type. Encoded as the one-byte `version` of a `{ version, hashbytes }` tuple |
40+
| `data` | `Uint8Array` | Hash or witness program: 20 bytes for versions `0x00` to `0x04`, 32 bytes for `0x05` and `0x06`. Encoded as `hashbytes` |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Build
2+
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# buildAnnounceL1EarlyExit
2+
3+
Builds an unsigned contract call to pox-5 `announce-l1-early-exit`, which tells pox-5 that a staker has left their L1 BTC lockup early. pox-5 stops counting the staker's BTC for the rest of the bond; their STX stays locked.
4+
5+
***
6+
7+
### Usage
8+
9+
```ts
10+
import {
11+
buildAnnounceL1EarlyExit,
12+
fetchBondMembership,
13+
fetchEligibleAnnounceL1EarlyExit,
14+
} from '@stacks/bitcoin-staking';
15+
import {
16+
Pc,
17+
TransactionSigner,
18+
broadcastTransaction,
19+
fetchNonce,
20+
privateKeyToAddress,
21+
privateKeyToPublic,
22+
publicKeyToHex,
23+
} from '@stacks/transactions';
24+
25+
const network = 'mainnet';
26+
const privateKey = process.env.STAKER_KEY!;
27+
const staker = privateKeyToAddress(privateKey, network);
28+
29+
const membership = await fetchBondMembership({ address: staker, network });
30+
if (!membership?.isL1Lock) throw new Error('no active L1 bond membership');
31+
const oldSignerManager = membership.signer;
32+
33+
const check = await fetchEligibleAnnounceL1EarlyExit({ staker, oldSignerManager, network });
34+
if (!check.ok) throw new Error(`announce-l1-early-exit would fail: ${check.reasons}`);
35+
36+
const tx = await buildAnnounceL1EarlyExit({
37+
staker,
38+
oldSignerManager,
39+
publicKey: publicKeyToHex(privateKeyToPublic(privateKey)),
40+
fee: 10_000n, // micro-STX
41+
nonce: await fetchNonce({ address: staker, network }),
42+
network,
43+
postConditions: [Pc.principal(staker).willPerformPox()],
44+
});
45+
46+
new TransactionSigner(tx).signOrigin(privateKey);
47+
const result = await broadcastTransaction({ transaction: tx, network });
48+
```
49+
50+
#### Notes
51+
52+
* The staker must send it. pox-5 requires `contract-caller`, `tx-sender` and `staker` to be the same principal, so a call forwarded through another contract also fails with `ERR_UNAUTHORIZED (u1)` ([pox-5.clar L1175-L1257](https://github.com/stacks-network/stacks-core/blob/10474cdec9f9c0bdf05841b58cffc89cdad87a9b/stackslib/src/chainstate/stacks/boot/pox-5.clar#L1175-L1257)).
53+
* Only for a membership backed by an L1 lockup (`isL1Lock`): an sBTC-backed membership fails with `ERR_CANNOT_ANNOUNCE_L1_EARLY_UNLOCK (u35)` and withdraws with [buildUnstakeSbtc](buildunstakesbtc.md) instead.
54+
* Also fails with `ERR_NOT_BOND_PARTICIPANT (u34)` when the staker has no bond membership or its bond has ended, `ERR_STAKE_IN_PREPARE_PHASE (u47)` in the last `prepareCycleLength` Bitcoin blocks of a reward cycle (100 on mainnet), and `ERR_INVALID_OLD_SIGNER_MANAGER (u36)` when `oldSignerManager` is not the bound signer-manager.
55+
* A second announcement for the same bond fails with `ERR_L1_EARLY_EXIT_ALREADY_ANNOUNCED (u50)`. Check with [fetchHasAnnouncedL1EarlyExit](../fetch/fetchhasannouncedl1earlyexit.md).
56+
* [fetchEligibleAnnounceL1EarlyExit](../eligibility/fetcheligibleannouncel1earlyexit.md) dry-runs every check except the caller check.
57+
* On success, pox-5 settles the staker's rewards, then sets their bond shares to zero from the current reward cycle, or the bond's first reward cycle if it has not started, through the end of the bond, reduces the bond totals by the same sats, and sets the membership's `amount-sats` to 0. The locked STX is untouched and unlocks on the bond's normal schedule.
58+
* `announce-l1-early-exit` is a position-altering PoX action for the sender ([pox\_5.rs](https://github.com/stacks-network/stacks-core/blob/10474cdec9f9c0bdf05841b58cffc89cdad87a9b/pox-locking/src/pox_5.rs#L572-L584)). In the default `deny` mode, and in `originator` mode, the node aborts the transaction unless it carries a PoX post condition for the sender, such as `Pc.principal(staker).willPerformPox()` ([node check](https://github.com/stacks-network/stacks-core/blob/10474cdec9f9c0bdf05841b58cffc89cdad87a9b/crates/stacks-transactions/src/lib.rs#L462-L494)). No Stacks asset moves, so no other post condition is needed.
59+
* A txid from [broadcastTransaction](../../stacks-transactions/network/broadcastTransaction.md) confirms submission only. The call can still fail on-chain: read the mined transaction's result and pass an `(err uN)` to [parsePox5Error](../errors/parsepox5error.md).
60+
61+
[**Reference Link**](https://github.com/stx-labs/stacks.js/blob/6101c99efe5a9616ce7e16cef68e28fd10676e7e/packages/bitcoin-staking/src/build.ts#L356-L394)
62+
63+
***
64+
65+
### Signature
66+
67+
```ts
68+
function buildAnnounceL1EarlyExit(
69+
args: {
70+
staker: string;
71+
oldSignerManager: string;
72+
} & TxParams
73+
): Promise<StacksTransactionWire>;
74+
```
75+
76+
The transaction fields come from [TxParams](../types/txparams.md).
77+
78+
***
79+
80+
### Returns
81+
82+
`Promise<StacksTransactionWire>`
83+
84+
Resolves to an unsigned [StacksTransactionWire](../../stacks-transactions/signing/StacksTransactionWire.md) that calls pox-5 `announce-l1-early-exit`. Sign it with [TransactionSigner](../../stacks-transactions/signing/TransactionSigner.md), then send it with [broadcastTransaction](../../stacks-transactions/network/broadcastTransaction.md).
85+
86+
***
87+
88+
### Parameters
89+
90+
#### args.staker (required)
91+
92+
* **Type**: `string`
93+
94+
Stacks address of the staker. It must be the address that signs the transaction.
95+
96+
#### args.oldSignerManager (required)
97+
98+
* **Type**: `string`
99+
100+
Contract principal of the signer-manager the membership is bound to. [fetchBondMembership](../fetch/fetchbondmembership.md) returns it as `signer`.
101+
102+
#### args.fee (required)
103+
104+
* **Type**: `IntegerType`
105+
106+
Transaction fee in micro-STX.
107+
108+
#### args.nonce (required)
109+
110+
* **Type**: `IntegerType`
111+
112+
Nonce of the origin account. Read it with [fetchNonce](../../stacks-transactions/network/fetchNonce.md).
113+
114+
#### args.network (required)
115+
116+
* **Type**: `StacksNetworkName | StacksNetwork`
117+
118+
A name such as `'mainnet'` or `'testnet'`, or a [StacksNetwork](../../stacks-network/network/StacksNetwork.md) object. Sets the contract address (the network's boot address, `SP000000000000000000002Q6VF78` on mainnet) and the transaction version and chain ID.
119+
120+
#### args.publicKey (required for a single-sig origin)
121+
122+
* **Type**: `string`
123+
124+
Hex public key of the account that signs the transaction.
125+
126+
#### args.publicKeys and args.numSignatures (required for a multisig origin)
127+
128+
* **Type**: `PublicKey[]` and `number`
129+
130+
Pass these instead of `publicKey` to build for an M-of-N multisig origin. This package defaults `useNonSequentialMultiSig` to `true`; pass `false` for the sequential hash mode. Pass `address` to check the key order against the multisig address.
131+
132+
#### args.postConditions (optional)
133+
134+
* **Type**: `PostCondition[]`
135+
136+
[Post conditions](../../stacks-transactions/types/PostCondition.md) to attach. The builder adds none.
137+
138+
#### args.postConditionMode (optional)
139+
140+
* **Type**: `PostConditionModeName`
141+
142+
`'allow'`, `'deny'` or `'originator'`. Defaults to `'deny'`.

0 commit comments

Comments
 (0)