@outpost-protocol/core 0.0.0-stage → 0.1.1
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 +22 -2
- package/dist/base32.d.ts +37 -0
- package/dist/base32.js +145 -0
- package/dist/bcur.d.ts +59 -0
- package/dist/bcur.js +167 -0
- package/dist/bleTransport.d.ts +163 -0
- package/dist/bleTransport.js +702 -0
- package/dist/crc32c.d.ts +18 -0
- package/dist/crc32c.js +66 -0
- package/dist/digest.d.ts +169 -0
- package/dist/digest.js +356 -0
- package/dist/feeBump.d.ts +216 -0
- package/dist/feeBump.js +708 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/relayDirect.d.ts +200 -0
- package/dist/relayDirect.js +632 -0
- package/dist/vault.d.ts +34 -0
- package/dist/vault.js +53 -0
- package/dist/wallet.d.ts +303 -0
- package/dist/wallet.js +950 -0
- package/dist/wire.d.ts +177 -0
- package/dist/wire.js +997 -0
- package/dist/witnessClient.d.ts +264 -0
- package/dist/witnessClient.js +306 -0
- package/dist/xdr.d.ts +149 -0
- package/dist/xdr.js +384 -0
- package/package.json +51 -4
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@outpost-protocol/core`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Outpost v1.2 offline-resilient Stellar payment wire codec, cryptographic digests, BLE transport, and non-custodial sender wallet SDK.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @outpost-protocol/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
> Building an app that both **sends and receives**? Install [`@outpost-protocol/sdk`](https://www.npmjs.com/package/@outpost-protocol/sdk) instead for a single unified import.
|
|
12
|
+
|
|
13
|
+
## What's Included
|
|
14
|
+
|
|
15
|
+
- **Wire Codecs (`SREQ1`, `SMESHI1`, `SMESHC1`, `SMESHD1`, `SMESHF1`, BC-UR)** — strict `BigInt` asset-agnostic encoding/decoding with CRC-32C framing, canonical Base32, and cross-language golden-vector parity with Soroban & Rust (`outpost-wire`).
|
|
16
|
+
- **Cryptographic Digests & Vault Derivation** — cofactorless RFC 8032 Ed25519 helpers, canonical `UserVault` deterministic ID derivation, and domain-separated `GENESIS` / `INTENT` / `WITNESS_LOCK` / `WITNESS_CERT` preimages.
|
|
17
|
+
- **Non-Custodial `OfflineWallet`** — per-pocket async mutex, burn-before-display monotonic `localSeq`, asset-agnostic untrusted `SREQ1.tip` caps, duplicate `(recipient, orderId)` retry protection, and authenticated `SEQ_TOO_LOW` resync.
|
|
18
|
+
- **Direct & Classic Settlement Builders** — `order_id`-bound CAP-0046-11 `relay_direct` (`SMESHD1`) and hardened CAP-0015 `FeeBumpTransaction` (`SMESHF1`).
|
|
19
|
+
- **Station Transports** — `WitnessClient` (LAN/WAN HTTP) and `BleWitnessClient` (`OUTPOST_BLE_V1` GATT framing for internet-free mobile/POS witnessing).
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
Apache-2.0 — [outpost.cash](https://outpost.cash)
|
package/dist/base32.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unpadded uppercase RFC 4648 Base32 alphabet (`A-Z`, `2-7`).
|
|
3
|
+
* Valid in QR Alphanumeric Mode with zero whitespace or Markdown characters.
|
|
4
|
+
*/
|
|
5
|
+
export declare const BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
6
|
+
/**
|
|
7
|
+
* Hard cap on framed/raw Base32 wire input length in characters (exact parity with Rust `outpost-wire`, `AUD6-M11`).
|
|
8
|
+
* Rejects multi-megabyte inputs in O(1) before trimming, slicing, or allocating decode buffers.
|
|
9
|
+
*/
|
|
10
|
+
export declare const MAX_FRAMED_BASE32_LEN = 32768;
|
|
11
|
+
/**
|
|
12
|
+
* Encode arbitrary bytes to unpadded uppercase RFC 4648 Base32 (`A-Z`, `2-7`).
|
|
13
|
+
*/
|
|
14
|
+
export declare function encodeBase32(bytes: Uint8Array): string;
|
|
15
|
+
/**
|
|
16
|
+
* Decode unpadded RFC 4648 Base32 (`A-Z`, `2-7`, case-insensitive).
|
|
17
|
+
* Strictly rejects invalid characters, padding (`=`), impossible lengths,
|
|
18
|
+
* and non-zero trailing padding bits.
|
|
19
|
+
*/
|
|
20
|
+
export declare function decodeBase32(input: string): Uint8Array;
|
|
21
|
+
/**
|
|
22
|
+
* Encode `<HRP>1<UPPERCASE_UNPADDED_RFC4648_BASE32(payload || crc32c_be_4B)>`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function encodeFramed(hrp: string, payload: Uint8Array): string;
|
|
25
|
+
/**
|
|
26
|
+
* Decode any Outpost framed string `<HRP>1<BASE32(payload || crc32c_be_4B)>`
|
|
27
|
+
* returning uppercase `hrp` and verified `payload`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function splitFramed(text: string): {
|
|
30
|
+
hrp: string;
|
|
31
|
+
payload: Uint8Array;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Decode and verify `<expectedHrp>1<BASE32(payload || crc32c_be_4B)>` (case-insensitive).
|
|
35
|
+
* Verifies HRP match, strict Base32 decoding, and trailing 4-byte CRC32C checksum.
|
|
36
|
+
*/
|
|
37
|
+
export declare function decodeFramed(expectedHrp: string, text: string): Uint8Array;
|
package/dist/base32.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { appendCrc32c, verifyAndStripCrc32c } from "./crc32c.js";
|
|
2
|
+
/**
|
|
3
|
+
* Unpadded uppercase RFC 4648 Base32 alphabet (`A-Z`, `2-7`).
|
|
4
|
+
* Valid in QR Alphanumeric Mode with zero whitespace or Markdown characters.
|
|
5
|
+
*/
|
|
6
|
+
export const BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
7
|
+
/**
|
|
8
|
+
* Hard cap on framed/raw Base32 wire input length in characters (exact parity with Rust `outpost-wire`, `AUD6-M11`).
|
|
9
|
+
* Rejects multi-megabyte inputs in O(1) before trimming, slicing, or allocating decode buffers.
|
|
10
|
+
*/
|
|
11
|
+
export const MAX_FRAMED_BASE32_LEN = 32_768;
|
|
12
|
+
const DECODE_MAP = (() => {
|
|
13
|
+
const map = new Int8Array(128);
|
|
14
|
+
map.fill(-1);
|
|
15
|
+
for (let i = 0; i < BASE32_ALPHABET.length; i++) {
|
|
16
|
+
const code = BASE32_ALPHABET.charCodeAt(i);
|
|
17
|
+
map[code] = i;
|
|
18
|
+
// Case-insensitive on decode
|
|
19
|
+
if (code >= 65 && code <= 90) {
|
|
20
|
+
map[code + 32] = i;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return map;
|
|
24
|
+
})();
|
|
25
|
+
const HRP_REGEX = /^[A-Za-z]+$/;
|
|
26
|
+
/**
|
|
27
|
+
* Encode arbitrary bytes to unpadded uppercase RFC 4648 Base32 (`A-Z`, `2-7`).
|
|
28
|
+
*/
|
|
29
|
+
export function encodeBase32(bytes) {
|
|
30
|
+
if (bytes.length === 0) {
|
|
31
|
+
return "";
|
|
32
|
+
}
|
|
33
|
+
let output = "";
|
|
34
|
+
let buffer = 0;
|
|
35
|
+
let bitsLeft = 0;
|
|
36
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
37
|
+
buffer = (buffer << 8) | bytes[i];
|
|
38
|
+
bitsLeft += 8;
|
|
39
|
+
while (bitsLeft >= 5) {
|
|
40
|
+
bitsLeft -= 5;
|
|
41
|
+
output += BASE32_ALPHABET[(buffer >>> bitsLeft) & 0x1f];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (bitsLeft > 0) {
|
|
45
|
+
output += BASE32_ALPHABET[(buffer << (5 - bitsLeft)) & 0x1f];
|
|
46
|
+
}
|
|
47
|
+
return output;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Decode unpadded RFC 4648 Base32 (`A-Z`, `2-7`, case-insensitive).
|
|
51
|
+
* Strictly rejects invalid characters, padding (`=`), impossible lengths,
|
|
52
|
+
* and non-zero trailing padding bits.
|
|
53
|
+
*/
|
|
54
|
+
export function decodeBase32(input) {
|
|
55
|
+
if (input.length === 0) {
|
|
56
|
+
return new Uint8Array(0);
|
|
57
|
+
}
|
|
58
|
+
if (input.length > MAX_FRAMED_BASE32_LEN) {
|
|
59
|
+
throw new Error(`Base32 input length (${input.length}) exceeds maximum allowed length (${MAX_FRAMED_BASE32_LEN})`);
|
|
60
|
+
}
|
|
61
|
+
// Valid unpadded RFC 4648 Base32 lengths mod 8 are 0, 2, 4, 5, 7.
|
|
62
|
+
const rem = input.length % 8;
|
|
63
|
+
if (rem === 1 || rem === 3 || rem === 6) {
|
|
64
|
+
throw new Error(`Invalid unpadded Base32 length: ${input.length} (mod 8 = ${rem})`);
|
|
65
|
+
}
|
|
66
|
+
const outLen = Math.floor((input.length * 5) / 8);
|
|
67
|
+
const out = new Uint8Array(outLen);
|
|
68
|
+
let outIndex = 0;
|
|
69
|
+
let buffer = 0;
|
|
70
|
+
let bitsLeft = 0;
|
|
71
|
+
for (let i = 0; i < input.length; i++) {
|
|
72
|
+
const code = input.charCodeAt(i);
|
|
73
|
+
const val = code < 128 ? DECODE_MAP[code] : -1;
|
|
74
|
+
if (val === -1) {
|
|
75
|
+
throw new Error(`Invalid Base32 character '${input[i]}' at index ${i}`);
|
|
76
|
+
}
|
|
77
|
+
buffer = (buffer << 5) | val;
|
|
78
|
+
bitsLeft += 5;
|
|
79
|
+
if (bitsLeft >= 8) {
|
|
80
|
+
bitsLeft -= 8;
|
|
81
|
+
out[outIndex++] = (buffer >>> bitsLeft) & 0xff;
|
|
82
|
+
buffer &= (1 << bitsLeft) - 1;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Strict non-zero trailing bit rejection (canonical encoding)
|
|
86
|
+
if (bitsLeft > 0) {
|
|
87
|
+
const trailingMask = (1 << bitsLeft) - 1;
|
|
88
|
+
if ((buffer & trailingMask) !== 0) {
|
|
89
|
+
throw new Error("Non-zero trailing bits in Base32 input");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Validate an Outpost Human-Readable Part (e.g. "SREQ", "SMESHD", "SMESHF", "SMESHI", "SMESHC").
|
|
96
|
+
*/
|
|
97
|
+
function normalizeHrp(hrp) {
|
|
98
|
+
if (!HRP_REGEX.test(hrp)) {
|
|
99
|
+
throw new Error(`Invalid HRP "${hrp}": must be non-empty ASCII letters without separator '1'`);
|
|
100
|
+
}
|
|
101
|
+
return hrp.toUpperCase();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Encode `<HRP>1<UPPERCASE_UNPADDED_RFC4648_BASE32(payload || crc32c_be_4B)>`.
|
|
105
|
+
*/
|
|
106
|
+
export function encodeFramed(hrp, payload) {
|
|
107
|
+
const normHrp = normalizeHrp(hrp);
|
|
108
|
+
const withCrc = appendCrc32c(payload);
|
|
109
|
+
return `${normHrp}1${encodeBase32(withCrc)}`;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Decode any Outpost framed string `<HRP>1<BASE32(payload || crc32c_be_4B)>`
|
|
113
|
+
* returning uppercase `hrp` and verified `payload`.
|
|
114
|
+
*/
|
|
115
|
+
export function splitFramed(text) {
|
|
116
|
+
if (text.length > MAX_FRAMED_BASE32_LEN) {
|
|
117
|
+
throw new Error(`Framed Base32 input length (${text.length}) exceeds maximum allowed length (${MAX_FRAMED_BASE32_LEN})`);
|
|
118
|
+
}
|
|
119
|
+
const trimmed = text.trim();
|
|
120
|
+
const sepIndex = trimmed.indexOf("1");
|
|
121
|
+
if (sepIndex <= 0) {
|
|
122
|
+
throw new Error("Missing or misplaced '1' HRP separator in framed payload");
|
|
123
|
+
}
|
|
124
|
+
const rawHrp = trimmed.slice(0, sepIndex);
|
|
125
|
+
const hrp = normalizeHrp(rawHrp);
|
|
126
|
+
const dataPart = trimmed.slice(sepIndex + 1);
|
|
127
|
+
if (dataPart.length === 0) {
|
|
128
|
+
throw new Error(`Empty Base32 body for framed payload with HRP "${hrp}"`);
|
|
129
|
+
}
|
|
130
|
+
const decoded = decodeBase32(dataPart);
|
|
131
|
+
const payload = verifyAndStripCrc32c(decoded);
|
|
132
|
+
return { hrp, payload };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Decode and verify `<expectedHrp>1<BASE32(payload || crc32c_be_4B)>` (case-insensitive).
|
|
136
|
+
* Verifies HRP match, strict Base32 decoding, and trailing 4-byte CRC32C checksum.
|
|
137
|
+
*/
|
|
138
|
+
export function decodeFramed(expectedHrp, text) {
|
|
139
|
+
const normExpected = normalizeHrp(expectedHrp);
|
|
140
|
+
const { hrp, payload } = splitFramed(text);
|
|
141
|
+
if (hrp !== normExpected) {
|
|
142
|
+
throw new Error(`HRP mismatch: expected "${normExpected}", got "${hrp}"`);
|
|
143
|
+
}
|
|
144
|
+
return payload;
|
|
145
|
+
}
|
package/dist/bcur.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default max raw chunk size per `ur:smesh` QR frame (WIRE-FORMAT-v1.2.md §4.6).
|
|
3
|
+
* Keeps every frame within QR V9-M @ 4-5 fps.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEFAULT_UR_SMESH_MAX_CHUNK_BYTES = 112;
|
|
6
|
+
export declare const UR_SMESH_PREFIX = "UR:SMESH";
|
|
7
|
+
/** Maximum `ur:smesh` frame count (aligned with Rust `outpost-wire` `MAX_UR_FRAMES = 256`). */
|
|
8
|
+
export declare const MAX_UR_SMESH_TOTAL_FRAMES = 256;
|
|
9
|
+
/** Maximum Base32 body character length for `chunk(<=112B) || crc32c(4B)`: ceil(116 * 8 / 5) = 186. */
|
|
10
|
+
export declare const MAX_UR_SMESH_BODY_CHARS: number;
|
|
11
|
+
/** Maximum total reassembled byte length including 4-byte whole-payload CRC32C. */
|
|
12
|
+
export declare const MAX_UR_SMESH_COMBINED_BYTES: number;
|
|
13
|
+
export interface UrSmeshFrame {
|
|
14
|
+
/** 1-based frame index (`1..=total`). */
|
|
15
|
+
readonly index: number;
|
|
16
|
+
/** Total frame count (`>= 1`). */
|
|
17
|
+
readonly total: number;
|
|
18
|
+
/** Decoded chunk payload (per-frame CRC32C already verified and stripped). */
|
|
19
|
+
readonly chunk: Uint8Array;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Split `payload` into `ur:smesh` animated QR frames (WIRE-FORMAT-v1.2.md §4.6):
|
|
23
|
+
* - Appends trailing 4-byte whole-payload CRC32C to `payload`.
|
|
24
|
+
* - Splits into chunks of at most `maxChunkBytes` (default 112B).
|
|
25
|
+
* - Formats each frame as:
|
|
26
|
+
* `UR:SMESH/<index_1_based>-<total>/<UPPERCASE_BASE32(chunk || crc32c_be_4B)>`
|
|
27
|
+
*/
|
|
28
|
+
export declare function encodeUrSmesh(payload: Uint8Array, maxChunkBytes?: number): string[];
|
|
29
|
+
/**
|
|
30
|
+
* Parse and verify a single `ur:smesh` frame (case-insensitive).
|
|
31
|
+
* Verifies frame header format, bounds, strict Base32, and per-frame CRC32C.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseUrSmeshFrame(frame: string): UrSmeshFrame;
|
|
34
|
+
/**
|
|
35
|
+
* Stateful out-of-order reassembler for `ur:smesh` animated QR frames.
|
|
36
|
+
* Verifies per-frame CRC32C on receipt, tolerates duplicate identical frames,
|
|
37
|
+
* rejects conflicting duplicates or mismatched totals, and verifies whole-payload
|
|
38
|
+
* CRC32C upon reassembly.
|
|
39
|
+
*/
|
|
40
|
+
export declare class UrSmeshDecoder {
|
|
41
|
+
private total;
|
|
42
|
+
private readonly chunks;
|
|
43
|
+
/**
|
|
44
|
+
* Feed a scanned `ur:smesh` frame (in any order).
|
|
45
|
+
* Returns `true` once all frames `1..total` have been received.
|
|
46
|
+
*/
|
|
47
|
+
receivePart(frame: string): boolean;
|
|
48
|
+
isComplete(): boolean;
|
|
49
|
+
receivedCount(): number;
|
|
50
|
+
expectedTotal(): number | null;
|
|
51
|
+
missingIndices(): number[];
|
|
52
|
+
progress(): number;
|
|
53
|
+
/**
|
|
54
|
+
* Reassemble all chunks in index order `1..total`, verify and strip trailing 4B whole-payload CRC32C,
|
|
55
|
+
* and return the original payload bytes.
|
|
56
|
+
*/
|
|
57
|
+
result(): Uint8Array;
|
|
58
|
+
reset(): void;
|
|
59
|
+
}
|
package/dist/bcur.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { decodeBase32, encodeBase32, MAX_FRAMED_BASE32_LEN } from "./base32.js";
|
|
2
|
+
import { appendCrc32c, verifyAndStripCrc32c } from "./crc32c.js";
|
|
3
|
+
import { bytesEqual } from "./xdr.js";
|
|
4
|
+
/**
|
|
5
|
+
* Default max raw chunk size per `ur:smesh` QR frame (WIRE-FORMAT-v1.2.md §4.6).
|
|
6
|
+
* Keeps every frame within QR V9-M @ 4-5 fps.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_UR_SMESH_MAX_CHUNK_BYTES = 112;
|
|
9
|
+
export const UR_SMESH_PREFIX = "UR:SMESH";
|
|
10
|
+
/** Maximum `ur:smesh` frame count (aligned with Rust `outpost-wire` `MAX_UR_FRAMES = 256`). */
|
|
11
|
+
export const MAX_UR_SMESH_TOTAL_FRAMES = 256;
|
|
12
|
+
/** Maximum Base32 body character length for `chunk(<=112B) || crc32c(4B)`: ceil(116 * 8 / 5) = 186. */
|
|
13
|
+
export const MAX_UR_SMESH_BODY_CHARS = Math.ceil(((DEFAULT_UR_SMESH_MAX_CHUNK_BYTES + 4) * 8) / 5);
|
|
14
|
+
/** Maximum total reassembled byte length including 4-byte whole-payload CRC32C. */
|
|
15
|
+
export const MAX_UR_SMESH_COMBINED_BYTES = MAX_UR_SMESH_TOTAL_FRAMES * DEFAULT_UR_SMESH_MAX_CHUNK_BYTES;
|
|
16
|
+
/**
|
|
17
|
+
* Split `payload` into `ur:smesh` animated QR frames (WIRE-FORMAT-v1.2.md §4.6):
|
|
18
|
+
* - Appends trailing 4-byte whole-payload CRC32C to `payload`.
|
|
19
|
+
* - Splits into chunks of at most `maxChunkBytes` (default 112B).
|
|
20
|
+
* - Formats each frame as:
|
|
21
|
+
* `UR:SMESH/<index_1_based>-<total>/<UPPERCASE_BASE32(chunk || crc32c_be_4B)>`
|
|
22
|
+
*/
|
|
23
|
+
export function encodeUrSmesh(payload, maxChunkBytes = DEFAULT_UR_SMESH_MAX_CHUNK_BYTES) {
|
|
24
|
+
if (!(payload instanceof Uint8Array) || payload.length === 0) {
|
|
25
|
+
throw new Error("ur:smesh payload must be a non-empty Uint8Array");
|
|
26
|
+
}
|
|
27
|
+
if (!Number.isInteger(maxChunkBytes) ||
|
|
28
|
+
maxChunkBytes < 1 ||
|
|
29
|
+
maxChunkBytes > DEFAULT_UR_SMESH_MAX_CHUNK_BYTES) {
|
|
30
|
+
throw new Error(`maxChunkBytes must be an integer in 1..=${DEFAULT_UR_SMESH_MAX_CHUNK_BYTES}, got ${maxChunkBytes}`);
|
|
31
|
+
}
|
|
32
|
+
const withWholeCrc = appendCrc32c(payload);
|
|
33
|
+
const total = Math.ceil(withWholeCrc.length / maxChunkBytes);
|
|
34
|
+
if (total > MAX_UR_SMESH_TOTAL_FRAMES) {
|
|
35
|
+
throw new Error(`ur:smesh frame count ${total} exceeds limit ${MAX_UR_SMESH_TOTAL_FRAMES}`);
|
|
36
|
+
}
|
|
37
|
+
const frames = [];
|
|
38
|
+
for (let i = 0; i < total; i++) {
|
|
39
|
+
const start = i * maxChunkBytes;
|
|
40
|
+
const end = Math.min(start + maxChunkBytes, withWholeCrc.length);
|
|
41
|
+
const chunk = withWholeCrc.subarray(start, end);
|
|
42
|
+
const body = encodeBase32(appendCrc32c(chunk));
|
|
43
|
+
frames.push(`${UR_SMESH_PREFIX}/${i + 1}-${total}/${body}`);
|
|
44
|
+
}
|
|
45
|
+
return frames;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parse and verify a single `ur:smesh` frame (case-insensitive).
|
|
49
|
+
* Verifies frame header format, bounds, strict Base32, and per-frame CRC32C.
|
|
50
|
+
*/
|
|
51
|
+
export function parseUrSmeshFrame(frame) {
|
|
52
|
+
if (frame.length > MAX_FRAMED_BASE32_LEN) {
|
|
53
|
+
throw new Error(`ur:smesh frame length (${frame.length}) exceeds maximum allowed length (${MAX_FRAMED_BASE32_LEN})`);
|
|
54
|
+
}
|
|
55
|
+
const upper = frame.trim().toUpperCase();
|
|
56
|
+
const parts = upper.split("/");
|
|
57
|
+
if (parts.length !== 3 || parts[0] !== UR_SMESH_PREFIX) {
|
|
58
|
+
throw new Error(`Invalid ur:smesh frame prefix: expected "${UR_SMESH_PREFIX}/<idx>-<total>/<base32>"`);
|
|
59
|
+
}
|
|
60
|
+
const seqMatch = /^([1-9]\d*)-([1-9]\d*)$/.exec(parts[1]);
|
|
61
|
+
if (!seqMatch) {
|
|
62
|
+
throw new Error(`Invalid ur:smesh sequence spec "${parts[1]}"`);
|
|
63
|
+
}
|
|
64
|
+
const index = Number(seqMatch[1]);
|
|
65
|
+
const total = Number(seqMatch[2]);
|
|
66
|
+
if (!Number.isSafeInteger(index) ||
|
|
67
|
+
!Number.isSafeInteger(total) ||
|
|
68
|
+
total < 1 ||
|
|
69
|
+
total > MAX_UR_SMESH_TOTAL_FRAMES ||
|
|
70
|
+
index < 1 ||
|
|
71
|
+
index > total) {
|
|
72
|
+
throw new Error(`Invalid ur:smesh frame bounds: ${index}/${total}`);
|
|
73
|
+
}
|
|
74
|
+
if (parts[2].length === 0 || parts[2].length > MAX_UR_SMESH_BODY_CHARS) {
|
|
75
|
+
throw new Error(`Invalid ur:smesh Base32 body length ${parts[2].length} (must be in 1..=${MAX_UR_SMESH_BODY_CHARS})`);
|
|
76
|
+
}
|
|
77
|
+
const decoded = decodeBase32(parts[2]);
|
|
78
|
+
const chunk = verifyAndStripCrc32c(decoded);
|
|
79
|
+
if (chunk.length === 0 || chunk.length > DEFAULT_UR_SMESH_MAX_CHUNK_BYTES) {
|
|
80
|
+
throw new Error(`Invalid ur:smesh chunk size ${chunk.length} (must be in 1..=${DEFAULT_UR_SMESH_MAX_CHUNK_BYTES})`);
|
|
81
|
+
}
|
|
82
|
+
return { index, total, chunk };
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Stateful out-of-order reassembler for `ur:smesh` animated QR frames.
|
|
86
|
+
* Verifies per-frame CRC32C on receipt, tolerates duplicate identical frames,
|
|
87
|
+
* rejects conflicting duplicates or mismatched totals, and verifies whole-payload
|
|
88
|
+
* CRC32C upon reassembly.
|
|
89
|
+
*/
|
|
90
|
+
export class UrSmeshDecoder {
|
|
91
|
+
total = null;
|
|
92
|
+
chunks = new Map();
|
|
93
|
+
/**
|
|
94
|
+
* Feed a scanned `ur:smesh` frame (in any order).
|
|
95
|
+
* Returns `true` once all frames `1..total` have been received.
|
|
96
|
+
*/
|
|
97
|
+
receivePart(frame) {
|
|
98
|
+
const parsed = parseUrSmeshFrame(frame);
|
|
99
|
+
if (this.total === null) {
|
|
100
|
+
this.total = parsed.total;
|
|
101
|
+
}
|
|
102
|
+
else if (this.total !== parsed.total) {
|
|
103
|
+
throw new Error(`ur:smesh total mismatch: expected ${this.total}, got ${parsed.total}`);
|
|
104
|
+
}
|
|
105
|
+
const existing = this.chunks.get(parsed.index);
|
|
106
|
+
if (existing !== undefined) {
|
|
107
|
+
if (!bytesEqual(existing, parsed.chunk)) {
|
|
108
|
+
throw new Error(`Conflicting ur:smesh data received for frame ${parsed.index}/${parsed.total}`);
|
|
109
|
+
}
|
|
110
|
+
return this.isComplete();
|
|
111
|
+
}
|
|
112
|
+
this.chunks.set(parsed.index, parsed.chunk);
|
|
113
|
+
return this.isComplete();
|
|
114
|
+
}
|
|
115
|
+
isComplete() {
|
|
116
|
+
return this.total !== null && this.chunks.size === this.total;
|
|
117
|
+
}
|
|
118
|
+
receivedCount() {
|
|
119
|
+
return this.chunks.size;
|
|
120
|
+
}
|
|
121
|
+
expectedTotal() {
|
|
122
|
+
return this.total;
|
|
123
|
+
}
|
|
124
|
+
missingIndices() {
|
|
125
|
+
if (this.total === null)
|
|
126
|
+
return [];
|
|
127
|
+
const missing = [];
|
|
128
|
+
for (let i = 1; i <= this.total; i++) {
|
|
129
|
+
if (!this.chunks.has(i))
|
|
130
|
+
missing.push(i);
|
|
131
|
+
}
|
|
132
|
+
return missing;
|
|
133
|
+
}
|
|
134
|
+
progress() {
|
|
135
|
+
if (this.total === null || this.total === 0)
|
|
136
|
+
return 0;
|
|
137
|
+
return this.chunks.size / this.total;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Reassemble all chunks in index order `1..total`, verify and strip trailing 4B whole-payload CRC32C,
|
|
141
|
+
* and return the original payload bytes.
|
|
142
|
+
*/
|
|
143
|
+
result() {
|
|
144
|
+
if (!this.isComplete() || this.total === null) {
|
|
145
|
+
throw new Error(`ur:smesh incomplete: received ${this.chunks.size}/${this.total ?? "?"} frames`);
|
|
146
|
+
}
|
|
147
|
+
let totalLen = 0;
|
|
148
|
+
for (let i = 1; i <= this.total; i++) {
|
|
149
|
+
totalLen += this.chunks.get(i).length;
|
|
150
|
+
}
|
|
151
|
+
if (totalLen > MAX_UR_SMESH_COMBINED_BYTES) {
|
|
152
|
+
throw new Error(`ur:smesh reassembled length ${totalLen} exceeds maximum ${MAX_UR_SMESH_COMBINED_BYTES} bytes`);
|
|
153
|
+
}
|
|
154
|
+
const combined = new Uint8Array(totalLen);
|
|
155
|
+
let offset = 0;
|
|
156
|
+
for (let i = 1; i <= this.total; i++) {
|
|
157
|
+
const part = this.chunks.get(i);
|
|
158
|
+
combined.set(part, offset);
|
|
159
|
+
offset += part.length;
|
|
160
|
+
}
|
|
161
|
+
return verifyAndStripCrc32c(combined);
|
|
162
|
+
}
|
|
163
|
+
reset() {
|
|
164
|
+
this.total = null;
|
|
165
|
+
this.chunks.clear();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical `OUTPOST_BLE_V1` Binary Framing, Castagnoli CRC32C RPC Envelope,
|
|
3
|
+
* DoS-Bounded Response Reassembler, and `BleWitnessClient` adapter.
|
|
4
|
+
*
|
|
5
|
+
* Byte-for-byte wire-compatible with `crates/witness-node-mobile/src/ble.rs` so
|
|
6
|
+
* any Android/iOS/desktop Outpost POS can execute the full Witness Station API
|
|
7
|
+
* (`GET /info`, `GET /state`, `POST /sign`, `POST /commit-cert`, `POST /fraud-proof`,
|
|
8
|
+
* etc.) over Bluetooth Low Energy GATT with zero Wi-Fi or internet.
|
|
9
|
+
*/
|
|
10
|
+
import { WitnessClient } from "./witnessClient.js";
|
|
11
|
+
/** Outpost v2 BLE GATT Service UUID advertised by Outpost Stations. */
|
|
12
|
+
export declare const OUTPOST_BLE_SERVICE_UUID = "8d4f0001-7b3a-4e2c-9a1d-5c6e7f8a9b01";
|
|
13
|
+
/** GATT RX Characteristic UUID (Central POS -> Peripheral Station: Write / Write Without Response). */
|
|
14
|
+
export declare const OUTPOST_BLE_RX_CHAR_UUID = "8d4f0002-7b3a-4e2c-9a1d-5c6e7f8a9b01";
|
|
15
|
+
/** GATT TX Characteristic UUID (Peripheral Station -> Central POS: Notify / Indicate). */
|
|
16
|
+
export declare const OUTPOST_BLE_TX_CHAR_UUID = "8d4f0003-7b3a-4e2c-9a1d-5c6e7f8a9b01";
|
|
17
|
+
/** GATT Info Characteristic UUID (Read-only compact station metadata <= 128 B JSON). */
|
|
18
|
+
export declare const OUTPOST_BLE_INFO_CHAR_UUID = "8d4f0004-7b3a-4e2c-9a1d-5c6e7f8a9b01";
|
|
19
|
+
export declare const BLE_FRAME_MAGIC_V1 = 177;
|
|
20
|
+
export declare const BLE_FLAG_REQUEST = 0;
|
|
21
|
+
export declare const BLE_FLAG_RESPONSE = 1;
|
|
22
|
+
export declare const BLE_FLAG_ERROR = 2;
|
|
23
|
+
export declare const BLE_CHUNK_HEADER_LEN = 8;
|
|
24
|
+
/** Minimum ATT characteristic value MTU (ATT_MTU 23 - 3 = 20B -> 12B payload/chunk). */
|
|
25
|
+
export declare const MIN_BLE_ATT_VALUE_MTU = 20;
|
|
26
|
+
/** Maximum ATT characteristic value MTU permitted by BLE 4.2/5.x GATT. */
|
|
27
|
+
export declare const MAX_BLE_ATT_VALUE_MTU = 512;
|
|
28
|
+
/** Maximum reassembled BLE RPC request envelope size (64 KiB). */
|
|
29
|
+
export declare const MAX_BLE_REQUEST_BYTES = 65536;
|
|
30
|
+
/** Maximum reassembled BLE RPC response envelope size (128 KiB). */
|
|
31
|
+
export declare const MAX_BLE_RESPONSE_BYTES = 131072;
|
|
32
|
+
/** Maximum BLE Info characteristic payload size (256 B safety cap; station emits <= 128 B). */
|
|
33
|
+
export declare const MAX_BLE_INFO_BYTES = 256;
|
|
34
|
+
/** Maximum ASCII path+query length in a BLE RPC request. */
|
|
35
|
+
export declare const MAX_BLE_PATH_LEN = 1024;
|
|
36
|
+
/** Maximum chunks in a single inbound/outbound request. */
|
|
37
|
+
export declare const MAX_BLE_REQUEST_CHUNKS = 2048;
|
|
38
|
+
/** Maximum chunks in a single inbound/outbound response. */
|
|
39
|
+
export declare const MAX_BLE_RESPONSE_CHUNKS = 4096;
|
|
40
|
+
/** Default client-side reassembly bounds (`AGENTS.md` §1.4). */
|
|
41
|
+
export declare const DEFAULT_BLE_MAX_INFLIGHT_MSGS = 8;
|
|
42
|
+
export declare const DEFAULT_BLE_MAX_TOTAL_BUFFER_BYTES: number;
|
|
43
|
+
export declare const DEFAULT_BLE_REASSEMBLY_TIMEOUT_MS = 15000;
|
|
44
|
+
export declare const BLE_METHOD_GET = 1;
|
|
45
|
+
export declare const BLE_METHOD_POST = 2;
|
|
46
|
+
export declare class BleTransportError extends Error {
|
|
47
|
+
readonly code: string;
|
|
48
|
+
constructor(code: string, message: string);
|
|
49
|
+
}
|
|
50
|
+
export interface BleChunkView {
|
|
51
|
+
readonly flags: number;
|
|
52
|
+
readonly msgId: number;
|
|
53
|
+
readonly chunkIdx: number;
|
|
54
|
+
readonly totalChunks: number;
|
|
55
|
+
readonly payload: Uint8Array;
|
|
56
|
+
}
|
|
57
|
+
export interface BleRpcRequest {
|
|
58
|
+
readonly method: "GET" | "POST";
|
|
59
|
+
readonly path: string;
|
|
60
|
+
readonly body: Uint8Array;
|
|
61
|
+
}
|
|
62
|
+
export interface BleRpcResponse {
|
|
63
|
+
readonly status: number;
|
|
64
|
+
readonly body: Uint8Array;
|
|
65
|
+
}
|
|
66
|
+
export interface BleStationInfoPayload {
|
|
67
|
+
readonly v: number;
|
|
68
|
+
readonly nh: number;
|
|
69
|
+
readonly pk: string;
|
|
70
|
+
readonly id: string;
|
|
71
|
+
}
|
|
72
|
+
export type BleReassemblyOutcome = {
|
|
73
|
+
readonly complete: false;
|
|
74
|
+
readonly msgId: number;
|
|
75
|
+
readonly receivedChunks: number;
|
|
76
|
+
readonly totalChunks: number;
|
|
77
|
+
} | {
|
|
78
|
+
readonly complete: true;
|
|
79
|
+
readonly flags: number;
|
|
80
|
+
readonly msgId: number;
|
|
81
|
+
readonly payload: Uint8Array;
|
|
82
|
+
};
|
|
83
|
+
export declare function validateBleRpcPath(path: string): void;
|
|
84
|
+
export declare function encodeBleRequest(method: "GET" | "POST", path: string, body?: Uint8Array): Uint8Array;
|
|
85
|
+
export declare function decodeBleRequest(bytes: Uint8Array): BleRpcRequest;
|
|
86
|
+
export declare function encodeBleResponse(status: number, body?: Uint8Array): Uint8Array;
|
|
87
|
+
export declare function decodeBleResponse(bytes: Uint8Array): BleRpcResponse;
|
|
88
|
+
export declare function chunkBleMessage(flags: number, msgId: number, payload: Uint8Array, attValueMtu: number): Uint8Array[];
|
|
89
|
+
export declare function parseBleChunk(chunk: Uint8Array): BleChunkView;
|
|
90
|
+
export interface BleResponseReassemblerOptions {
|
|
91
|
+
readonly maxInflightMsgs?: number;
|
|
92
|
+
readonly maxTotalBytes?: number;
|
|
93
|
+
readonly maxMessageBytes?: number;
|
|
94
|
+
readonly timeoutMs?: number;
|
|
95
|
+
readonly nowMs?: () => number;
|
|
96
|
+
}
|
|
97
|
+
export declare class BleResponseReassembler {
|
|
98
|
+
private readonly maxInflightMsgs;
|
|
99
|
+
private readonly maxTotalBytes;
|
|
100
|
+
private readonly maxMessageBytes;
|
|
101
|
+
private readonly timeoutMs;
|
|
102
|
+
private readonly nowMsFn;
|
|
103
|
+
private readonly inflight;
|
|
104
|
+
private totalBytes;
|
|
105
|
+
constructor(options?: BleResponseReassemblerOptions);
|
|
106
|
+
get bufferedBytes(): number;
|
|
107
|
+
get inflightCount(): number;
|
|
108
|
+
clear(): void;
|
|
109
|
+
deleteMsg(msgId: number): void;
|
|
110
|
+
sweepExpired(nowMs?: number): void;
|
|
111
|
+
feedChunk(chunk: Uint8Array, nowMs?: number): BleReassemblyOutcome;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Minimal platform-agnostic BLE GATT characteristic link implemented by Android/iOS/Web Bluetooth
|
|
115
|
+
* or test harnesses. Zero OS or UI framework dependencies.
|
|
116
|
+
*/
|
|
117
|
+
export interface BleGattLink {
|
|
118
|
+
/** Optional stable BLE peer/station identifier used in `ble://<peerId>` baseUrl. */
|
|
119
|
+
readonly peerId?: string;
|
|
120
|
+
/** Currently negotiated ATT characteristic value MTU (`20..=512`, i.e. `ATT_MTU - 3`). */
|
|
121
|
+
getAttValueMtu(): number;
|
|
122
|
+
/** Writes one `OUTPOST_BLE_V1` chunk frame to `OUTPOST_BLE_RX_CHAR_UUID`. */
|
|
123
|
+
writeRx(chunk: Uint8Array): Promise<void>;
|
|
124
|
+
/** Subscribes to inbound TX Notify chunk frames from `OUTPOST_BLE_TX_CHAR_UUID`; returns unsubscribe fn. */
|
|
125
|
+
subscribeTx(handler: (chunk: Uint8Array) => void): () => void;
|
|
126
|
+
/** Optional read of `OUTPOST_BLE_INFO_CHAR_UUID` (`<= 128 B` JSON metadata). */
|
|
127
|
+
readInfo?(): Promise<Uint8Array>;
|
|
128
|
+
}
|
|
129
|
+
export interface BleWitnessClientOptions {
|
|
130
|
+
/** Optional synthetic `ble://<id>` label (defaults to `ble://${link.peerId ?? "station"}`). */
|
|
131
|
+
readonly baseUrl?: string;
|
|
132
|
+
/** Per-request timeout in milliseconds (defaults to `15_000` for BLE links). */
|
|
133
|
+
readonly timeoutMs?: number;
|
|
134
|
+
/** Hard response byte cap (defaults to `MAX_BLE_RESPONSE_BYTES` = 128 KiB). */
|
|
135
|
+
readonly maxResponseBytes?: number;
|
|
136
|
+
/** Maximum concurrent in-flight BLE requests multiplexed on this link (defaults to `8`). */
|
|
137
|
+
readonly maxInflightRequests?: number;
|
|
138
|
+
/** Optional clock provider for deterministic tests. */
|
|
139
|
+
readonly nowMs?: () => number;
|
|
140
|
+
}
|
|
141
|
+
export declare function parseBleStationInfoPayload(raw: Uint8Array): BleStationInfoPayload;
|
|
142
|
+
/**
|
|
143
|
+
* Full `WitnessClient` speaking `OUTPOST_BLE_V1` over a [`BleGattLink`].
|
|
144
|
+
*
|
|
145
|
+
* Because it extends `WitnessClient`, it drops directly into `@outpost-protocol/receiver` (and `@outpost-protocol/sdk`)
|
|
146
|
+
* (`orchestrateWitnessSync`, `broadcastFraudProof`, mixed BLE + Wi-Fi 2-of-3 quorums)
|
|
147
|
+
* with zero changes to POS business logic.
|
|
148
|
+
*/
|
|
149
|
+
export declare class BleWitnessClient extends WitnessClient {
|
|
150
|
+
private readonly link;
|
|
151
|
+
private readonly disposeTransport;
|
|
152
|
+
constructor(link: BleGattLink, options?: BleWitnessClientOptions);
|
|
153
|
+
/**
|
|
154
|
+
* Reads and parses the compact `OUTPOST_BLE_INFO_CHAR_UUID` characteristic (`{ v, nh, pk, id }`)
|
|
155
|
+
* before sending any RPCs, if the underlying `BleGattLink` exposes `readInfo`.
|
|
156
|
+
*/
|
|
157
|
+
readBleInfo(): Promise<BleStationInfoPayload>;
|
|
158
|
+
/** Unsubscribes TX notifications and rejects any pending requests cleanly. Idempotent. */
|
|
159
|
+
dispose(): void;
|
|
160
|
+
}
|
|
161
|
+
export declare function createBleWitnessClient(link: BleGattLink, options?: BleWitnessClientOptions): BleWitnessClient;
|
|
162
|
+
/** Helper decoding raw response bytes to UTF-8 text (useful for diagnostics). */
|
|
163
|
+
export declare function decodeBleBodyUtf8(body: Uint8Array): string;
|