@bsv/sdk 1.6.3 → 1.6.5
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/dist/cjs/package.json +1 -1
- package/dist/cjs/src/transaction/chaintrackers/BlockHeadersService.js +91 -0
- package/dist/cjs/src/transaction/chaintrackers/BlockHeadersService.js.map +1 -0
- package/dist/cjs/src/transaction/chaintrackers/index.js +3 -1
- package/dist/cjs/src/transaction/chaintrackers/index.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/transaction/chaintrackers/BlockHeadersService.js +90 -0
- package/dist/esm/src/transaction/chaintrackers/BlockHeadersService.js.map +1 -0
- package/dist/esm/src/transaction/chaintrackers/index.js +1 -0
- package/dist/esm/src/transaction/chaintrackers/index.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/script/index.d.ts +1 -0
- package/dist/types/src/script/index.d.ts.map +1 -1
- package/dist/types/src/transaction/chaintrackers/BlockHeadersService.d.ts +46 -0
- package/dist/types/src/transaction/chaintrackers/BlockHeadersService.d.ts.map +1 -0
- package/dist/types/src/transaction/chaintrackers/index.d.ts +1 -0
- package/dist/types/src/transaction/chaintrackers/index.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/dist/umd/bundle.js +1 -1
- package/docs/overlay-tools.md +2 -3
- package/docs/primitives.md +20 -53
- package/docs/transaction.md +1 -1
- package/package.json +1 -1
- package/src/script/index.ts +1 -0
- package/src/transaction/chaintrackers/BlockHeadersService.ts +138 -0
- package/src/transaction/chaintrackers/index.ts +1 -0
package/docs/overlay-tools.md
CHANGED
|
@@ -272,6 +272,7 @@ Tagged BEEF
|
|
|
272
272
|
export interface TaggedBEEF {
|
|
273
273
|
beef: number[];
|
|
274
274
|
topics: string[];
|
|
275
|
+
offChainValues?: number[];
|
|
275
276
|
}
|
|
276
277
|
```
|
|
277
278
|
|
|
@@ -530,10 +531,8 @@ export type LookupAnswer = {
|
|
|
530
531
|
outputs: Array<{
|
|
531
532
|
beef: number[];
|
|
532
533
|
outputIndex: number;
|
|
534
|
+
context?: number[];
|
|
533
535
|
}>;
|
|
534
|
-
} | {
|
|
535
|
-
type: "freeform";
|
|
536
|
-
result: unknown;
|
|
537
536
|
}
|
|
538
537
|
```
|
|
539
538
|
|
package/docs/primitives.md
CHANGED
|
@@ -93,8 +93,7 @@ export default class BigNumber {
|
|
|
93
93
|
zeroBits(): number
|
|
94
94
|
byteLength(): number { if (this._magnitude === 0n)
|
|
95
95
|
return 0; return Math.ceil(this.bitLength() / 8); }
|
|
96
|
-
toTwos(width: number): BigNumber
|
|
97
|
-
v = (1n << Bw) + v; const m = (1n << Bw) - 1n; v &= m; const r = new BigNumber(0n); r._initializeState(v, 0); return r; }
|
|
96
|
+
toTwos(width: number): BigNumber
|
|
98
97
|
fromTwos(width: number): BigNumber
|
|
99
98
|
isNeg(): boolean
|
|
100
99
|
neg(): BigNumber
|
|
@@ -132,15 +131,7 @@ export default class BigNumber {
|
|
|
132
131
|
iushln(bits: number): this { this.assert(typeof bits === "number" && bits >= 0); if (bits === 0)
|
|
133
132
|
return this; this._magnitude <<= BigInt(bits); this._finishInitialization(); return this.strip(); }
|
|
134
133
|
ishln(bits: number): this
|
|
135
|
-
iushrn(bits: number, hint?: number, extended?: BigNumber): this
|
|
136
|
-
if (extended != null)
|
|
137
|
-
extended._initializeState(0n, 0);
|
|
138
|
-
return this;
|
|
139
|
-
} if (extended != null) {
|
|
140
|
-
const m = (1n << BigInt(bits)) - 1n;
|
|
141
|
-
const sOut = this._magnitude & m;
|
|
142
|
-
extended._initializeState(sOut, 0);
|
|
143
|
-
} this._magnitude >>= BigInt(bits); this._finishInitialization(); return this.strip(); }
|
|
134
|
+
iushrn(bits: number, hint?: number, extended?: BigNumber): this
|
|
144
135
|
ishrn(bits: number, hint?: number, extended?: BigNumber): this
|
|
145
136
|
shln(bits: number): BigNumber
|
|
146
137
|
ushln(bits: number): BigNumber
|
|
@@ -168,31 +159,8 @@ export default class BigNumber {
|
|
|
168
159
|
a: BigNumber;
|
|
169
160
|
b: BigNumber;
|
|
170
161
|
gcd: BigNumber;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
let t = vV;
|
|
174
|
-
vV = uV % vV;
|
|
175
|
-
uV = t;
|
|
176
|
-
t = pa;
|
|
177
|
-
pa = a - q * pa;
|
|
178
|
-
a = t;
|
|
179
|
-
t = pb;
|
|
180
|
-
pb = b - q * pb;
|
|
181
|
-
b = t;
|
|
182
|
-
} const ra = new BigNumber(0n); ra._setValueFromSigned(a); const rb = new BigNumber(0n); rb._setValueFromSigned(b); const rg = new BigNumber(0n); rg._initializeState(uV < 0n ? -uV : uV, 0); return { a: ra, b: rb, gcd: rg }; }
|
|
183
|
-
gcd(num: BigNumber): BigNumber { let u = this._magnitude; let v = num._magnitude; if (u === 0n) {
|
|
184
|
-
const r = new BigNumber(0n);
|
|
185
|
-
r._setValueFromSigned(v);
|
|
186
|
-
return r.iabs();
|
|
187
|
-
} if (v === 0n) {
|
|
188
|
-
const r = new BigNumber(0n);
|
|
189
|
-
r._setValueFromSigned(u);
|
|
190
|
-
return r.iabs();
|
|
191
|
-
} while (v !== 0n) {
|
|
192
|
-
const t = u % v;
|
|
193
|
-
u = v;
|
|
194
|
-
v = t;
|
|
195
|
-
} const res = new BigNumber(0n); res._initializeState(u, 0); return res; }
|
|
162
|
+
}
|
|
163
|
+
gcd(num: BigNumber): BigNumber
|
|
196
164
|
invm(num: BigNumber): BigNumber
|
|
197
165
|
isEven(): boolean
|
|
198
166
|
isOdd(): boolean
|
|
@@ -3247,8 +3215,8 @@ export class Reader {
|
|
|
3247
3215
|
public pos: number;
|
|
3248
3216
|
constructor(bin: number[] = [], pos: number = 0)
|
|
3249
3217
|
public eof(): boolean
|
|
3250
|
-
public read(len = this.
|
|
3251
|
-
public readReverse(len = this.
|
|
3218
|
+
public read(len = this.length): number[]
|
|
3219
|
+
public readReverse(len = this.length): number[]
|
|
3252
3220
|
public readUInt8(): number
|
|
3253
3221
|
public readInt8(): number
|
|
3254
3222
|
public readUInt16BE(): number
|
|
@@ -5047,9 +5015,9 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
|
|
|
5047
5015
|
|
|
5048
5016
|
```ts
|
|
5049
5017
|
exclusiveOR = function (block0: number[], block1: number[]): number[] {
|
|
5050
|
-
|
|
5051
|
-
const result =
|
|
5052
|
-
for (i = 0; i <
|
|
5018
|
+
const len = block0.length;
|
|
5019
|
+
const result = new Array(len);
|
|
5020
|
+
for (let i = 0; i < len; i++) {
|
|
5053
5021
|
result[i] = block0[i] ^ block1[i];
|
|
5054
5022
|
}
|
|
5055
5023
|
return result;
|
|
@@ -5231,20 +5199,19 @@ Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](
|
|
|
5231
5199
|
|
|
5232
5200
|
```ts
|
|
5233
5201
|
multiply = function (block0: number[], block1: number[]): number[] {
|
|
5234
|
-
|
|
5235
|
-
|
|
5236
|
-
let
|
|
5237
|
-
|
|
5238
|
-
|
|
5239
|
-
|
|
5240
|
-
if (checkBit(block0, i, j) !== 0) {
|
|
5241
|
-
z = exclusiveOR(z, v);
|
|
5202
|
+
const v = block1.slice();
|
|
5203
|
+
const z = createZeroBlock(16);
|
|
5204
|
+
for (let i = 0; i < 16; i++) {
|
|
5205
|
+
for (let j = 7; j >= 0; j--) {
|
|
5206
|
+
if ((block0[i] & (1 << j)) !== 0) {
|
|
5207
|
+
xorInto(z, v);
|
|
5242
5208
|
}
|
|
5243
|
-
if (
|
|
5244
|
-
|
|
5209
|
+
if ((v[15] & 1) !== 0) {
|
|
5210
|
+
rightShift(v);
|
|
5211
|
+
xorInto(v, R);
|
|
5245
5212
|
}
|
|
5246
5213
|
else {
|
|
5247
|
-
|
|
5214
|
+
rightShift(v);
|
|
5248
5215
|
}
|
|
5249
5216
|
}
|
|
5250
5217
|
}
|
|
@@ -5252,7 +5219,7 @@ multiply = function (block0: number[], block1: number[]): number[] {
|
|
|
5252
5219
|
}
|
|
5253
5220
|
```
|
|
5254
5221
|
|
|
5255
|
-
See also: [
|
|
5222
|
+
See also: [rightShift](./primitives.md#variable-rightshift)
|
|
5256
5223
|
|
|
5257
5224
|
Links: [API](#api), [Interfaces](#interfaces), [Classes](#classes), [Functions](#functions), [Types](#types), [Enums](#enums), [Variables](#variables)
|
|
5258
5225
|
|
package/docs/transaction.md
CHANGED
|
@@ -1506,7 +1506,7 @@ export default class Transaction {
|
|
|
1506
1506
|
static fromHex(hex: string): Transaction
|
|
1507
1507
|
static fromHexEF(hex: string): Transaction
|
|
1508
1508
|
static fromHexBEEF(hex: string, txid?: string): Transaction
|
|
1509
|
-
constructor(version: number = 1, inputs: TransactionInput[] = [], outputs: TransactionOutput[] = [], lockTime: number = 0, metadata: Record<string, any> =
|
|
1509
|
+
constructor(version: number = 1, inputs: TransactionInput[] = [], outputs: TransactionOutput[] = [], lockTime: number = 0, metadata: Record<string, any> = new Map(), merklePath?: MerklePath)
|
|
1510
1510
|
addInput(input: TransactionInput): void
|
|
1511
1511
|
addOutput(output: TransactionOutput): void
|
|
1512
1512
|
addP2PKHOutput(address: number[] | string, satoshis?: number): void
|
package/package.json
CHANGED
package/src/script/index.ts
CHANGED
|
@@ -6,3 +6,4 @@ export { default as Spend } from './Spend.js'
|
|
|
6
6
|
export type { default as ScriptTemplateUnlock } from './ScriptTemplateUnlock.js'
|
|
7
7
|
export type { default as ScriptTemplate } from './ScriptTemplate.js'
|
|
8
8
|
export * from './templates/index.js'
|
|
9
|
+
export type { default as ScriptChunk } from './ScriptChunk.js'
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import ChainTracker from '../ChainTracker.js'
|
|
2
|
+
import { HttpClient } from '../http/HttpClient.js'
|
|
3
|
+
import { defaultHttpClient } from '../http/DefaultHttpClient.js'
|
|
4
|
+
|
|
5
|
+
/** Configuration options for the BlockHeadersService ChainTracker. */
|
|
6
|
+
export interface BlockHeadersServiceConfig {
|
|
7
|
+
/** The HTTP client used to make requests to the API. */
|
|
8
|
+
httpClient?: HttpClient
|
|
9
|
+
|
|
10
|
+
/** The API key used to authenticate requests to the BlockHeadersService API. */
|
|
11
|
+
apiKey?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface MerkleRootVerificationRequest {
|
|
15
|
+
blockHeight: number
|
|
16
|
+
merkleRoot: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface MerkleRootConfirmation {
|
|
20
|
+
blockHash: string
|
|
21
|
+
blockHeight: number
|
|
22
|
+
merkleRoot: string
|
|
23
|
+
confirmation: 'CONFIRMED' | 'UNCONFIRMED'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface MerkleRootVerificationResponse {
|
|
27
|
+
confirmationState: 'CONFIRMED' | 'UNCONFIRMED'
|
|
28
|
+
confirmations: MerkleRootConfirmation[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Represents a chain tracker based on a BlockHeadersService API.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const chainTracker = new BlockHeadersService('https://headers.spv.money', {
|
|
37
|
+
* apiKey: '17JxRHcJerGBEbusx56W8o1m8Js73TFGo'
|
|
38
|
+
* })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export class BlockHeadersService implements ChainTracker {
|
|
42
|
+
protected readonly baseUrl: string
|
|
43
|
+
protected readonly httpClient: HttpClient
|
|
44
|
+
protected readonly apiKey: string
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Constructs an instance of the BlockHeadersService ChainTracker.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} baseUrl - The base URL for the BlockHeadersService API (e.g. https://headers.spv.money)
|
|
50
|
+
* @param {BlockHeadersServiceConfig} config - Configuration options for the BlockHeadersService ChainTracker.
|
|
51
|
+
*/
|
|
52
|
+
constructor(
|
|
53
|
+
baseUrl: string,
|
|
54
|
+
config: BlockHeadersServiceConfig = {}
|
|
55
|
+
) {
|
|
56
|
+
const { httpClient, apiKey } = config
|
|
57
|
+
this.baseUrl = baseUrl
|
|
58
|
+
this.httpClient = httpClient ?? defaultHttpClient()
|
|
59
|
+
this.apiKey = apiKey ?? ''
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Verifies if a given merkle root is valid for a specific block height.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} root - The merkle root to verify.
|
|
66
|
+
* @param {number} height - The block height to check against.
|
|
67
|
+
* @returns {Promise<boolean>} - A promise that resolves to true if the merkle root is valid for the specified block height, false otherwise.
|
|
68
|
+
*/
|
|
69
|
+
async isValidRootForHeight(root: string, height: number): Promise<boolean> {
|
|
70
|
+
const requestOptions = {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
headers: {
|
|
73
|
+
'Content-Type': 'application/json',
|
|
74
|
+
'Accept': 'application/json',
|
|
75
|
+
'Authorization': `Bearer ${this.apiKey}`
|
|
76
|
+
},
|
|
77
|
+
data: [
|
|
78
|
+
{
|
|
79
|
+
blockHeight: height,
|
|
80
|
+
merkleRoot: root
|
|
81
|
+
}
|
|
82
|
+
] as MerkleRootVerificationRequest[]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
const response = await this.httpClient.request<MerkleRootVerificationResponse>(
|
|
87
|
+
`${this.baseUrl}/api/v1/chain/merkleroot/verify`,
|
|
88
|
+
requestOptions
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
if (response.ok) {
|
|
92
|
+
return response.data.confirmationState === 'CONFIRMED'
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`Failed to verify merkleroot for height ${height} because of an error: ${JSON.stringify(response.data)}`
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
`Failed to verify merkleroot for height ${height} because of an error: ${error instanceof Error ? error.message : String(error)}`
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Gets the current block height from the BlockHeadersService API.
|
|
107
|
+
*
|
|
108
|
+
* @returns {Promise<number>} - A promise that resolves to the current block height.
|
|
109
|
+
*/
|
|
110
|
+
async currentHeight(): Promise<number> {
|
|
111
|
+
const requestOptions = {
|
|
112
|
+
method: 'GET',
|
|
113
|
+
headers: {
|
|
114
|
+
'Accept': 'application/json',
|
|
115
|
+
'Authorization': `Bearer ${this.apiKey}`
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
const response = await this.httpClient.request<{ height: number }>(
|
|
121
|
+
`${this.baseUrl}/api/v1/chain/tip/longest`,
|
|
122
|
+
requestOptions
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
if (response.ok && response.data && typeof response.data.height === 'number') {
|
|
126
|
+
return response.data.height
|
|
127
|
+
} else {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`Failed to get current height because of an error: ${JSON.stringify(response.data)}`
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
} catch (error) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`Failed to get current height because of an error: ${error instanceof Error ? error.message : String(error)}`
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|