@inversealtruism/cairn-sdk 0.1.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/chain.cjs ADDED
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/chain.ts
21
+ var chain_exports = {};
22
+ __export(chain_exports, {
23
+ ADDR_RE: () => import_csd_crypto.ADDR_RE,
24
+ Chain: () => Chain,
25
+ CsdClient: () => import_csd_client.CsdClient,
26
+ LightClient: () => import_csd_light.LightClient,
27
+ addrFromPriv: () => import_csd_crypto.addrFromPriv,
28
+ addrFromPub: () => import_csd_crypto.addrFromPub,
29
+ buildAttest: () => import_csd_tx.buildAttest,
30
+ buildPropose: () => import_csd_tx.buildPropose,
31
+ buildSend: () => import_csd_tx.buildSend,
32
+ canonicalJson: () => import_csd_codec.canonicalJson,
33
+ deserialize: () => import_csd_codec.deserialize,
34
+ expectedBits: () => import_csd_light.expectedBits,
35
+ expectedBitsFromWindow: () => import_csd_light.expectedBitsFromWindow,
36
+ hash160: () => import_csd_crypto.hash160,
37
+ hb: () => import_csd_codec.hb,
38
+ headerHash: () => import_csd_codec.headerHash,
39
+ hx: () => import_csd_codec.hx,
40
+ isValidAddr: () => import_csd_crypto.isValidAddr,
41
+ isValidPriv: () => import_csd_crypto.isValidPriv,
42
+ keygen: () => import_csd_crypto.keygen,
43
+ merkleBranch: () => import_csd_codec.merkleBranch,
44
+ merkleRoot: () => import_csd_codec.merkleRoot,
45
+ payloadHash: () => import_csd_codec.payloadHash,
46
+ pubFromPriv: () => import_csd_crypto.pubFromPriv,
47
+ randomNonce: () => import_csd_crypto.randomNonce,
48
+ rpcHeaderToHeader: () => import_csd_client.rpcHeaderToHeader,
49
+ rpcTxToTx: () => import_csd_client.rpcTxToTx,
50
+ selectInputs: () => import_csd_tx.selectInputs,
51
+ serialize: () => import_csd_codec.serialize,
52
+ sha256d: () => import_csd_codec.sha256d,
53
+ sighash: () => import_csd_codec.sighash,
54
+ signDigest: () => import_csd_crypto.signDigest,
55
+ signTx: () => import_csd_tx.signTx,
56
+ strip0x: () => import_csd_codec.strip0x,
57
+ strippedTx: () => import_csd_codec.strippedTx,
58
+ txSize: () => import_csd_tx.txSize,
59
+ txToNodeJson: () => import_csd_tx.txToNodeJson,
60
+ txid: () => import_csd_codec.txid,
61
+ verifyContentBytes: () => import_csd_codec.verifyContentBytes,
62
+ verifyDigest: () => import_csd_crypto.verifyDigest,
63
+ verifyMerkleProof: () => import_csd_codec.verifyMerkleProof
64
+ });
65
+ module.exports = __toCommonJS(chain_exports);
66
+ var import_csd_client = require("@inversealtruism/csd-client");
67
+ var import_csd_tx = require("@inversealtruism/csd-tx");
68
+ var import_csd_codec = require("@inversealtruism/csd-codec");
69
+ var import_csd_crypto = require("@inversealtruism/csd-crypto");
70
+ var import_csd_light = require("@inversealtruism/csd-light");
71
+ var import_csd_client2 = require("@inversealtruism/csd-client");
72
+ var import_csd_light2 = require("@inversealtruism/csd-light");
73
+ var Chain = class {
74
+ client;
75
+ opts;
76
+ _light = null;
77
+ constructor(opts) {
78
+ this.opts = opts;
79
+ this.client = new import_csd_client2.CsdClient({ baseUrl: opts.rpcUrl, fetch: opts.fetch, timeoutMs: opts.timeoutMs });
80
+ }
81
+ /** Current chain tip ({ tip, height, chainwork }). */
82
+ tip() {
83
+ return this.client.tip();
84
+ }
85
+ /** Spendable UTXOs + confirmed balance for an address. */
86
+ utxos(addr) {
87
+ return this.client.utxos(addr);
88
+ }
89
+ /** Submit a signed transaction (node-shaped JSON from `signTx().nodeJson`). */
90
+ submit(nodeJsonTx) {
91
+ return this.client.submit(nodeJsonTx);
92
+ }
93
+ /** A verifying light client over the same RPC (built once, reused). */
94
+ light() {
95
+ if (!this._light) {
96
+ this._light = new import_csd_light2.LightClient({ client: this.client });
97
+ }
98
+ return this._light;
99
+ }
100
+ };
101
+ // Annotate the CommonJS export names for ESM import in node:
102
+ 0 && (module.exports = {
103
+ ADDR_RE,
104
+ Chain,
105
+ CsdClient,
106
+ LightClient,
107
+ addrFromPriv,
108
+ addrFromPub,
109
+ buildAttest,
110
+ buildPropose,
111
+ buildSend,
112
+ canonicalJson,
113
+ deserialize,
114
+ expectedBits,
115
+ expectedBitsFromWindow,
116
+ hash160,
117
+ hb,
118
+ headerHash,
119
+ hx,
120
+ isValidAddr,
121
+ isValidPriv,
122
+ keygen,
123
+ merkleBranch,
124
+ merkleRoot,
125
+ payloadHash,
126
+ pubFromPriv,
127
+ randomNonce,
128
+ rpcHeaderToHeader,
129
+ rpcTxToTx,
130
+ selectInputs,
131
+ serialize,
132
+ sha256d,
133
+ sighash,
134
+ signDigest,
135
+ signTx,
136
+ strip0x,
137
+ strippedTx,
138
+ txSize,
139
+ txToNodeJson,
140
+ txid,
141
+ verifyContentBytes,
142
+ verifyDigest,
143
+ verifyMerkleProof
144
+ });
@@ -0,0 +1,36 @@
1
+ import * as _inversealtruism_csd_client from '@inversealtruism/csd-client';
2
+ import { CsdClient } from '@inversealtruism/csd-client';
3
+ export { ClientOptions, CsdClient, RpcBlock, RpcHeaderJson, RpcSubmit, RpcTip, RpcTxJson, RpcUtxo, RpcUtxos, rpcHeaderToHeader, rpcTxToTx } from '@inversealtruism/csd-client';
4
+ export { BuildResult, Selection, Signed, Utxo, buildAttest, buildPropose, buildSend, selectInputs, signTx, txSize, txToNodeJson } from '@inversealtruism/csd-tx';
5
+ export { App, BlockHeader, Tx, TxInput, TxOutput, canonicalJson, deserialize, hb, headerHash, hx, merkleBranch, merkleRoot, payloadHash, serialize, sha256d, sighash, strip0x, strippedTx, txid, verifyContentBytes, verifyMerkleProof } from '@inversealtruism/csd-codec';
6
+ export { ADDR_RE, addrFromPriv, addrFromPub, hash160, isValidAddr, isValidPriv, keygen, pubFromPriv, randomNonce, signDigest, verifyDigest } from '@inversealtruism/csd-crypto';
7
+ import { LightClient } from '@inversealtruism/csd-light';
8
+ export { InclusionResult, LightClient, ReorgResult, TrustLevel, VerifiedHeader, expectedBits, expectedBitsFromWindow } from '@inversealtruism/csd-light';
9
+ import { F as FetchLike } from './http-D2CzfOsm.cjs';
10
+
11
+ interface ChainOptions {
12
+ /** Node RPC base URL — defaults to the public Cairn proxy (no local node needed). */
13
+ rpcUrl: string;
14
+ fetch?: FetchLike;
15
+ timeoutMs?: number;
16
+ }
17
+ /**
18
+ * Convenience handle over a CSD node RPC. `client` is the full typed RPC client;
19
+ * `light()` lazily builds a verifying light client over the same endpoint.
20
+ */
21
+ declare class Chain {
22
+ readonly client: CsdClient;
23
+ private readonly opts;
24
+ private _light;
25
+ constructor(opts: ChainOptions);
26
+ /** Current chain tip ({ tip, height, chainwork }). */
27
+ tip(): Promise<_inversealtruism_csd_client.RpcTip>;
28
+ /** Spendable UTXOs + confirmed balance for an address. */
29
+ utxos(addr: string): Promise<_inversealtruism_csd_client.RpcUtxos>;
30
+ /** Submit a signed transaction (node-shaped JSON from `signTx().nodeJson`). */
31
+ submit(nodeJsonTx: unknown): Promise<_inversealtruism_csd_client.RpcSubmit>;
32
+ /** A verifying light client over the same RPC (built once, reused). */
33
+ light(): LightClient;
34
+ }
35
+
36
+ export { Chain, type ChainOptions };
@@ -0,0 +1,36 @@
1
+ import * as _inversealtruism_csd_client from '@inversealtruism/csd-client';
2
+ import { CsdClient } from '@inversealtruism/csd-client';
3
+ export { ClientOptions, CsdClient, RpcBlock, RpcHeaderJson, RpcSubmit, RpcTip, RpcTxJson, RpcUtxo, RpcUtxos, rpcHeaderToHeader, rpcTxToTx } from '@inversealtruism/csd-client';
4
+ export { BuildResult, Selection, Signed, Utxo, buildAttest, buildPropose, buildSend, selectInputs, signTx, txSize, txToNodeJson } from '@inversealtruism/csd-tx';
5
+ export { App, BlockHeader, Tx, TxInput, TxOutput, canonicalJson, deserialize, hb, headerHash, hx, merkleBranch, merkleRoot, payloadHash, serialize, sha256d, sighash, strip0x, strippedTx, txid, verifyContentBytes, verifyMerkleProof } from '@inversealtruism/csd-codec';
6
+ export { ADDR_RE, addrFromPriv, addrFromPub, hash160, isValidAddr, isValidPriv, keygen, pubFromPriv, randomNonce, signDigest, verifyDigest } from '@inversealtruism/csd-crypto';
7
+ import { LightClient } from '@inversealtruism/csd-light';
8
+ export { InclusionResult, LightClient, ReorgResult, TrustLevel, VerifiedHeader, expectedBits, expectedBitsFromWindow } from '@inversealtruism/csd-light';
9
+ import { F as FetchLike } from './http-D2CzfOsm.js';
10
+
11
+ interface ChainOptions {
12
+ /** Node RPC base URL — defaults to the public Cairn proxy (no local node needed). */
13
+ rpcUrl: string;
14
+ fetch?: FetchLike;
15
+ timeoutMs?: number;
16
+ }
17
+ /**
18
+ * Convenience handle over a CSD node RPC. `client` is the full typed RPC client;
19
+ * `light()` lazily builds a verifying light client over the same endpoint.
20
+ */
21
+ declare class Chain {
22
+ readonly client: CsdClient;
23
+ private readonly opts;
24
+ private _light;
25
+ constructor(opts: ChainOptions);
26
+ /** Current chain tip ({ tip, height, chainwork }). */
27
+ tip(): Promise<_inversealtruism_csd_client.RpcTip>;
28
+ /** Spendable UTXOs + confirmed balance for an address. */
29
+ utxos(addr: string): Promise<_inversealtruism_csd_client.RpcUtxos>;
30
+ /** Submit a signed transaction (node-shaped JSON from `signTx().nodeJson`). */
31
+ submit(nodeJsonTx: unknown): Promise<_inversealtruism_csd_client.RpcSubmit>;
32
+ /** A verifying light client over the same RPC (built once, reused). */
33
+ light(): LightClient;
34
+ }
35
+
36
+ export { Chain, type ChainOptions };
package/dist/chain.js ADDED
@@ -0,0 +1,86 @@
1
+ import {
2
+ ADDR_RE,
3
+ Chain,
4
+ CsdClient,
5
+ LightClient,
6
+ addrFromPriv,
7
+ addrFromPub,
8
+ buildAttest,
9
+ buildPropose,
10
+ buildSend,
11
+ canonicalJson,
12
+ deserialize,
13
+ expectedBits,
14
+ expectedBitsFromWindow,
15
+ hash160,
16
+ hb,
17
+ headerHash,
18
+ hx,
19
+ isValidAddr,
20
+ isValidPriv,
21
+ keygen,
22
+ merkleBranch,
23
+ merkleRoot,
24
+ payloadHash,
25
+ pubFromPriv,
26
+ randomNonce,
27
+ rpcHeaderToHeader,
28
+ rpcTxToTx,
29
+ selectInputs,
30
+ serialize,
31
+ sha256d,
32
+ sighash,
33
+ signDigest,
34
+ signTx,
35
+ strip0x,
36
+ strippedTx,
37
+ txSize,
38
+ txToNodeJson,
39
+ txid,
40
+ verifyContentBytes,
41
+ verifyDigest,
42
+ verifyMerkleProof
43
+ } from "./chunk-6VWLWF7R.js";
44
+ export {
45
+ ADDR_RE,
46
+ Chain,
47
+ CsdClient,
48
+ LightClient,
49
+ addrFromPriv,
50
+ addrFromPub,
51
+ buildAttest,
52
+ buildPropose,
53
+ buildSend,
54
+ canonicalJson,
55
+ deserialize,
56
+ expectedBits,
57
+ expectedBitsFromWindow,
58
+ hash160,
59
+ hb,
60
+ headerHash,
61
+ hx,
62
+ isValidAddr,
63
+ isValidPriv,
64
+ keygen,
65
+ merkleBranch,
66
+ merkleRoot,
67
+ payloadHash,
68
+ pubFromPriv,
69
+ randomNonce,
70
+ rpcHeaderToHeader,
71
+ rpcTxToTx,
72
+ selectInputs,
73
+ serialize,
74
+ sha256d,
75
+ sighash,
76
+ signDigest,
77
+ signTx,
78
+ strip0x,
79
+ strippedTx,
80
+ txSize,
81
+ txToNodeJson,
82
+ txid,
83
+ verifyContentBytes,
84
+ verifyDigest,
85
+ verifyMerkleProof
86
+ };
@@ -0,0 +1,121 @@
1
+ // src/chain.ts
2
+ import {
3
+ CsdClient,
4
+ rpcTxToTx,
5
+ rpcHeaderToHeader
6
+ } from "@inversealtruism/csd-client";
7
+ import {
8
+ selectInputs,
9
+ txSize,
10
+ txToNodeJson,
11
+ signTx,
12
+ buildSend,
13
+ buildPropose,
14
+ buildAttest
15
+ } from "@inversealtruism/csd-tx";
16
+ import {
17
+ payloadHash,
18
+ canonicalJson,
19
+ verifyContentBytes,
20
+ txid,
21
+ sighash,
22
+ serialize,
23
+ deserialize,
24
+ strippedTx,
25
+ merkleRoot,
26
+ merkleBranch,
27
+ verifyMerkleProof,
28
+ headerHash,
29
+ hb,
30
+ hx,
31
+ strip0x,
32
+ sha256d
33
+ } from "@inversealtruism/csd-codec";
34
+ import {
35
+ keygen,
36
+ pubFromPriv,
37
+ addrFromPriv,
38
+ addrFromPub,
39
+ hash160,
40
+ isValidAddr,
41
+ isValidPriv,
42
+ signDigest,
43
+ verifyDigest,
44
+ randomNonce,
45
+ ADDR_RE
46
+ } from "@inversealtruism/csd-crypto";
47
+ import { LightClient, expectedBits, expectedBitsFromWindow } from "@inversealtruism/csd-light";
48
+ import { CsdClient as CsdClient2 } from "@inversealtruism/csd-client";
49
+ import { LightClient as LightClient2 } from "@inversealtruism/csd-light";
50
+ var Chain = class {
51
+ client;
52
+ opts;
53
+ _light = null;
54
+ constructor(opts) {
55
+ this.opts = opts;
56
+ this.client = new CsdClient2({ baseUrl: opts.rpcUrl, fetch: opts.fetch, timeoutMs: opts.timeoutMs });
57
+ }
58
+ /** Current chain tip ({ tip, height, chainwork }). */
59
+ tip() {
60
+ return this.client.tip();
61
+ }
62
+ /** Spendable UTXOs + confirmed balance for an address. */
63
+ utxos(addr) {
64
+ return this.client.utxos(addr);
65
+ }
66
+ /** Submit a signed transaction (node-shaped JSON from `signTx().nodeJson`). */
67
+ submit(nodeJsonTx) {
68
+ return this.client.submit(nodeJsonTx);
69
+ }
70
+ /** A verifying light client over the same RPC (built once, reused). */
71
+ light() {
72
+ if (!this._light) {
73
+ this._light = new LightClient2({ client: this.client });
74
+ }
75
+ return this._light;
76
+ }
77
+ };
78
+
79
+ export {
80
+ Chain,
81
+ CsdClient,
82
+ rpcTxToTx,
83
+ rpcHeaderToHeader,
84
+ selectInputs,
85
+ txSize,
86
+ txToNodeJson,
87
+ signTx,
88
+ buildSend,
89
+ buildPropose,
90
+ buildAttest,
91
+ payloadHash,
92
+ canonicalJson,
93
+ verifyContentBytes,
94
+ txid,
95
+ sighash,
96
+ serialize,
97
+ deserialize,
98
+ strippedTx,
99
+ merkleRoot,
100
+ merkleBranch,
101
+ verifyMerkleProof,
102
+ headerHash,
103
+ hb,
104
+ hx,
105
+ strip0x,
106
+ sha256d,
107
+ keygen,
108
+ pubFromPriv,
109
+ addrFromPriv,
110
+ addrFromPub,
111
+ hash160,
112
+ isValidAddr,
113
+ isValidPriv,
114
+ signDigest,
115
+ verifyDigest,
116
+ randomNonce,
117
+ ADDR_RE,
118
+ LightClient,
119
+ expectedBits,
120
+ expectedBitsFromWindow
121
+ };
@@ -0,0 +1,76 @@
1
+ import {
2
+ ContentVerificationError
3
+ } from "./chunk-PHMU3CG5.js";
4
+
5
+ // src/content.ts
6
+ import { payloadHash, canonicalJson, verifyContentBytes } from "@inversealtruism/csd-codec";
7
+ var ContentClient = class {
8
+ sources;
9
+ constructor(opts) {
10
+ this.sources = [];
11
+ if (opts.swarm) this.sources.push({ name: "swarm", http: opts.swarm, path: (h) => `/content/${h}` });
12
+ if (opts.indexer) this.sources.push({ name: "indexer", http: opts.indexer, path: (h) => `/content/${h}` });
13
+ if (opts.cairn) this.sources.push({ name: "cairn", http: opts.cairn, path: (h) => `/content/${h}` });
14
+ }
15
+ /**
16
+ * Compute the canonical hash + bytes for an object, ready to anchor on-chain.
17
+ * This is byte-identical to what the cairn server and wallet compute.
18
+ */
19
+ prepare(obj) {
20
+ const canonical = canonicalJson(obj);
21
+ return {
22
+ payloadHash: payloadHash(obj),
23
+ canonical,
24
+ bytes: new TextEncoder().encode(canonical)
25
+ };
26
+ }
27
+ /** Alias for `prepare()` — emphasises this does not itself publish (anchor via board.propose). */
28
+ put(obj) {
29
+ return this.prepare(obj);
30
+ }
31
+ /** Just the payload hash for an object. */
32
+ hash(obj) {
33
+ return payloadHash(obj);
34
+ }
35
+ /**
36
+ * Fetch content by hash and VERIFY it self-certifies (sha256(bytes) == hash).
37
+ * Tries each configured source in order. Returns raw bytes, or null if no
38
+ * source holds it. Throws ContentVerificationError if a source returns bytes
39
+ * that don't match the hash (tampering / wrong object).
40
+ */
41
+ async getBytes(hash) {
42
+ const h = normalizeHash(hash);
43
+ for (const src of this.sources) {
44
+ let got = null;
45
+ try {
46
+ got = await src.http.getBytes(src.path(h));
47
+ } catch {
48
+ continue;
49
+ }
50
+ if (!got) continue;
51
+ if (!verifyContentBytes(got.bytes, h)) {
52
+ throw new ContentVerificationError(h);
53
+ }
54
+ return got.bytes;
55
+ }
56
+ return null;
57
+ }
58
+ /** Fetch + verify + parse as UTF-8 JSON. Returns null if not found. */
59
+ async get(hash) {
60
+ const bytes = await this.getBytes(hash);
61
+ if (!bytes) return null;
62
+ return JSON.parse(new TextDecoder().decode(bytes));
63
+ }
64
+ /** Verify already-fetched bytes against a hash (no network). */
65
+ verify(bytes, hash) {
66
+ return verifyContentBytes(bytes, normalizeHash(hash));
67
+ }
68
+ };
69
+ function normalizeHash(hash) {
70
+ const h = hash.startsWith("0x") || hash.startsWith("0X") ? hash : `0x${hash}`;
71
+ return h.toLowerCase();
72
+ }
73
+
74
+ export {
75
+ ContentClient
76
+ };