@stacks/network 7.6.0 → 7.6.1-pr.1880.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -85
- package/dist/umd/index.js +149 -151
- package/dist/umd/index.js.map +1 -1
- package/package.json +2 -4
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# @stacks/network
|
|
2
2
|
|
|
3
|
-
Network
|
|
3
|
+
Network configuration for Stacks.js.
|
|
4
|
+
|
|
5
|
+
This package defines which chain and which node the other `@stacks/*` packages talk to. A network is a plain data object. It is not a class. It holds constants (chain id, transaction version, address versions) and a static client instance with the node URL.
|
|
6
|
+
|
|
7
|
+
The network is a plain object on purpose. You can serialize it, copy it, and override single fields.
|
|
4
8
|
|
|
5
9
|
## Installation
|
|
6
10
|
|
|
@@ -10,119 +14,101 @@ npm install @stacks/network
|
|
|
10
14
|
|
|
11
15
|
## Usage
|
|
12
16
|
|
|
13
|
-
###
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { StacksMainnet, StacksTestnet, StacksMocknet } from '@stacks/network';
|
|
17
|
+
### Use a network
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
In most cases the string name is enough. Every function that accepts a network accepts `'mainnet'`, `'testnet'`, or `'devnet'`.
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
```typescript
|
|
22
|
+
import { makeSTXTokenTransfer } from '@stacks/transactions';
|
|
21
23
|
|
|
22
|
-
const
|
|
24
|
+
const tx = await makeSTXTokenTransfer({
|
|
25
|
+
// ...
|
|
26
|
+
network: 'testnet',
|
|
27
|
+
});
|
|
23
28
|
```
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
The exported constants hold the full network objects. Use them when you want the object itself.
|
|
26
31
|
|
|
27
32
|
```typescript
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Check if network is mainnet
|
|
33
|
+
import { STACKS_MAINNET, STACKS_TESTNET, STACKS_DEVNET } from '@stacks/network';
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
const tx = await makeSTXTokenTransfer({
|
|
36
|
+
// ...
|
|
37
|
+
network: STACKS_TESTNET,
|
|
38
|
+
});
|
|
35
39
|
```
|
|
36
40
|
|
|
37
|
-
###
|
|
41
|
+
### Customize a network
|
|
42
|
+
|
|
43
|
+
Use `createNetwork` to set an API key or a custom node URL.
|
|
38
44
|
|
|
39
45
|
```typescript
|
|
40
|
-
import {
|
|
46
|
+
import { createNetwork } from '@stacks/network';
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
recipient: 'SP2BS6HD7TN34V8Z5BNF8Q2AW3K8K2DPV4264CF26',
|
|
45
|
-
amount: new BigNum(12345),
|
|
46
|
-
senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
|
|
47
|
-
};
|
|
48
|
+
// Network name and API key
|
|
49
|
+
const network = createNetwork('mainnet', 'my-api-key');
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
// Options object
|
|
52
|
+
const network2 = createNetwork({ network: 'testnet', apiKey: 'my-api-key' });
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
// Custom node URL
|
|
55
|
+
const network3 = createNetwork({
|
|
56
|
+
network: 'mainnet',
|
|
57
|
+
client: { baseUrl: 'https://custom-api.example.com' },
|
|
58
|
+
});
|
|
59
|
+
```
|
|
53
60
|
|
|
54
|
-
|
|
61
|
+
`createNetwork` copies the base network. It does not mutate `STACKS_MAINNET` or the other constants.
|
|
55
62
|
|
|
56
|
-
|
|
57
|
-
import { createApiKeyMiddleware, createFetchFn, StacksMainnet } from '@stacks/network';
|
|
58
|
-
import { broadcastTransaction, getNonce, makeSTXTokenTransfer } from '@stacks/transactions';
|
|
59
|
-
|
|
60
|
-
const myApiMiddleware = createApiKeyMiddleware('example_e8e044a3_41d8b0fe_3dd3988ef302');
|
|
61
|
-
const myFetchFn = createFetchFn(myApiMiddleware); // middlewares can be used to create a new fetch function
|
|
62
|
-
const myMainnet = new StacksMainnet({ fetchFn: myFetchFn }); // the fetchFn options can be passed to a StacksNetwork to override the default fetch function
|
|
63
|
-
|
|
64
|
-
const txOptions = {
|
|
65
|
-
recipient: 'SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159',
|
|
66
|
-
amount: 12345n,
|
|
67
|
-
senderKey: 'b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01',
|
|
68
|
-
memo: 'some memo',
|
|
69
|
-
anchorMode: AnchorMode.Any,
|
|
70
|
-
network: myMainnet, // make sure to pass in the custom network object
|
|
71
|
-
};
|
|
72
|
-
const transaction = await makeSTXTokenTransfer(txOptions); // fee-estimation will use the custom fetchFn
|
|
73
|
-
|
|
74
|
-
const response = await broadcastTransaction(transaction, myMainnet); // make sure to broadcast via the custom network object
|
|
75
|
-
|
|
76
|
-
// stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
|
|
77
|
-
const nonce = await getNonce('SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159', myMainnet);
|
|
78
|
-
```
|
|
63
|
+
The API key is sent as an `x-api-key` header. By default, the header is only sent to Hiro API hosts.
|
|
79
64
|
|
|
80
|
-
###
|
|
65
|
+
### The network and client options
|
|
81
66
|
|
|
82
|
-
|
|
67
|
+
Functions in other packages accept the network and the client as separate options. The network selects the chain. The client selects the node URL and the fetch function.
|
|
83
68
|
|
|
84
69
|
```typescript
|
|
85
|
-
import {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
};
|
|
92
|
-
const postMiddleware = (ctx: ResponseContext) => {
|
|
93
|
-
console.log(await ctx.response.json()); // log response body as json
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const fetchFn = createFetchFn({ pre: preMiddleware, post: preMiddleware }); // a middleware can contain `pre`, `post`, or both
|
|
97
|
-
const network = new StacksTestnet({ fetchFn });
|
|
98
|
-
|
|
99
|
-
// stacks.js functions, which take a StacksNetwork object will use the custom fetchFn
|
|
100
|
-
const nonce = await getNonce('SP3FGQ8Z7JY9BWYZ5WM53E0M9NK7WHJF0691NZ159', network);
|
|
70
|
+
import { broadcastTransaction } from '@stacks/transactions';
|
|
71
|
+
|
|
72
|
+
await broadcastTransaction({
|
|
73
|
+
transaction,
|
|
74
|
+
network: 'mainnet',
|
|
75
|
+
client: { baseUrl: 'https://custom-api.example.com' }, // optional override
|
|
76
|
+
});
|
|
101
77
|
```
|
|
102
78
|
|
|
103
|
-
|
|
79
|
+
The network's own `client` is used by default. A `client` option overrides it, field by field.
|
|
104
80
|
|
|
105
|
-
|
|
106
|
-
const txBroadcastUrl = network.getBroadcastApiUrl();
|
|
81
|
+
The `client` object has two optional fields:
|
|
107
82
|
|
|
108
|
-
|
|
83
|
+
- `baseUrl` — the node URL.
|
|
84
|
+
- `fetch` — a custom [fetch-compatible](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) function.
|
|
109
85
|
|
|
110
|
-
|
|
111
|
-
const accountInfoUrl = network.getAccountApiUrl(address);
|
|
86
|
+
`fetch` is the only field of a network that is not serializable. It is also the extension point: bake middleware (authentication, retries, logging) into your `fetch` function.
|
|
112
87
|
|
|
113
|
-
|
|
114
|
-
const abiUrl = network.getAbiApiUrl(address, contractName);
|
|
88
|
+
On the exported constants, `client.fetch` is undefined. The consuming function creates a default fetch function when it needs one.
|
|
115
89
|
|
|
116
|
-
|
|
117
|
-
const readOnlyFunctionCallUrl = network.getReadOnlyFunctionCallApiUrl(
|
|
118
|
-
address,
|
|
119
|
-
contractName,
|
|
120
|
-
functionName
|
|
121
|
-
);
|
|
90
|
+
## The network object
|
|
122
91
|
|
|
123
|
-
|
|
92
|
+
A `StacksNetwork` object has this shape:
|
|
124
93
|
|
|
125
|
-
|
|
94
|
+
```typescript
|
|
95
|
+
interface StacksNetwork {
|
|
96
|
+
chainId: number;
|
|
97
|
+
transactionVersion: number;
|
|
98
|
+
peerNetworkId: number;
|
|
99
|
+
magicBytes: string;
|
|
100
|
+
bootAddress: string;
|
|
101
|
+
addressVersion: { singleSig: number; multiSig: number };
|
|
102
|
+
client: { baseUrl: string; fetch?: FetchFn };
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The constants `ChainId`, `TransactionVersion`, and `AddressVersion` are exported for code that reads these fields.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { AddressVersion, ChainId, TransactionVersion } from '@stacks/network';
|
|
126
110
|
|
|
127
|
-
|
|
111
|
+
ChainId.Mainnet; // 0x00000001
|
|
112
|
+
TransactionVersion.Mainnet; // 0x00
|
|
113
|
+
AddressVersion.MainnetSingleSig; // 22
|
|
128
114
|
```
|
package/dist/umd/index.js
CHANGED
|
@@ -12,131 +12,11 @@ return /******/ (() => { // webpackBootstrap
|
|
|
12
12
|
/******/ "use strict";
|
|
13
13
|
/******/ var __webpack_modules__ = ({
|
|
14
14
|
|
|
15
|
-
/***/ "
|
|
16
|
-
/*!***************************************!*\
|
|
17
|
-
!*** ../common/dist/esm/constants.js ***!
|
|
18
|
-
\***************************************/
|
|
19
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
20
|
-
|
|
21
|
-
__webpack_require__.r(__webpack_exports__);
|
|
22
|
-
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
23
|
-
/* harmony export */ DEVNET_URL: () => (/* binding */ DEVNET_URL),
|
|
24
|
-
/* harmony export */ GAIA_URL: () => (/* binding */ GAIA_URL),
|
|
25
|
-
/* harmony export */ HIRO_MAINNET_URL: () => (/* binding */ HIRO_MAINNET_URL),
|
|
26
|
-
/* harmony export */ HIRO_TESTNET_URL: () => (/* binding */ HIRO_TESTNET_URL),
|
|
27
|
-
/* harmony export */ PRIVATE_KEY_BYTES_COMPRESSED: () => (/* binding */ PRIVATE_KEY_BYTES_COMPRESSED),
|
|
28
|
-
/* harmony export */ PRIVATE_KEY_BYTES_UNCOMPRESSED: () => (/* binding */ PRIVATE_KEY_BYTES_UNCOMPRESSED)
|
|
29
|
-
/* harmony export */ });
|
|
30
|
-
const HIRO_MAINNET_URL = 'https://api.mainnet.hiro.so';
|
|
31
|
-
const HIRO_TESTNET_URL = 'https://api.testnet.hiro.so';
|
|
32
|
-
const DEVNET_URL = 'http://localhost:3999';
|
|
33
|
-
const GAIA_URL = 'https://hub.blockstack.org';
|
|
34
|
-
const PRIVATE_KEY_BYTES_COMPRESSED = 33;
|
|
35
|
-
const PRIVATE_KEY_BYTES_UNCOMPRESSED = 32;
|
|
36
|
-
//# sourceMappingURL=constants.js.map
|
|
37
|
-
|
|
38
|
-
/***/ }),
|
|
39
|
-
|
|
40
|
-
/***/ "../common/dist/esm/fetch.js":
|
|
41
|
-
/*!***********************************!*\
|
|
42
|
-
!*** ../common/dist/esm/fetch.js ***!
|
|
43
|
-
\***********************************/
|
|
44
|
-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
45
|
-
|
|
46
|
-
__webpack_require__.r(__webpack_exports__);
|
|
47
|
-
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
48
|
-
/* harmony export */ createApiKeyMiddleware: () => (/* binding */ createApiKeyMiddleware),
|
|
49
|
-
/* harmony export */ createFetchFn: () => (/* binding */ createFetchFn),
|
|
50
|
-
/* harmony export */ fetchWrapper: () => (/* binding */ fetchWrapper),
|
|
51
|
-
/* harmony export */ getFetchOptions: () => (/* binding */ getFetchOptions),
|
|
52
|
-
/* harmony export */ hostMatches: () => (/* binding */ hostMatches),
|
|
53
|
-
/* harmony export */ setFetchOptions: () => (/* binding */ setFetchOptions)
|
|
54
|
-
/* harmony export */ });
|
|
55
|
-
const defaultFetchOpts = {
|
|
56
|
-
referrerPolicy: 'origin',
|
|
57
|
-
headers: {
|
|
58
|
-
'x-hiro-product': 'stacksjs',
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
const getFetchOptions = () => {
|
|
62
|
-
return defaultFetchOpts;
|
|
63
|
-
};
|
|
64
|
-
const setFetchOptions = (ops) => {
|
|
65
|
-
return Object.assign(defaultFetchOpts, ops);
|
|
66
|
-
};
|
|
67
|
-
async function fetchWrapper(input, init) {
|
|
68
|
-
const fetchOpts = {};
|
|
69
|
-
Object.assign(fetchOpts, defaultFetchOpts, init);
|
|
70
|
-
const fetchResult = await fetch(input, fetchOpts);
|
|
71
|
-
return fetchResult;
|
|
72
|
-
}
|
|
73
|
-
function hostMatches(host, pattern) {
|
|
74
|
-
if (typeof pattern === 'string')
|
|
75
|
-
return pattern === host;
|
|
76
|
-
return pattern.exec(host);
|
|
77
|
-
}
|
|
78
|
-
function createApiKeyMiddleware({ apiKey, host = /(.*)api(.*)(\.stacks\.co|\.hiro\.so)$/i, httpHeader = 'x-api-key', }) {
|
|
79
|
-
return {
|
|
80
|
-
pre: context => {
|
|
81
|
-
const reqUrl = new URL(context.url);
|
|
82
|
-
if (!hostMatches(reqUrl.host, host))
|
|
83
|
-
return;
|
|
84
|
-
const headers = context.init.headers instanceof Headers
|
|
85
|
-
? context.init.headers
|
|
86
|
-
: (context.init.headers = new Headers(context.init.headers));
|
|
87
|
-
headers.set(httpHeader, apiKey);
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
function argsForCreateFetchFn(args) {
|
|
92
|
-
let fetchLib = fetchWrapper;
|
|
93
|
-
let middlewares = [];
|
|
94
|
-
if (args.length > 0 && typeof args[0] === 'function') {
|
|
95
|
-
fetchLib = args.shift();
|
|
96
|
-
}
|
|
97
|
-
if (args.length > 0) {
|
|
98
|
-
middlewares = args;
|
|
99
|
-
}
|
|
100
|
-
return { fetchLib, middlewares };
|
|
101
|
-
}
|
|
102
|
-
function createFetchFn(...args) {
|
|
103
|
-
const { fetchLib, middlewares } = argsForCreateFetchFn(args);
|
|
104
|
-
const fetchFn = async (url, init) => {
|
|
105
|
-
let fetchParams = { url, init: init ?? {} };
|
|
106
|
-
for (const middleware of middlewares) {
|
|
107
|
-
if (typeof middleware.pre === 'function') {
|
|
108
|
-
const result = await Promise.resolve(middleware.pre({
|
|
109
|
-
fetch: fetchLib,
|
|
110
|
-
...fetchParams,
|
|
111
|
-
}));
|
|
112
|
-
fetchParams = result ?? fetchParams;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
let response = await fetchLib(fetchParams.url, fetchParams.init);
|
|
116
|
-
for (const middleware of middlewares) {
|
|
117
|
-
if (typeof middleware.post === 'function') {
|
|
118
|
-
const result = await Promise.resolve(middleware.post({
|
|
119
|
-
fetch: fetchLib,
|
|
120
|
-
url: fetchParams.url,
|
|
121
|
-
init: fetchParams.init,
|
|
122
|
-
response: response?.clone() ?? response,
|
|
123
|
-
}));
|
|
124
|
-
response = result ?? response;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
return response;
|
|
128
|
-
};
|
|
129
|
-
return fetchFn;
|
|
130
|
-
}
|
|
131
|
-
//# sourceMappingURL=fetch.js.map
|
|
132
|
-
|
|
133
|
-
/***/ }),
|
|
134
|
-
|
|
135
|
-
/***/ "./src/constants.ts":
|
|
15
|
+
/***/ "./src/constants.ts"
|
|
136
16
|
/*!**************************!*\
|
|
137
17
|
!*** ./src/constants.ts ***!
|
|
138
18
|
\**************************/
|
|
139
|
-
|
|
19
|
+
(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
140
20
|
|
|
141
21
|
__webpack_require__.r(__webpack_exports__);
|
|
142
22
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
@@ -178,13 +58,13 @@ function whenTransactionVersion(transactionVersion) {
|
|
|
178
58
|
}
|
|
179
59
|
|
|
180
60
|
|
|
181
|
-
/***/ }
|
|
61
|
+
/***/ },
|
|
182
62
|
|
|
183
|
-
/***/ "./src/network.ts"
|
|
63
|
+
/***/ "./src/network.ts"
|
|
184
64
|
/*!************************!*\
|
|
185
65
|
!*** ./src/network.ts ***!
|
|
186
66
|
\************************/
|
|
187
|
-
|
|
67
|
+
(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
188
68
|
|
|
189
69
|
__webpack_require__.r(__webpack_exports__);
|
|
190
70
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
@@ -325,28 +205,154 @@ function createNetwork(arg1, arg2) {
|
|
|
325
205
|
}
|
|
326
206
|
|
|
327
207
|
|
|
328
|
-
/***/ }
|
|
208
|
+
/***/ },
|
|
209
|
+
|
|
210
|
+
/***/ "../common/dist/esm/constants.js"
|
|
211
|
+
/*!***************************************!*\
|
|
212
|
+
!*** ../common/dist/esm/constants.js ***!
|
|
213
|
+
\***************************************/
|
|
214
|
+
(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
215
|
+
|
|
216
|
+
__webpack_require__.r(__webpack_exports__);
|
|
217
|
+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
218
|
+
/* harmony export */ DEVNET_URL: () => (/* binding */ DEVNET_URL),
|
|
219
|
+
/* harmony export */ GAIA_URL: () => (/* binding */ GAIA_URL),
|
|
220
|
+
/* harmony export */ HIRO_MAINNET_URL: () => (/* binding */ HIRO_MAINNET_URL),
|
|
221
|
+
/* harmony export */ HIRO_TESTNET_URL: () => (/* binding */ HIRO_TESTNET_URL),
|
|
222
|
+
/* harmony export */ PRIVATE_KEY_BYTES_COMPRESSED: () => (/* binding */ PRIVATE_KEY_BYTES_COMPRESSED),
|
|
223
|
+
/* harmony export */ PRIVATE_KEY_BYTES_UNCOMPRESSED: () => (/* binding */ PRIVATE_KEY_BYTES_UNCOMPRESSED)
|
|
224
|
+
/* harmony export */ });
|
|
225
|
+
const HIRO_MAINNET_URL = 'https://api.mainnet.hiro.so';
|
|
226
|
+
const HIRO_TESTNET_URL = 'https://api.testnet.hiro.so';
|
|
227
|
+
const DEVNET_URL = 'http://localhost:3999';
|
|
228
|
+
const GAIA_URL = 'https://hub.blockstack.org';
|
|
229
|
+
const PRIVATE_KEY_BYTES_COMPRESSED = 33;
|
|
230
|
+
const PRIVATE_KEY_BYTES_UNCOMPRESSED = 32;
|
|
231
|
+
//# sourceMappingURL=constants.js.map
|
|
232
|
+
|
|
233
|
+
/***/ },
|
|
234
|
+
|
|
235
|
+
/***/ "../common/dist/esm/fetch.js"
|
|
236
|
+
/*!***********************************!*\
|
|
237
|
+
!*** ../common/dist/esm/fetch.js ***!
|
|
238
|
+
\***********************************/
|
|
239
|
+
(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
240
|
+
|
|
241
|
+
__webpack_require__.r(__webpack_exports__);
|
|
242
|
+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
243
|
+
/* harmony export */ createApiKeyMiddleware: () => (/* binding */ createApiKeyMiddleware),
|
|
244
|
+
/* harmony export */ createFetchFn: () => (/* binding */ createFetchFn),
|
|
245
|
+
/* harmony export */ fetchWrapper: () => (/* binding */ fetchWrapper),
|
|
246
|
+
/* harmony export */ getFetchOptions: () => (/* binding */ getFetchOptions),
|
|
247
|
+
/* harmony export */ hostMatches: () => (/* binding */ hostMatches),
|
|
248
|
+
/* harmony export */ setFetchOptions: () => (/* binding */ setFetchOptions)
|
|
249
|
+
/* harmony export */ });
|
|
250
|
+
const defaultFetchOpts = {
|
|
251
|
+
referrerPolicy: 'origin',
|
|
252
|
+
headers: {
|
|
253
|
+
'x-hiro-product': 'stacksjs',
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
const getFetchOptions = () => {
|
|
257
|
+
return defaultFetchOpts;
|
|
258
|
+
};
|
|
259
|
+
const setFetchOptions = (ops) => {
|
|
260
|
+
return Object.assign(defaultFetchOpts, ops);
|
|
261
|
+
};
|
|
262
|
+
async function fetchWrapper(input, init) {
|
|
263
|
+
const fetchOpts = {};
|
|
264
|
+
Object.assign(fetchOpts, defaultFetchOpts, init);
|
|
265
|
+
const fetchResult = await fetch(input, fetchOpts);
|
|
266
|
+
return fetchResult;
|
|
267
|
+
}
|
|
268
|
+
function hostMatches(host, pattern) {
|
|
269
|
+
if (typeof pattern === 'string')
|
|
270
|
+
return pattern === host;
|
|
271
|
+
return pattern.exec(host);
|
|
272
|
+
}
|
|
273
|
+
function createApiKeyMiddleware({ apiKey, host = /(.*)api(.*)(\.stacks\.co|\.hiro\.so)$/i, httpHeader = 'x-api-key', }) {
|
|
274
|
+
return {
|
|
275
|
+
pre: context => {
|
|
276
|
+
const reqUrl = new URL(context.url);
|
|
277
|
+
if (!hostMatches(reqUrl.host, host))
|
|
278
|
+
return;
|
|
279
|
+
const headers = context.init.headers instanceof Headers
|
|
280
|
+
? context.init.headers
|
|
281
|
+
: (context.init.headers = new Headers(context.init.headers));
|
|
282
|
+
headers.set(httpHeader, apiKey);
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function argsForCreateFetchFn(args) {
|
|
287
|
+
let fetchLib = fetchWrapper;
|
|
288
|
+
let middlewares = [];
|
|
289
|
+
if (args.length > 0 && typeof args[0] === 'function') {
|
|
290
|
+
fetchLib = args.shift();
|
|
291
|
+
}
|
|
292
|
+
if (args.length > 0) {
|
|
293
|
+
middlewares = args;
|
|
294
|
+
}
|
|
295
|
+
return { fetchLib, middlewares };
|
|
296
|
+
}
|
|
297
|
+
function createFetchFn(...args) {
|
|
298
|
+
const { fetchLib, middlewares } = argsForCreateFetchFn(args);
|
|
299
|
+
const fetchFn = async (url, init) => {
|
|
300
|
+
let fetchParams = { url, init: init ?? {} };
|
|
301
|
+
for (const middleware of middlewares) {
|
|
302
|
+
if (typeof middleware.pre === 'function') {
|
|
303
|
+
const result = await Promise.resolve(middleware.pre({
|
|
304
|
+
fetch: fetchLib,
|
|
305
|
+
...fetchParams,
|
|
306
|
+
}));
|
|
307
|
+
fetchParams = result ?? fetchParams;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
let response = await fetchLib(fetchParams.url, fetchParams.init);
|
|
311
|
+
for (const middleware of middlewares) {
|
|
312
|
+
if (typeof middleware.post === 'function') {
|
|
313
|
+
const result = await Promise.resolve(middleware.post({
|
|
314
|
+
fetch: fetchLib,
|
|
315
|
+
url: fetchParams.url,
|
|
316
|
+
init: fetchParams.init,
|
|
317
|
+
response: response?.clone() ?? response,
|
|
318
|
+
}));
|
|
319
|
+
response = result ?? response;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return response;
|
|
323
|
+
};
|
|
324
|
+
return fetchFn;
|
|
325
|
+
}
|
|
326
|
+
//# sourceMappingURL=fetch.js.map
|
|
327
|
+
|
|
328
|
+
/***/ }
|
|
329
329
|
|
|
330
330
|
/******/ });
|
|
331
331
|
/************************************************************************/
|
|
332
332
|
/******/ // The module cache
|
|
333
|
-
/******/
|
|
333
|
+
/******/ const __webpack_module_cache__ = {};
|
|
334
334
|
/******/
|
|
335
335
|
/******/ // The require function
|
|
336
336
|
/******/ function __webpack_require__(moduleId) {
|
|
337
337
|
/******/ // Check if module is in cache
|
|
338
|
-
/******/
|
|
338
|
+
/******/ const cachedModule = __webpack_module_cache__[moduleId];
|
|
339
339
|
/******/ if (cachedModule !== undefined) {
|
|
340
340
|
/******/ return cachedModule.exports;
|
|
341
341
|
/******/ }
|
|
342
342
|
/******/ // Create a new module (and put it into the cache)
|
|
343
|
-
/******/
|
|
343
|
+
/******/ const module = __webpack_module_cache__[moduleId] = {
|
|
344
344
|
/******/ // no module.id needed
|
|
345
345
|
/******/ // no module.loaded needed
|
|
346
346
|
/******/ exports: {}
|
|
347
347
|
/******/ };
|
|
348
348
|
/******/
|
|
349
349
|
/******/ // Execute the module function
|
|
350
|
+
/******/ if (!(moduleId in __webpack_modules__)) {
|
|
351
|
+
/******/ delete __webpack_module_cache__[moduleId];
|
|
352
|
+
/******/ const e = new Error("Cannot find module '" + moduleId + "'");
|
|
353
|
+
/******/ e.code = 'MODULE_NOT_FOUND';
|
|
354
|
+
/******/ throw e;
|
|
355
|
+
/******/ }
|
|
350
356
|
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
|
|
351
357
|
/******/
|
|
352
358
|
/******/ // Return the exports of the module
|
|
@@ -355,35 +361,27 @@ function createNetwork(arg1, arg2) {
|
|
|
355
361
|
/******/
|
|
356
362
|
/************************************************************************/
|
|
357
363
|
/******/ /* webpack/runtime/define property getters */
|
|
358
|
-
/******/
|
|
359
|
-
/******/
|
|
360
|
-
/******/
|
|
361
|
-
/******/
|
|
362
|
-
/******/
|
|
363
|
-
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
364
|
-
/******/ }
|
|
364
|
+
/******/ // define getter/value functions for harmony exports
|
|
365
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
366
|
+
/******/ for(var key in definition) {
|
|
367
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
368
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
365
369
|
/******/ }
|
|
366
|
-
/******/ }
|
|
367
|
-
/******/ }
|
|
370
|
+
/******/ }
|
|
371
|
+
/******/ };
|
|
368
372
|
/******/
|
|
369
373
|
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
370
|
-
/******/ (
|
|
371
|
-
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
372
|
-
/******/ })();
|
|
374
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop));
|
|
373
375
|
/******/
|
|
374
376
|
/******/ /* webpack/runtime/make namespace object */
|
|
375
|
-
/******/
|
|
376
|
-
/******/
|
|
377
|
-
/******/
|
|
378
|
-
/******/
|
|
379
|
-
/******/
|
|
380
|
-
/******/ }
|
|
381
|
-
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
382
|
-
/******/ };
|
|
383
|
-
/******/ })();
|
|
377
|
+
/******/ // define __esModule on exports
|
|
378
|
+
/******/ __webpack_require__.r = (exports) => {
|
|
379
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
380
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
381
|
+
/******/ };
|
|
384
382
|
/******/
|
|
385
383
|
/************************************************************************/
|
|
386
|
-
|
|
384
|
+
let __webpack_exports__ = {};
|
|
387
385
|
// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk.
|
|
388
386
|
(() => {
|
|
389
387
|
/*!**********************!*\
|
package/dist/umd/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;;;;;;;;;;;;;;;;;;ACVO;AACA;AACA;AACA;AACA;AACA;AACP,qC;;;;;;;;;;;;;;;;;;;ACNA;AACA;AACA;AACA;AACA,KAAK;AACL;AACO;AACP;AACA;AACO;AACP;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACO,kCAAkC,oFAAoF;AAC7H;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACO;AACP,YAAY,wBAAwB;AACpC;AACA,4BAA4B;AAC5B;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iC;;;;;;;;;;;;;;;;;;;;;ACxEO,IAAK,UAAL,kBAAKA,aAAL;AACL,EAAAA,kBAAA,aAAU,KAAV;AACA,EAAAA,kBAAA,aAAU,cAAV;AAFU,SAAAA;AAAA;AAeL,IAAK,gBAAL,kBAAKC,mBAAL;AACL,EAAAA,8BAAA,aAAU,aAAV;AACA,EAAAA,8BAAA,aAAU,cAAV;AAFU,SAAAA;AAAA;AAKL,MAAM,mB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,UAAK,OAAO,YAAZ,YAAuB,WAAW,OAAO;AACrE,iBAAW,OAAO,SAAQ,UAAK,OAAO,UAAZ,YAAqB,WAAW,OAAO;AAAA,IACnE;AAEA,QAAI,OAAO,KAAK,WAAW,UAAU;AACnC,YAAM,aAAa,sEAAsB,CAAC,IAA4B;AACtE,iBAAW,OAAO,QAAQ,WAAW,OAAO,QACxC,6DAAa,CAAC,WAAW,OAAO,OAAO,UAAU,IACjD,6DAAa,CAAC,UAAU;AAAA,IAC9B;AAEA,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,SAAS,UAAU;AAC5B,UAAM,aAAa,sEAAsB,CAAC,EAAE,QAAQ,KAAK,CAAC;AAC1D,eAAW,OAAO,QAAQ,WAAW,OAAO,QACxC,6DAAa,CAAC,WAAW,OAAO,OAAO,UAAU,IACjD,6DAAa,CAAC,UAAU;AAC5B,WAAO;AAAA,EACT;AAGA,SAAO;AACT;;;;;;;UCvPA;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;;UAEA;UACA;UACA;;;;;WCtBA;WACA;WACA;WACA;WACA,yCAAyC,wCAAwC;WACjF;WACA;WACA,E;;;;;WCPA,wF;;;;;WCAA;WACA;WACA;WACA,uDAAuD,iBAAiB;WACxE;WACA,gDAAgD,aAAa;WAC7D,E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACNc;AACA","sources":["webpack://StacksNetwork/webpack/universalModuleDefinition","webpack://StacksNetwork/../common/dist/esm/constants.js","webpack://StacksNetwork/../common/dist/esm/fetch.js","webpack://StacksNetwork/./src/constants.ts","webpack://StacksNetwork/./src/network.ts","webpack://StacksNetwork/webpack/bootstrap","webpack://StacksNetwork/webpack/runtime/define property getters","webpack://StacksNetwork/webpack/runtime/hasOwnProperty shorthand","webpack://StacksNetwork/webpack/runtime/make namespace object","webpack://StacksNetwork/./src/index.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"StacksNetwork\"] = factory();\n\telse\n\t\troot[\"StacksNetwork\"] = factory();\n})(this, () => {\nreturn ","export const HIRO_MAINNET_URL = 'https://api.mainnet.hiro.so';\nexport const HIRO_TESTNET_URL = 'https://api.testnet.hiro.so';\nexport const DEVNET_URL = 'http://localhost:3999';\nexport const GAIA_URL = 'https://hub.blockstack.org';\nexport const PRIVATE_KEY_BYTES_COMPRESSED = 33;\nexport const PRIVATE_KEY_BYTES_UNCOMPRESSED = 32;\n//# sourceMappingURL=constants.js.map","const defaultFetchOpts = {\n referrerPolicy: 'origin',\n headers: {\n 'x-hiro-product': 'stacksjs',\n },\n};\nexport const getFetchOptions = () => {\n return defaultFetchOpts;\n};\nexport const setFetchOptions = (ops) => {\n return Object.assign(defaultFetchOpts, ops);\n};\nexport async function fetchWrapper(input, init) {\n const fetchOpts = {};\n Object.assign(fetchOpts, defaultFetchOpts, init);\n const fetchResult = await fetch(input, fetchOpts);\n return fetchResult;\n}\nexport function hostMatches(host, pattern) {\n if (typeof pattern === 'string')\n return pattern === host;\n return pattern.exec(host);\n}\nexport function createApiKeyMiddleware({ apiKey, host = /(.*)api(.*)(\\.stacks\\.co|\\.hiro\\.so)$/i, httpHeader = 'x-api-key', }) {\n return {\n pre: context => {\n const reqUrl = new URL(context.url);\n if (!hostMatches(reqUrl.host, host))\n return;\n const headers = context.init.headers instanceof Headers\n ? context.init.headers\n : (context.init.headers = new Headers(context.init.headers));\n headers.set(httpHeader, apiKey);\n },\n };\n}\nfunction argsForCreateFetchFn(args) {\n let fetchLib = fetchWrapper;\n let middlewares = [];\n if (args.length > 0 && typeof args[0] === 'function') {\n fetchLib = args.shift();\n }\n if (args.length > 0) {\n middlewares = args;\n }\n return { fetchLib, middlewares };\n}\nexport function createFetchFn(...args) {\n const { fetchLib, middlewares } = argsForCreateFetchFn(args);\n const fetchFn = async (url, init) => {\n let fetchParams = { url, init: init ?? {} };\n for (const middleware of middlewares) {\n if (typeof middleware.pre === 'function') {\n const result = await Promise.resolve(middleware.pre({\n fetch: fetchLib,\n ...fetchParams,\n }));\n fetchParams = result ?? fetchParams;\n }\n }\n let response = await fetchLib(fetchParams.url, fetchParams.init);\n for (const middleware of middlewares) {\n if (typeof middleware.post === 'function') {\n const result = await Promise.resolve(middleware.post({\n fetch: fetchLib,\n url: fetchParams.url,\n init: fetchParams.init,\n response: response?.clone() ?? response,\n }));\n response = result ?? response;\n }\n }\n return response;\n };\n return fetchFn;\n}\n//# sourceMappingURL=fetch.js.map","/**\n * The chain ID (unsigned 32-bit integer), used so transactions can't be replayed on other chains.\n * Similar to the {@link TransactionVersion}.\n */\nexport enum ChainId {\n Mainnet = 0x00000001,\n Testnet = 0x80000000,\n}\n\n/**\n * The **peer** network ID.\n * Typically not used in signing, but used for broadcasting to the P2P network.\n * It can also be used to determine the parent of a subnet.\n *\n * **Attention:**\n * For mainnet/testnet the v2/info response `.network_id` refers to the chain ID.\n * For subnets the v2/info response `.network_id` refers to the peer network ID and the chain ID (they are the same for subnets).\n * The `.parent_network_id` refers to the actual peer network ID (of the parent) in both cases.\n */\nexport enum PeerNetworkId {\n Mainnet = 0x17000000,\n Testnet = 0xff000000,\n}\n\nexport const DEFAULT_CHAIN_ID = ChainId.Mainnet;\n\n/**\n * The transaction version, used so transactions can't be replayed on other networks.\n * Similar to the {@link ChainId}.\n * Used internally for serializing and deserializing transactions.\n */\nexport enum TransactionVersion {\n Mainnet = 0x00,\n Testnet = 0x80,\n}\n\n/**\n * Address versions for identifying address types in an encoded Stacks address.\n * The address version is a single byte, indicating the address type.\n * Every Stacks address starts with `S` followed by a single character indicating the address version.\n * The second character is the c32-encoded AddressVersion byte.\n */\nexport enum AddressVersion {\n /** `P` — A single-sig address for mainnet (starting with `SP`) */\n MainnetSingleSig = 22,\n /** `M` — A multi-sig address for mainnet (starting with `SM`) */\n MainnetMultiSig = 20,\n /** `T` — A single-sig address for testnet (starting with `ST`) */\n TestnetSingleSig = 26,\n /** `N` — A multi-sig address for testnet (starting with `SN`) */\n TestnetMultiSig = 21,\n}\n\nexport const DEFAULT_TRANSACTION_VERSION = TransactionVersion.Mainnet;\n\n/** @ignore */\nexport function whenTransactionVersion(transactionVersion: TransactionVersion) {\n return <T>(map: Record<TransactionVersion, T>): T => map[transactionVersion];\n}\n","import {\n DEVNET_URL,\n FetchFn,\n HIRO_MAINNET_URL,\n HIRO_TESTNET_URL,\n createFetchFn,\n createApiKeyMiddleware,\n ClientOpts,\n ApiKeyMiddlewareOpts,\n} from '@stacks/common';\nimport { AddressVersion, ChainId, PeerNetworkId, TransactionVersion } from './constants';\nimport { ClientParam } from '@stacks/common';\n\nexport type StacksNetwork = {\n chainId: number;\n transactionVersion: number;\n peerNetworkId: number;\n magicBytes: string;\n bootAddress: string;\n addressVersion: {\n singleSig: number;\n multiSig: number;\n };\n // todo: add check32 character bytes string\n client: {\n baseUrl: string; // URL is always required\n fetch?: FetchFn; // fetch is optional and will be created by default in fetch helpers\n };\n};\n\nexport interface NetworkParam {\n network?: StacksNetworkName | StacksNetwork;\n}\n\nexport type NetworkClientParam = NetworkParam & ClientParam;\n\nexport const STACKS_MAINNET: StacksNetwork = {\n chainId: ChainId.Mainnet,\n transactionVersion: TransactionVersion.Mainnet,\n peerNetworkId: PeerNetworkId.Mainnet,\n magicBytes: 'X2', // todo: comment bytes version of magic bytes\n bootAddress: 'SP000000000000000000002Q6VF78',\n addressVersion: {\n singleSig: AddressVersion.MainnetSingleSig,\n multiSig: AddressVersion.MainnetMultiSig,\n },\n client: { baseUrl: HIRO_MAINNET_URL },\n};\n\nexport const STACKS_TESTNET: StacksNetwork = {\n chainId: ChainId.Testnet,\n transactionVersion: TransactionVersion.Testnet,\n peerNetworkId: PeerNetworkId.Testnet,\n magicBytes: 'T2', // todo: comment bytes version of magic bytes\n bootAddress: 'ST000000000000000000002AMW42H',\n addressVersion: {\n singleSig: AddressVersion.TestnetSingleSig,\n multiSig: AddressVersion.TestnetMultiSig,\n },\n client: { baseUrl: HIRO_TESTNET_URL },\n};\n\nexport const STACKS_DEVNET: StacksNetwork = {\n ...STACKS_TESTNET, // todo: ensure deep copy\n addressVersion: { ...STACKS_TESTNET.addressVersion }, // deep copy\n magicBytes: 'id', // todo: comment bytes version of magic bytes\n client: { baseUrl: DEVNET_URL },\n};\n\nexport const STACKS_MOCKNET: StacksNetwork = {\n ...STACKS_DEVNET,\n addressVersion: { ...STACKS_DEVNET.addressVersion }, // deep copy\n client: { ...STACKS_DEVNET.client }, // deep copy\n};\n\n/** @ignore internal */\nexport const StacksNetworks = ['mainnet', 'testnet', 'devnet', 'mocknet'] as const;\n/** The enum-style names of different common Stacks networks */\nexport type StacksNetworkName = (typeof StacksNetworks)[number];\n\n/**\n * Returns the default network for a given name\n * @example\n * ```ts\n * networkFromName('mainnet') // same as STACKS_MAINNET\n * networkFromName('testnet') // same as STACKS_TESTNET\n * networkFromName('devnet') // same as STACKS_DEVNET\n * networkFromName('mocknet') // same as STACKS_MOCKNET\n * ```\n */\nexport function networkFromName(name: StacksNetworkName) {\n switch (name) {\n case 'mainnet':\n return STACKS_MAINNET;\n case 'testnet':\n return STACKS_TESTNET;\n case 'devnet':\n return STACKS_DEVNET;\n case 'mocknet':\n return STACKS_MOCKNET;\n default:\n throw new Error(`Unknown network name: ${name}`);\n }\n}\n\n/** @ignore */\nexport function networkFrom(network: StacksNetworkName | StacksNetwork) {\n if (typeof network === 'string') return networkFromName(network);\n return network;\n}\n\n/** @ignore */\nexport function defaultUrlFromNetwork(network?: StacksNetworkName | StacksNetwork) {\n if (!network) return HIRO_MAINNET_URL; // default to mainnet if no network is given\n\n network = networkFrom(network);\n\n return !network || network.transactionVersion === TransactionVersion.Mainnet\n ? HIRO_MAINNET_URL // default to mainnet if txVersion is mainnet\n : network.magicBytes === 'id'\n ? DEVNET_URL // default to devnet if magicBytes are devnet\n : HIRO_TESTNET_URL;\n}\n\n/**\n * Returns the client of a network, creating a new fetch function if none is available\n */\nexport function clientFromNetwork(network: StacksNetwork): Required<ClientOpts> {\n if (network.client.fetch) return network.client as Required<ClientOpts>;\n return {\n ...network.client,\n fetch: createFetchFn(),\n };\n}\n\n/**\n * Creates a customized Stacks network.\n *\n * This function allows you to create a network based on a predefined network\n * (mainnet, testnet, devnet, mocknet) or a custom network object. You can also customize\n * the network with an API key or other client options.\n *\n * @example\n * ```ts\n * // Create a basic network from a network name\n * const network = createNetwork('mainnet');\n * const network = createNetwork(STACKS_MAINNET);\n * ```\n *\n * @example\n * ```ts\n * // Create a network with an API key\n * const network = createNetwork('testnet', 'my-api-key');\n * const network = createNetwork(STACKS_TESTNET, 'my-api-key');\n * ```\n *\n * @example\n * ```ts\n * // Create a network with options object\n * const network = createNetwork({\n * network: 'mainnet',\n * apiKey: 'my-api-key',\n * });\n * ```\n *\n * @example\n * ```ts\n * // Create a network with options object with custom API key options\n * const network = createNetwork({\n * network: 'mainnet',\n * apiKey: 'my-api-key',\n * host: /\\.example\\.com$/, // default is /(.*)api(.*)(\\.stacks\\.co|\\.hiro\\.so)$/i\n * httpHeader: 'x-custom-api-key', // default is 'x-api-key'\n * });\n * ```\n *\n * @example\n * ```ts\n * // Create a network with custom client options\n * const network = createNetwork({\n * network: STACKS_TESTNET,\n * client: {\n * baseUrl: 'https://custom-api.example.com',\n * fetch: customFetchFunction\n * }\n * });\n * ```\n */\nexport function createNetwork(network: StacksNetworkName | StacksNetwork): StacksNetwork;\nexport function createNetwork(\n network: StacksNetworkName | StacksNetwork,\n apiKey: string\n): StacksNetwork;\nexport function createNetwork(\n options: {\n network: StacksNetworkName | StacksNetwork;\n client?: ClientOpts;\n } & Partial<ApiKeyMiddlewareOpts>\n): StacksNetwork;\nexport function createNetwork(\n arg1:\n | StacksNetworkName\n | StacksNetwork\n | ({\n network: StacksNetworkName | StacksNetwork;\n client?: ClientOpts;\n } & Partial<ApiKeyMiddlewareOpts>),\n arg2?: string\n): StacksNetwork {\n const baseNetwork = networkFrom(\n typeof arg1 === 'object' && 'network' in arg1 ? arg1.network : arg1\n );\n\n const newNetwork: StacksNetwork = {\n ...baseNetwork,\n addressVersion: { ...baseNetwork.addressVersion }, // deep copy\n client: { ...baseNetwork.client }, // deep copy\n };\n\n // Options object argument\n if (typeof arg1 === 'object' && 'network' in arg1) {\n if (arg1.client) {\n newNetwork.client.baseUrl = arg1.client.baseUrl ?? newNetwork.client.baseUrl;\n newNetwork.client.fetch = arg1.client.fetch ?? newNetwork.client.fetch;\n }\n\n if (typeof arg1.apiKey === 'string') {\n const middleware = createApiKeyMiddleware(arg1 as ApiKeyMiddlewareOpts);\n newNetwork.client.fetch = newNetwork.client.fetch\n ? createFetchFn(newNetwork.client.fetch, middleware)\n : createFetchFn(middleware);\n }\n\n return newNetwork;\n }\n\n // Additional API key argument\n if (typeof arg2 === 'string') {\n const middleware = createApiKeyMiddleware({ apiKey: arg2 });\n newNetwork.client.fetch = newNetwork.client.fetch\n ? createFetchFn(newNetwork.client.fetch, middleware)\n : createFetchFn(middleware);\n return newNetwork;\n }\n\n // Only network argument\n return newNetwork;\n}\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export * from './constants';\nexport * from './network';\n"],"names":["ChainId","PeerNetworkId","TransactionVersion","AddressVersion"],"sourceRoot":""}
|
|
1
|
+
{"version":3,"file":"index.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;;;;;;;;;;;;;;;;;;;;ACNO,IAAK,UAAL,kBAAKA,aAAL;AACL,EAAAA,kBAAA,aAAU,KAAV;AACA,EAAAA,kBAAA,aAAU,cAAV;AAFU,SAAAA;AAAA;AAeL,IAAK,gBAAL,kBAAKC,mBAAL;AACL,EAAAA,8BAAA,aAAU,aAAV;AACA,EAAAA,8BAAA,aAAU,cAAV;AAFU,SAAAA;AAAA;AAKL,MAAM,mBAAmB;AAOzB,IAAK,qBAAL,kBAAKC,wBAAL;AACL,EAAAA,wCAAA,aAAU,KAAV;AACA,EAAAA,wCAAA,aAAU,OAAV;AAFU,SAAAA;AAAA;AAWL,IAAK,iBAAL,kBAAKC,oBAAL;AAEL,EAAAA,gCAAA,sBAAmB,MAAnB;AAEA,EAAAA,gCAAA,qBAAkB,MAAlB;AAEA,EAAAA,gCAAA,sBAAmB,MAAnB;AAEA,EAAAA,gCAAA,qBAAkB,MAAlB;AARU,SAAAA;AAAA;AAWL,MAAM,8BAA8B;AAGpC,SAAS,uBAAuB,oBAAwC;AAC7E,SAAO,CAAI,QAA0C,IAAI,kBAAkB;AAC7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACjDO;AACoE;AA0BpE,MAAM,iBAAgC;AAAA,EAC3C,SAAS,+CA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C5B,WAAO;AAAA,EACT;AAGA,SAAO;AACT;;;;;;;;;;;;;;;;;;;;ACvPO;AACA;AACA;AACA;AACA;AACA;AACP,qC;;;;;;;;;;;;;;;;;;;ACNA;AACA;AACA;AACA;AACA,KAAK;AACL;AACO;AACP;AACA;AACO;AACP;AACA;AACO;AACP;AACA;AACA;AACA;AACA;AACO;AACP;AACA;AACA;AACA;AACO,kCAAkC,oFAAoF;AAC7H;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS;AACT;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,aAAa;AACb;AACO;AACP,YAAY,wBAAwB;AACpC;AACA,4BAA4B;AAC5B;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iBAAiB;AACjB;AACA;AACA;AACA;AACA;AACA;AACA;AACA,iC;;;;;;UC5EA;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;UACA;UACA;UACA;UACA;UACA;UACA;;UAEA;UACA;UACA;;;;UC5BA;UACA;UACA;UACA;UACA,yCAAyC,wCAAwC;UACjF;UACA;UACA,E;;;UCPA,yF;;;UCAA;UACA;UACA,sDAAsD,iBAAiB;UACvE,gDAAgD,aAAa;UAC7D,E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACJc;AACA","sources":["webpack://StacksNetwork/webpack/universalModuleDefinition","webpack://StacksNetwork/./src/constants.ts","webpack://StacksNetwork/./src/network.ts","webpack://StacksNetwork/../common/dist/esm/constants.js","webpack://StacksNetwork/../common/dist/esm/fetch.js","webpack://StacksNetwork/webpack/bootstrap","webpack://StacksNetwork/webpack/runtime/define property getters","webpack://StacksNetwork/webpack/runtime/hasOwnProperty shorthand","webpack://StacksNetwork/webpack/runtime/make namespace object","webpack://StacksNetwork/./src/index.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"StacksNetwork\"] = factory();\n\telse\n\t\troot[\"StacksNetwork\"] = factory();\n})(this, () => {\nreturn ","/**\n * The chain ID (unsigned 32-bit integer), used so transactions can't be replayed on other chains.\n * Similar to the {@link TransactionVersion}.\n */\nexport enum ChainId {\n Mainnet = 0x00000001,\n Testnet = 0x80000000,\n}\n\n/**\n * The **peer** network ID.\n * Typically not used in signing, but used for broadcasting to the P2P network.\n * It can also be used to determine the parent of a subnet.\n *\n * **Attention:**\n * For mainnet/testnet the v2/info response `.network_id` refers to the chain ID.\n * For subnets the v2/info response `.network_id` refers to the peer network ID and the chain ID (they are the same for subnets).\n * The `.parent_network_id` refers to the actual peer network ID (of the parent) in both cases.\n */\nexport enum PeerNetworkId {\n Mainnet = 0x17000000,\n Testnet = 0xff000000,\n}\n\nexport const DEFAULT_CHAIN_ID = ChainId.Mainnet;\n\n/**\n * The transaction version, used so transactions can't be replayed on other networks.\n * Similar to the {@link ChainId}.\n * Used internally for serializing and deserializing transactions.\n */\nexport enum TransactionVersion {\n Mainnet = 0x00,\n Testnet = 0x80,\n}\n\n/**\n * Address versions for identifying address types in an encoded Stacks address.\n * The address version is a single byte, indicating the address type.\n * Every Stacks address starts with `S` followed by a single character indicating the address version.\n * The second character is the c32-encoded AddressVersion byte.\n */\nexport enum AddressVersion {\n /** `P` — A single-sig address for mainnet (starting with `SP`) */\n MainnetSingleSig = 22,\n /** `M` — A multi-sig address for mainnet (starting with `SM`) */\n MainnetMultiSig = 20,\n /** `T` — A single-sig address for testnet (starting with `ST`) */\n TestnetSingleSig = 26,\n /** `N` — A multi-sig address for testnet (starting with `SN`) */\n TestnetMultiSig = 21,\n}\n\nexport const DEFAULT_TRANSACTION_VERSION = TransactionVersion.Mainnet;\n\n/** @ignore */\nexport function whenTransactionVersion(transactionVersion: TransactionVersion) {\n return <T>(map: Record<TransactionVersion, T>): T => map[transactionVersion];\n}\n","import {\n DEVNET_URL,\n FetchFn,\n HIRO_MAINNET_URL,\n HIRO_TESTNET_URL,\n createFetchFn,\n createApiKeyMiddleware,\n ClientOpts,\n ApiKeyMiddlewareOpts,\n} from '@stacks/common';\nimport { AddressVersion, ChainId, PeerNetworkId, TransactionVersion } from './constants';\nimport { ClientParam } from '@stacks/common';\n\nexport type StacksNetwork = {\n chainId: number;\n transactionVersion: number;\n peerNetworkId: number;\n magicBytes: string;\n bootAddress: string;\n addressVersion: {\n singleSig: number;\n multiSig: number;\n };\n // todo: add check32 character bytes string\n client: {\n baseUrl: string; // URL is always required\n fetch?: FetchFn; // fetch is optional and will be created by default in fetch helpers\n };\n};\n\nexport interface NetworkParam {\n network?: StacksNetworkName | StacksNetwork;\n}\n\nexport type NetworkClientParam = NetworkParam & ClientParam;\n\nexport const STACKS_MAINNET: StacksNetwork = {\n chainId: ChainId.Mainnet,\n transactionVersion: TransactionVersion.Mainnet,\n peerNetworkId: PeerNetworkId.Mainnet,\n magicBytes: 'X2', // todo: comment bytes version of magic bytes\n bootAddress: 'SP000000000000000000002Q6VF78',\n addressVersion: {\n singleSig: AddressVersion.MainnetSingleSig,\n multiSig: AddressVersion.MainnetMultiSig,\n },\n client: { baseUrl: HIRO_MAINNET_URL },\n};\n\nexport const STACKS_TESTNET: StacksNetwork = {\n chainId: ChainId.Testnet,\n transactionVersion: TransactionVersion.Testnet,\n peerNetworkId: PeerNetworkId.Testnet,\n magicBytes: 'T2', // todo: comment bytes version of magic bytes\n bootAddress: 'ST000000000000000000002AMW42H',\n addressVersion: {\n singleSig: AddressVersion.TestnetSingleSig,\n multiSig: AddressVersion.TestnetMultiSig,\n },\n client: { baseUrl: HIRO_TESTNET_URL },\n};\n\nexport const STACKS_DEVNET: StacksNetwork = {\n ...STACKS_TESTNET, // todo: ensure deep copy\n addressVersion: { ...STACKS_TESTNET.addressVersion }, // deep copy\n magicBytes: 'id', // todo: comment bytes version of magic bytes\n client: { baseUrl: DEVNET_URL },\n};\n\nexport const STACKS_MOCKNET: StacksNetwork = {\n ...STACKS_DEVNET,\n addressVersion: { ...STACKS_DEVNET.addressVersion }, // deep copy\n client: { ...STACKS_DEVNET.client }, // deep copy\n};\n\n/** @ignore internal */\nexport const StacksNetworks = ['mainnet', 'testnet', 'devnet', 'mocknet'] as const;\n/** The enum-style names of different common Stacks networks */\nexport type StacksNetworkName = (typeof StacksNetworks)[number];\n\n/**\n * Returns the default network for a given name\n * @example\n * ```ts\n * networkFromName('mainnet') // same as STACKS_MAINNET\n * networkFromName('testnet') // same as STACKS_TESTNET\n * networkFromName('devnet') // same as STACKS_DEVNET\n * networkFromName('mocknet') // same as STACKS_MOCKNET\n * ```\n */\nexport function networkFromName(name: StacksNetworkName) {\n switch (name) {\n case 'mainnet':\n return STACKS_MAINNET;\n case 'testnet':\n return STACKS_TESTNET;\n case 'devnet':\n return STACKS_DEVNET;\n case 'mocknet':\n return STACKS_MOCKNET;\n default:\n throw new Error(`Unknown network name: ${name}`);\n }\n}\n\n/** @ignore */\nexport function networkFrom(network: StacksNetworkName | StacksNetwork) {\n if (typeof network === 'string') return networkFromName(network);\n return network;\n}\n\n/** @ignore */\nexport function defaultUrlFromNetwork(network?: StacksNetworkName | StacksNetwork) {\n if (!network) return HIRO_MAINNET_URL; // default to mainnet if no network is given\n\n network = networkFrom(network);\n\n return !network || network.transactionVersion === TransactionVersion.Mainnet\n ? HIRO_MAINNET_URL // default to mainnet if txVersion is mainnet\n : network.magicBytes === 'id'\n ? DEVNET_URL // default to devnet if magicBytes are devnet\n : HIRO_TESTNET_URL;\n}\n\n/**\n * Returns the client of a network, creating a new fetch function if none is available\n */\nexport function clientFromNetwork(network: StacksNetwork): Required<ClientOpts> {\n if (network.client.fetch) return network.client as Required<ClientOpts>;\n return {\n ...network.client,\n fetch: createFetchFn(),\n };\n}\n\n/**\n * Creates a customized Stacks network.\n *\n * This function allows you to create a network based on a predefined network\n * (mainnet, testnet, devnet, mocknet) or a custom network object. You can also customize\n * the network with an API key or other client options.\n *\n * @example\n * ```ts\n * // Create a basic network from a network name\n * const network = createNetwork('mainnet');\n * const network = createNetwork(STACKS_MAINNET);\n * ```\n *\n * @example\n * ```ts\n * // Create a network with an API key\n * const network = createNetwork('testnet', 'my-api-key');\n * const network = createNetwork(STACKS_TESTNET, 'my-api-key');\n * ```\n *\n * @example\n * ```ts\n * // Create a network with options object\n * const network = createNetwork({\n * network: 'mainnet',\n * apiKey: 'my-api-key',\n * });\n * ```\n *\n * @example\n * ```ts\n * // Create a network with options object with custom API key options\n * const network = createNetwork({\n * network: 'mainnet',\n * apiKey: 'my-api-key',\n * host: /\\.example\\.com$/, // default is /(.*)api(.*)(\\.stacks\\.co|\\.hiro\\.so)$/i\n * httpHeader: 'x-custom-api-key', // default is 'x-api-key'\n * });\n * ```\n *\n * @example\n * ```ts\n * // Create a network with custom client options\n * const network = createNetwork({\n * network: STACKS_TESTNET,\n * client: {\n * baseUrl: 'https://custom-api.example.com',\n * fetch: customFetchFunction\n * }\n * });\n * ```\n */\nexport function createNetwork(network: StacksNetworkName | StacksNetwork): StacksNetwork;\nexport function createNetwork(\n network: StacksNetworkName | StacksNetwork,\n apiKey: string\n): StacksNetwork;\nexport function createNetwork(\n options: {\n network: StacksNetworkName | StacksNetwork;\n client?: ClientOpts;\n } & Partial<ApiKeyMiddlewareOpts>\n): StacksNetwork;\nexport function createNetwork(\n arg1:\n | StacksNetworkName\n | StacksNetwork\n | ({\n network: StacksNetworkName | StacksNetwork;\n client?: ClientOpts;\n } & Partial<ApiKeyMiddlewareOpts>),\n arg2?: string\n): StacksNetwork {\n const baseNetwork = networkFrom(\n typeof arg1 === 'object' && 'network' in arg1 ? arg1.network : arg1\n );\n\n const newNetwork: StacksNetwork = {\n ...baseNetwork,\n addressVersion: { ...baseNetwork.addressVersion }, // deep copy\n client: { ...baseNetwork.client }, // deep copy\n };\n\n // Options object argument\n if (typeof arg1 === 'object' && 'network' in arg1) {\n if (arg1.client) {\n newNetwork.client.baseUrl = arg1.client.baseUrl ?? newNetwork.client.baseUrl;\n newNetwork.client.fetch = arg1.client.fetch ?? newNetwork.client.fetch;\n }\n\n if (typeof arg1.apiKey === 'string') {\n const middleware = createApiKeyMiddleware(arg1 as ApiKeyMiddlewareOpts);\n newNetwork.client.fetch = newNetwork.client.fetch\n ? createFetchFn(newNetwork.client.fetch, middleware)\n : createFetchFn(middleware);\n }\n\n return newNetwork;\n }\n\n // Additional API key argument\n if (typeof arg2 === 'string') {\n const middleware = createApiKeyMiddleware({ apiKey: arg2 });\n newNetwork.client.fetch = newNetwork.client.fetch\n ? createFetchFn(newNetwork.client.fetch, middleware)\n : createFetchFn(middleware);\n return newNetwork;\n }\n\n // Only network argument\n return newNetwork;\n}\n","export const HIRO_MAINNET_URL = 'https://api.mainnet.hiro.so';\nexport const HIRO_TESTNET_URL = 'https://api.testnet.hiro.so';\nexport const DEVNET_URL = 'http://localhost:3999';\nexport const GAIA_URL = 'https://hub.blockstack.org';\nexport const PRIVATE_KEY_BYTES_COMPRESSED = 33;\nexport const PRIVATE_KEY_BYTES_UNCOMPRESSED = 32;\n//# sourceMappingURL=constants.js.map","const defaultFetchOpts = {\n referrerPolicy: 'origin',\n headers: {\n 'x-hiro-product': 'stacksjs',\n },\n};\nexport const getFetchOptions = () => {\n return defaultFetchOpts;\n};\nexport const setFetchOptions = (ops) => {\n return Object.assign(defaultFetchOpts, ops);\n};\nexport async function fetchWrapper(input, init) {\n const fetchOpts = {};\n Object.assign(fetchOpts, defaultFetchOpts, init);\n const fetchResult = await fetch(input, fetchOpts);\n return fetchResult;\n}\nexport function hostMatches(host, pattern) {\n if (typeof pattern === 'string')\n return pattern === host;\n return pattern.exec(host);\n}\nexport function createApiKeyMiddleware({ apiKey, host = /(.*)api(.*)(\\.stacks\\.co|\\.hiro\\.so)$/i, httpHeader = 'x-api-key', }) {\n return {\n pre: context => {\n const reqUrl = new URL(context.url);\n if (!hostMatches(reqUrl.host, host))\n return;\n const headers = context.init.headers instanceof Headers\n ? context.init.headers\n : (context.init.headers = new Headers(context.init.headers));\n headers.set(httpHeader, apiKey);\n },\n };\n}\nfunction argsForCreateFetchFn(args) {\n let fetchLib = fetchWrapper;\n let middlewares = [];\n if (args.length > 0 && typeof args[0] === 'function') {\n fetchLib = args.shift();\n }\n if (args.length > 0) {\n middlewares = args;\n }\n return { fetchLib, middlewares };\n}\nexport function createFetchFn(...args) {\n const { fetchLib, middlewares } = argsForCreateFetchFn(args);\n const fetchFn = async (url, init) => {\n let fetchParams = { url, init: init ?? {} };\n for (const middleware of middlewares) {\n if (typeof middleware.pre === 'function') {\n const result = await Promise.resolve(middleware.pre({\n fetch: fetchLib,\n ...fetchParams,\n }));\n fetchParams = result ?? fetchParams;\n }\n }\n let response = await fetchLib(fetchParams.url, fetchParams.init);\n for (const middleware of middlewares) {\n if (typeof middleware.post === 'function') {\n const result = await Promise.resolve(middleware.post({\n fetch: fetchLib,\n url: fetchParams.url,\n init: fetchParams.init,\n response: response?.clone() ?? response,\n }));\n response = result ?? response;\n }\n }\n return response;\n };\n return fetchFn;\n}\n//# sourceMappingURL=fetch.js.map","// The module cache\nconst __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tconst cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tconst module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\tif (!(moduleId in __webpack_modules__)) {\n\t\tdelete __webpack_module_cache__[moduleId];\n\t\tconst e = new Error(\"Cannot find module '\" + moduleId + \"'\");\n\t\te.code = 'MODULE_NOT_FOUND';\n\t\tthrow e;\n\t}\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// define getter/value functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop));","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export * from './constants';\nexport * from './network';\n"],"names":["ChainId","PeerNetworkId","TransactionVersion","AddressVersion"],"sourceRoot":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacks/network",
|
|
3
|
-
"version": "7.6.0",
|
|
3
|
+
"version": "7.6.1-pr.1880.0",
|
|
4
4
|
"description": "Library for Stacks network operations",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Hiro Systems PBC (https://hiro.so)",
|
|
@@ -20,11 +20,9 @@
|
|
|
20
20
|
"typecheck:watch": "npm run typecheck -- --watch"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@stacks/common": "
|
|
24
|
-
"cross-fetch": "^3.1.5"
|
|
23
|
+
"@stacks/common": "7.6.1-pr.1880.0"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"process": "^0.11.10",
|
|
28
26
|
"rimraf": "^3.0.2"
|
|
29
27
|
},
|
|
30
28
|
"sideEffects": false,
|