bsv-x402 0.2.0 → 0.3.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/dist/index.d.cts CHANGED
@@ -8,6 +8,45 @@ interface Proof {
8
8
  txid: string;
9
9
  rawTx: string;
10
10
  }
11
+ interface Brc105Challenge {
12
+ version: string;
13
+ satoshisRequired: number;
14
+ serverIdentityKey: string;
15
+ derivationPrefix: string;
16
+ }
17
+ /** Minimal wallet interface for BRC-105 proof construction.
18
+ * Works with both CWIInterface (page context) and WalletInterface (SDK). */
19
+ interface Brc105Wallet {
20
+ getPublicKey(params: {
21
+ protocolID: [number, string];
22
+ keyID: string;
23
+ counterparty: string;
24
+ }): Promise<{
25
+ publicKey: string;
26
+ }>;
27
+ createHmac(params: {
28
+ data: number[];
29
+ protocolID: [number, string];
30
+ keyID: string;
31
+ counterparty?: string;
32
+ }): Promise<{
33
+ hmac: number[];
34
+ }>;
35
+ createAction(params: CWICreateActionParams): Promise<CWICreateActionResult>;
36
+ }
37
+ interface Brc105Proof {
38
+ derivationPrefix: string;
39
+ derivationSuffix: string;
40
+ transaction: string;
41
+ txid: string;
42
+ }
43
+ type Brc105ProofConstructor = (challenge: Brc105Challenge) => Promise<Brc105Proof>;
44
+ type PaymentProtocol = 'x402' | 'brc105';
45
+ interface PaymentRequest {
46
+ amount: number;
47
+ origin: string;
48
+ protocol: PaymentProtocol;
49
+ }
11
50
  type SpendMode = "interactive" | "programmatic";
12
51
  type TimeWindow = "minute" | "hour" | "day" | "week";
13
52
  interface WindowLimit {
@@ -49,6 +88,8 @@ interface X402Config {
49
88
  storage?: StorageAdapter;
50
89
  twoFactorProvider?: TwoFactorProvider;
51
90
  proofConstructor?: (challenge: Challenge) => Promise<Proof>;
91
+ brc105ProofConstructor?: Brc105ProofConstructor;
92
+ brc105Wallet?: Brc105Wallet;
52
93
  nightmareConfirmation?: string;
53
94
  onLimitReached?: (reason: string) => void;
54
95
  onYellowLight?: (detail: YellowLightEvent) => Promise<boolean>;
@@ -59,13 +100,14 @@ interface YellowLightEvent {
59
100
  currentSpend: number;
60
101
  limit: number;
61
102
  window: TimeWindow;
62
- challenge: Challenge;
103
+ challenge: Challenge | PaymentRequest;
63
104
  }
64
105
  interface LedgerEntry {
65
106
  timestamp: number;
66
107
  origin: string;
67
108
  satoshis: number;
68
109
  txid: string;
110
+ protocol?: PaymentProtocol;
69
111
  }
70
112
  interface LimitState {
71
113
  entries: LedgerEntry[];
@@ -89,6 +131,27 @@ interface StorageAdapter {
89
131
  loadSitePolicies(): Promise<Record<string, SitePolicy>>;
90
132
  saveSitePolicies(policies: Record<string, SitePolicy>): Promise<void>;
91
133
  }
134
+ interface CWICreateActionOutput {
135
+ satoshis: number;
136
+ lockingScript: string;
137
+ description?: string;
138
+ customInstructions?: string;
139
+ }
140
+ interface CWICreateActionParams {
141
+ description: string;
142
+ outputs: CWICreateActionOutput[];
143
+ labels?: string[];
144
+ options?: {
145
+ returnTXIDOnly?: boolean;
146
+ noSend?: boolean;
147
+ randomizeOutputs?: boolean;
148
+ };
149
+ }
150
+ interface CWICreateActionResult {
151
+ txid: string;
152
+ rawTx?: string;
153
+ tx?: number[];
154
+ }
92
155
  type TwoFactorAction = {
93
156
  type: "circuit-breaker-reset";
94
157
  } | {
@@ -125,6 +188,31 @@ declare function x402Fetch(input: RequestInfo | URL, init?: RequestInit): Promis
125
188
 
126
189
  declare function parseChallenge(header: string): Challenge;
127
190
 
191
+ /**
192
+ * Parses BRC-105 payment headers from a 402 response.
193
+ *
194
+ * Expects four headers:
195
+ * - x-bsv-payment-version (must be "1.0")
196
+ * - x-bsv-payment-satoshis-required (positive integer)
197
+ * - x-bsv-auth-identity-key (non-empty)
198
+ * - x-bsv-payment-derivation-prefix (non-empty)
199
+ */
200
+ declare function parseBrc105Challenge(response: Response): Brc105Challenge;
201
+
202
+ /**
203
+ * Construct a BRC-105 payment proof from a challenge using BRC-29 key derivation.
204
+ *
205
+ * Algorithm (matching AuthFetch reference implementation):
206
+ * 1. Generate derivation suffix via wallet HMAC
207
+ * 2. Derive payee public key using protocolID [2, '3241645161d8']
208
+ * 3. Build P2PKH locking script from derived public key
209
+ * 4. Call wallet.createAction with the locking script and custom instructions
210
+ * 5. Convert transaction to base64
211
+ */
212
+ declare function constructBrc105Proof(challenge: Brc105Challenge, wallet: Brc105Wallet): Promise<Brc105Proof>;
213
+
214
+ /** Anything with a numeric `amount` can be checked against spending limits. */
215
+ type SpendCheckable = Challenge | PaymentRequest;
128
216
  declare const BFG_DAILY_CEILING_SATOSHIS = 10000000000;
129
217
  declare const BFG_PER_TX_CEILING_SATOSHIS = 1000000000;
130
218
  declare const TIER_PRESETS: Record<TierName, TierPreset>;
@@ -135,7 +223,7 @@ declare class RateLimiter {
135
223
  private broken;
136
224
  private now;
137
225
  constructor(limits: SpendLimits, state?: LimitState, now?: () => number);
138
- check(challenge: Challenge, origin: string): LimitCheckResult;
226
+ check(request: SpendCheckable, origin: string): LimitCheckResult;
139
227
  record(entry: LedgerEntry): void;
140
228
  trip(): void;
141
229
  reset(): void;
@@ -179,4 +267,4 @@ type SitePromptFn = (origin: string) => Promise<SitePolicyAction>;
179
267
  */
180
268
  declare function resolveSitePolicy(origin: string, limits: SpendLimits, twoFactorProvider?: TwoFactorProvider, promptFn?: SitePromptFn): Promise<SitePolicy>;
181
269
 
182
- export { BFG_DAILY_CEILING_SATOSHIS, BFG_PER_TX_CEILING_SATOSHIS, type Challenge, type LedgerEntry, type LimitCheckResult, type LimitState, LocalStorageAdapter, type Proof, RateLimiter, type SitePolicy, type SitePolicyAction, type SpendLimits, type SpendMode, type StorageAdapter, TIER_PRESETS, type TierName, type TierPreset, type TimeWindow, type TwoFactorAction, type TwoFactorPolicy, type TwoFactorProvider, WalletTwoFactorProvider, type WindowLimit, type X402Config, type X402FetchFn, type YellowLightEvent, createX402Fetch, parseChallenge, resolveSitePolicy, resolveSpendLimits, x402Fetch };
270
+ export { BFG_DAILY_CEILING_SATOSHIS, BFG_PER_TX_CEILING_SATOSHIS, type Brc105Challenge, type Brc105Proof, type Brc105ProofConstructor, type Brc105Wallet, type Challenge, type LedgerEntry, type LimitCheckResult, type LimitState, LocalStorageAdapter, type PaymentProtocol, type PaymentRequest, type Proof, RateLimiter, type SitePolicy, type SitePolicyAction, type SpendCheckable, type SpendLimits, type SpendMode, type StorageAdapter, TIER_PRESETS, type TierName, type TierPreset, type TimeWindow, type TwoFactorAction, type TwoFactorPolicy, type TwoFactorProvider, WalletTwoFactorProvider, type WindowLimit, type X402Config, type X402FetchFn, type YellowLightEvent, constructBrc105Proof, createX402Fetch, parseBrc105Challenge, parseChallenge, resolveSitePolicy, resolveSpendLimits, x402Fetch };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,45 @@ interface Proof {
8
8
  txid: string;
9
9
  rawTx: string;
10
10
  }
11
+ interface Brc105Challenge {
12
+ version: string;
13
+ satoshisRequired: number;
14
+ serverIdentityKey: string;
15
+ derivationPrefix: string;
16
+ }
17
+ /** Minimal wallet interface for BRC-105 proof construction.
18
+ * Works with both CWIInterface (page context) and WalletInterface (SDK). */
19
+ interface Brc105Wallet {
20
+ getPublicKey(params: {
21
+ protocolID: [number, string];
22
+ keyID: string;
23
+ counterparty: string;
24
+ }): Promise<{
25
+ publicKey: string;
26
+ }>;
27
+ createHmac(params: {
28
+ data: number[];
29
+ protocolID: [number, string];
30
+ keyID: string;
31
+ counterparty?: string;
32
+ }): Promise<{
33
+ hmac: number[];
34
+ }>;
35
+ createAction(params: CWICreateActionParams): Promise<CWICreateActionResult>;
36
+ }
37
+ interface Brc105Proof {
38
+ derivationPrefix: string;
39
+ derivationSuffix: string;
40
+ transaction: string;
41
+ txid: string;
42
+ }
43
+ type Brc105ProofConstructor = (challenge: Brc105Challenge) => Promise<Brc105Proof>;
44
+ type PaymentProtocol = 'x402' | 'brc105';
45
+ interface PaymentRequest {
46
+ amount: number;
47
+ origin: string;
48
+ protocol: PaymentProtocol;
49
+ }
11
50
  type SpendMode = "interactive" | "programmatic";
12
51
  type TimeWindow = "minute" | "hour" | "day" | "week";
13
52
  interface WindowLimit {
@@ -49,6 +88,8 @@ interface X402Config {
49
88
  storage?: StorageAdapter;
50
89
  twoFactorProvider?: TwoFactorProvider;
51
90
  proofConstructor?: (challenge: Challenge) => Promise<Proof>;
91
+ brc105ProofConstructor?: Brc105ProofConstructor;
92
+ brc105Wallet?: Brc105Wallet;
52
93
  nightmareConfirmation?: string;
53
94
  onLimitReached?: (reason: string) => void;
54
95
  onYellowLight?: (detail: YellowLightEvent) => Promise<boolean>;
@@ -59,13 +100,14 @@ interface YellowLightEvent {
59
100
  currentSpend: number;
60
101
  limit: number;
61
102
  window: TimeWindow;
62
- challenge: Challenge;
103
+ challenge: Challenge | PaymentRequest;
63
104
  }
64
105
  interface LedgerEntry {
65
106
  timestamp: number;
66
107
  origin: string;
67
108
  satoshis: number;
68
109
  txid: string;
110
+ protocol?: PaymentProtocol;
69
111
  }
70
112
  interface LimitState {
71
113
  entries: LedgerEntry[];
@@ -89,6 +131,27 @@ interface StorageAdapter {
89
131
  loadSitePolicies(): Promise<Record<string, SitePolicy>>;
90
132
  saveSitePolicies(policies: Record<string, SitePolicy>): Promise<void>;
91
133
  }
134
+ interface CWICreateActionOutput {
135
+ satoshis: number;
136
+ lockingScript: string;
137
+ description?: string;
138
+ customInstructions?: string;
139
+ }
140
+ interface CWICreateActionParams {
141
+ description: string;
142
+ outputs: CWICreateActionOutput[];
143
+ labels?: string[];
144
+ options?: {
145
+ returnTXIDOnly?: boolean;
146
+ noSend?: boolean;
147
+ randomizeOutputs?: boolean;
148
+ };
149
+ }
150
+ interface CWICreateActionResult {
151
+ txid: string;
152
+ rawTx?: string;
153
+ tx?: number[];
154
+ }
92
155
  type TwoFactorAction = {
93
156
  type: "circuit-breaker-reset";
94
157
  } | {
@@ -125,6 +188,31 @@ declare function x402Fetch(input: RequestInfo | URL, init?: RequestInit): Promis
125
188
 
126
189
  declare function parseChallenge(header: string): Challenge;
127
190
 
191
+ /**
192
+ * Parses BRC-105 payment headers from a 402 response.
193
+ *
194
+ * Expects four headers:
195
+ * - x-bsv-payment-version (must be "1.0")
196
+ * - x-bsv-payment-satoshis-required (positive integer)
197
+ * - x-bsv-auth-identity-key (non-empty)
198
+ * - x-bsv-payment-derivation-prefix (non-empty)
199
+ */
200
+ declare function parseBrc105Challenge(response: Response): Brc105Challenge;
201
+
202
+ /**
203
+ * Construct a BRC-105 payment proof from a challenge using BRC-29 key derivation.
204
+ *
205
+ * Algorithm (matching AuthFetch reference implementation):
206
+ * 1. Generate derivation suffix via wallet HMAC
207
+ * 2. Derive payee public key using protocolID [2, '3241645161d8']
208
+ * 3. Build P2PKH locking script from derived public key
209
+ * 4. Call wallet.createAction with the locking script and custom instructions
210
+ * 5. Convert transaction to base64
211
+ */
212
+ declare function constructBrc105Proof(challenge: Brc105Challenge, wallet: Brc105Wallet): Promise<Brc105Proof>;
213
+
214
+ /** Anything with a numeric `amount` can be checked against spending limits. */
215
+ type SpendCheckable = Challenge | PaymentRequest;
128
216
  declare const BFG_DAILY_CEILING_SATOSHIS = 10000000000;
129
217
  declare const BFG_PER_TX_CEILING_SATOSHIS = 1000000000;
130
218
  declare const TIER_PRESETS: Record<TierName, TierPreset>;
@@ -135,7 +223,7 @@ declare class RateLimiter {
135
223
  private broken;
136
224
  private now;
137
225
  constructor(limits: SpendLimits, state?: LimitState, now?: () => number);
138
- check(challenge: Challenge, origin: string): LimitCheckResult;
226
+ check(request: SpendCheckable, origin: string): LimitCheckResult;
139
227
  record(entry: LedgerEntry): void;
140
228
  trip(): void;
141
229
  reset(): void;
@@ -179,4 +267,4 @@ type SitePromptFn = (origin: string) => Promise<SitePolicyAction>;
179
267
  */
180
268
  declare function resolveSitePolicy(origin: string, limits: SpendLimits, twoFactorProvider?: TwoFactorProvider, promptFn?: SitePromptFn): Promise<SitePolicy>;
181
269
 
182
- export { BFG_DAILY_CEILING_SATOSHIS, BFG_PER_TX_CEILING_SATOSHIS, type Challenge, type LedgerEntry, type LimitCheckResult, type LimitState, LocalStorageAdapter, type Proof, RateLimiter, type SitePolicy, type SitePolicyAction, type SpendLimits, type SpendMode, type StorageAdapter, TIER_PRESETS, type TierName, type TierPreset, type TimeWindow, type TwoFactorAction, type TwoFactorPolicy, type TwoFactorProvider, WalletTwoFactorProvider, type WindowLimit, type X402Config, type X402FetchFn, type YellowLightEvent, createX402Fetch, parseChallenge, resolveSitePolicy, resolveSpendLimits, x402Fetch };
270
+ export { BFG_DAILY_CEILING_SATOSHIS, BFG_PER_TX_CEILING_SATOSHIS, type Brc105Challenge, type Brc105Proof, type Brc105ProofConstructor, type Brc105Wallet, type Challenge, type LedgerEntry, type LimitCheckResult, type LimitState, LocalStorageAdapter, type PaymentProtocol, type PaymentRequest, type Proof, RateLimiter, type SitePolicy, type SitePolicyAction, type SpendCheckable, type SpendLimits, type SpendMode, type StorageAdapter, TIER_PRESETS, type TierName, type TierPreset, type TimeWindow, type TwoFactorAction, type TwoFactorPolicy, type TwoFactorProvider, WalletTwoFactorProvider, type WindowLimit, type X402Config, type X402FetchFn, type YellowLightEvent, constructBrc105Proof, createX402Fetch, parseBrc105Challenge, parseChallenge, resolveSitePolicy, resolveSpendLimits, x402Fetch };