@peridotvault/peridotbase-sdk 0.0.0-stage → 0.2.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/auth.d.ts +33 -0
- package/dist/auth.js +72 -0
- package/dist/client.d.ts +1149 -0
- package/dist/client.js +6030 -0
- package/dist/ddl.d.ts +31 -0
- package/dist/ddl.js +28 -0
- package/dist/errors.d.ts +32 -0
- package/dist/errors.js +187 -0
- package/dist/expr.d.ts +127 -0
- package/dist/expr.js +1177 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +28 -0
- package/dist/org-multisig.d.ts +29 -0
- package/dist/org-multisig.js +90 -0
- package/dist/pda.d.ts +95 -0
- package/dist/pda.js +327 -0
- package/dist/sql.d.ts +244 -0
- package/dist/sql.js +1523 -0
- package/dist/tooling.d.ts +68 -0
- package/dist/tooling.js +432 -0
- package/dist/v3.d.ts +357 -0
- package/dist/v3.js +1649 -0
- package/dist/values.d.ts +229 -0
- package/dist/values.js +1362 -0
- package/dist/verify.d.ts +37 -0
- package/dist/verify.js +210 -0
- package/idl/peridotbase_core.json +17404 -0
- package/package.json +43 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from "./values";
|
|
2
|
+
export * from "./pda";
|
|
3
|
+
export * from "./expr";
|
|
4
|
+
export * from "./sql";
|
|
5
|
+
export * from "./errors";
|
|
6
|
+
export * from "./client";
|
|
7
|
+
export * from "./org-multisig";
|
|
8
|
+
export * from "./ddl";
|
|
9
|
+
export * from "./v3";
|
|
10
|
+
export * from "./auth";
|
|
11
|
+
export * from "./tooling";
|
|
12
|
+
export * from "./verify";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./values"), exports);
|
|
18
|
+
__exportStar(require("./pda"), exports);
|
|
19
|
+
__exportStar(require("./expr"), exports);
|
|
20
|
+
__exportStar(require("./sql"), exports);
|
|
21
|
+
__exportStar(require("./errors"), exports);
|
|
22
|
+
__exportStar(require("./client"), exports);
|
|
23
|
+
__exportStar(require("./org-multisig"), exports);
|
|
24
|
+
__exportStar(require("./ddl"), exports);
|
|
25
|
+
__exportStar(require("./v3"), exports);
|
|
26
|
+
__exportStar(require("./auth"), exports);
|
|
27
|
+
__exportStar(require("./tooling"), exports);
|
|
28
|
+
__exportStar(require("./verify"), exports);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
/** Bit-mask values shared with the program (`state/org_policy.rs`). */
|
|
3
|
+
export declare const ORG_ACTION: {
|
|
4
|
+
readonly transferOwner: number;
|
|
5
|
+
readonly rename: number;
|
|
6
|
+
readonly addMember: number;
|
|
7
|
+
readonly removeMember: number;
|
|
8
|
+
readonly updateMember: number;
|
|
9
|
+
readonly setPolicy: number;
|
|
10
|
+
readonly closeDatabase: number;
|
|
11
|
+
};
|
|
12
|
+
export declare const ORG_ACTION_ALL: number;
|
|
13
|
+
/** `sha256(domain || organization || [action] || payload)`; mirrors Rust. */
|
|
14
|
+
export declare function organizationActionDigest(action: number, organization: PublicKey, payload: Buffer): Buffer;
|
|
15
|
+
export declare function transferOwnerDigest(organization: PublicKey, newOwner: PublicKey): Buffer;
|
|
16
|
+
export declare function renameDigest(organization: PublicKey, name: string): Buffer;
|
|
17
|
+
export declare function addMemberDigest(organization: PublicKey, principal: PublicKey, role: "admin" | "member"): Buffer;
|
|
18
|
+
/** Authorization for `beginCloseDatabase`; binds the exact database PDA. */
|
|
19
|
+
export declare function closeDatabaseDigest(organization: PublicKey, database: PublicKey): Buffer;
|
|
20
|
+
export declare function removeMemberDigest(organization: PublicKey, principal: PublicKey): Buffer;
|
|
21
|
+
export declare function updateMemberDigest(organization: PublicKey, principal: PublicKey, role: "admin" | "member"): Buffer;
|
|
22
|
+
/** Mirrors `policy_digest` in `instructions/org_multisig.rs`. */
|
|
23
|
+
export declare function setPolicyDigest(organization: PublicKey, policy: PublicKey, args: {
|
|
24
|
+
signers: PublicKey[];
|
|
25
|
+
threshold: number;
|
|
26
|
+
guardedActions: number;
|
|
27
|
+
recoveryAuthority?: PublicKey;
|
|
28
|
+
recoveryDelaySecs?: number;
|
|
29
|
+
}): Buffer;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//! Canonical payloads for the optional organization multisig. The on-chain
|
|
3
|
+
//! guard hashes exactly these bytes, so an approval can never be retargeted by
|
|
4
|
+
//! editing the instruction arguments client-side.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ORG_ACTION_ALL = exports.ORG_ACTION = void 0;
|
|
7
|
+
exports.organizationActionDigest = organizationActionDigest;
|
|
8
|
+
exports.transferOwnerDigest = transferOwnerDigest;
|
|
9
|
+
exports.renameDigest = renameDigest;
|
|
10
|
+
exports.addMemberDigest = addMemberDigest;
|
|
11
|
+
exports.closeDatabaseDigest = closeDatabaseDigest;
|
|
12
|
+
exports.removeMemberDigest = removeMemberDigest;
|
|
13
|
+
exports.updateMemberDigest = updateMemberDigest;
|
|
14
|
+
exports.setPolicyDigest = setPolicyDigest;
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
/** Bit-mask values shared with the program (`state/org_policy.rs`). */
|
|
18
|
+
exports.ORG_ACTION = {
|
|
19
|
+
transferOwner: 1 << 0,
|
|
20
|
+
rename: 1 << 1,
|
|
21
|
+
addMember: 1 << 2,
|
|
22
|
+
removeMember: 1 << 3,
|
|
23
|
+
updateMember: 1 << 4,
|
|
24
|
+
setPolicy: 1 << 5,
|
|
25
|
+
closeDatabase: 1 << 6,
|
|
26
|
+
};
|
|
27
|
+
exports.ORG_ACTION_ALL = exports.ORG_ACTION.transferOwner |
|
|
28
|
+
exports.ORG_ACTION.rename |
|
|
29
|
+
exports.ORG_ACTION.addMember |
|
|
30
|
+
exports.ORG_ACTION.removeMember |
|
|
31
|
+
exports.ORG_ACTION.updateMember |
|
|
32
|
+
exports.ORG_ACTION.setPolicy |
|
|
33
|
+
exports.ORG_ACTION.closeDatabase;
|
|
34
|
+
const DOMAIN = Buffer.from("peridotbase:org-action:v1");
|
|
35
|
+
function u32le(value) {
|
|
36
|
+
const buffer = Buffer.alloc(4);
|
|
37
|
+
buffer.writeUInt32LE(value);
|
|
38
|
+
return buffer;
|
|
39
|
+
}
|
|
40
|
+
function roleByte(role) {
|
|
41
|
+
return role === "admin" ? 0 : 1;
|
|
42
|
+
}
|
|
43
|
+
/** `sha256(domain || organization || [action] || payload)`; mirrors Rust. */
|
|
44
|
+
function organizationActionDigest(action, organization, payload) {
|
|
45
|
+
return (0, crypto_1.createHash)("sha256")
|
|
46
|
+
.update(DOMAIN)
|
|
47
|
+
.update(organization.toBuffer())
|
|
48
|
+
.update(Buffer.from([action]))
|
|
49
|
+
.update(payload)
|
|
50
|
+
.digest();
|
|
51
|
+
}
|
|
52
|
+
function transferOwnerDigest(organization, newOwner) {
|
|
53
|
+
return organizationActionDigest(exports.ORG_ACTION.transferOwner, organization, newOwner.toBuffer());
|
|
54
|
+
}
|
|
55
|
+
function renameDigest(organization, name) {
|
|
56
|
+
const encoded = Buffer.from(name);
|
|
57
|
+
return organizationActionDigest(exports.ORG_ACTION.rename, organization, Buffer.concat([u32le(encoded.length), encoded]));
|
|
58
|
+
}
|
|
59
|
+
function addMemberDigest(organization, principal, role) {
|
|
60
|
+
return memberDigest(exports.ORG_ACTION.addMember, organization, principal, role);
|
|
61
|
+
}
|
|
62
|
+
/** Authorization for `beginCloseDatabase`; binds the exact database PDA. */
|
|
63
|
+
function closeDatabaseDigest(organization, database) {
|
|
64
|
+
return organizationActionDigest(exports.ORG_ACTION.closeDatabase, organization, database.toBuffer());
|
|
65
|
+
}
|
|
66
|
+
function removeMemberDigest(organization, principal) {
|
|
67
|
+
return memberDigest(exports.ORG_ACTION.removeMember, organization, principal);
|
|
68
|
+
}
|
|
69
|
+
function updateMemberDigest(organization, principal, role) {
|
|
70
|
+
return memberDigest(exports.ORG_ACTION.updateMember, organization, principal, role);
|
|
71
|
+
}
|
|
72
|
+
/** Mirrors `policy_digest` in `instructions/org_multisig.rs`. */
|
|
73
|
+
function setPolicyDigest(organization, policy, args) {
|
|
74
|
+
const recovery = args.recoveryAuthority ?? web3_js_1.PublicKey.default;
|
|
75
|
+
const delay = args.recoveryDelaySecs ?? 0;
|
|
76
|
+
const payload = Buffer.concat([
|
|
77
|
+
u32le(args.signers.length),
|
|
78
|
+
...args.signers.map((signer) => signer.toBuffer()),
|
|
79
|
+
Buffer.from([args.threshold, args.guardedActions]),
|
|
80
|
+
recovery.toBuffer(),
|
|
81
|
+
u32le(delay),
|
|
82
|
+
]);
|
|
83
|
+
return organizationActionDigest(exports.ORG_ACTION.setPolicy, policy, payload);
|
|
84
|
+
}
|
|
85
|
+
function memberDigest(action, organization, principal, role) {
|
|
86
|
+
const payload = role === undefined
|
|
87
|
+
? principal.toBuffer()
|
|
88
|
+
: Buffer.concat([principal.toBuffer(), Buffer.from([roleByte(role)])]);
|
|
89
|
+
return organizationActionDigest(action, organization, payload);
|
|
90
|
+
}
|
package/dist/pda.d.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
/** Program ID of the deployed shared DB Core Program. */
|
|
3
|
+
export declare const PROGRAM_ID: PublicKey;
|
|
4
|
+
export declare function u64le(value: number | bigint): Buffer;
|
|
5
|
+
export declare function u16le(value: number): Buffer;
|
|
6
|
+
export declare function u32le(value: number): Buffer;
|
|
7
|
+
/** `["protocol"]` — one per deployed program. */
|
|
8
|
+
export declare function protocolPda(programId?: PublicKey): [PublicKey, number];
|
|
9
|
+
/** `["pd", "project", organization, project_id LE]` */
|
|
10
|
+
export declare function projectPda(organization: PublicKey, projectId: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
11
|
+
/** `["pd", "database", project, database_id LE]` */
|
|
12
|
+
export declare function databasePda(project: PublicKey, databaseId: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
13
|
+
/** `["pd", "table", database, table_id LE]` */
|
|
14
|
+
export declare function tablePda(database: PublicKey, tableId: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
15
|
+
/** `["pd", "tablename", database, name_hash]` */
|
|
16
|
+
/** `["pd", "type", database, type_name_hash]` */
|
|
17
|
+
export declare function typePda(database: PublicKey, nameHash: Uint8Array | Buffer | number[], programId?: PublicKey): [PublicKey, number];
|
|
18
|
+
/** `["pd", "schema", database, schema_name_hash]` */
|
|
19
|
+
export declare function schemaPda(database: PublicKey, nameHash: Uint8Array | Buffer | number[], programId?: PublicKey): [PublicKey, number];
|
|
20
|
+
export declare function tableNameRegistryPda(database: PublicKey, schema: PublicKey, nameHash: Uint8Array | Buffer | number[], programId?: PublicKey): [PublicKey, number];
|
|
21
|
+
/** SHA-256 of the lowercased name; must match `table_name_hash` on-chain. */
|
|
22
|
+
export declare function tableNameHash(name: string): Promise<Uint8Array>;
|
|
23
|
+
/** `["pd", "page", table, shard_id LE, page_no LE]` */
|
|
24
|
+
export declare function dataPagePda(table: PublicKey, shardId: number, pageNo: number, programId?: PublicKey): [PublicKey, number];
|
|
25
|
+
/** `["pd", "shards", table]` */
|
|
26
|
+
export declare function shardDirectoryPda(table: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
27
|
+
/** `["pd", "pkidx", table, bucket_id LE, chain_no LE]` */
|
|
28
|
+
export declare function pkIndexPagePda(table: PublicKey, bucketId: number, chainNo: number, programId?: PublicKey): [PublicKey, number];
|
|
29
|
+
/** `["pd", "ovfb", table, key]` */
|
|
30
|
+
export declare function overflowBlobPda(table: PublicKey, key: Uint8Array, programId?: PublicKey): [PublicKey, number];
|
|
31
|
+
/** `["pd", "ovf", table, key, chunk_no LE]` */
|
|
32
|
+
export declare function overflowChunkPda(table: PublicKey, key: Uint8Array, chunkNo: number, programId?: PublicKey): [PublicKey, number];
|
|
33
|
+
/** `["pd", "index", table, index_id LE]` */
|
|
34
|
+
export declare function indexCatalogPda(table: PublicKey, indexId: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
35
|
+
/** `["pd", "ipage", table, index_id LE, bucket_id LE, page_no LE]` */
|
|
36
|
+
export declare function indexPagePda(table: PublicKey, indexId: number | bigint, bucketId: number, pageNo: number, programId?: PublicKey): [PublicKey, number];
|
|
37
|
+
/** `["pd", "audit-trail", database]` */
|
|
38
|
+
export declare function auditTrailPda(database: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
39
|
+
/** `["pd", "sponsor-allowance", sponsor, grantee]` */
|
|
40
|
+
export declare function sponsorAllowancePda(sponsor: PublicKey, grantee: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
41
|
+
/** `["pd", "audit-event", database, sequence_u64_LE]` */
|
|
42
|
+
export declare function auditEventPda(database: PublicKey, sequence: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
43
|
+
/** `["pd", "role", database, principal]` */
|
|
44
|
+
export declare function roleGrantPda(database: PublicKey, principal: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
45
|
+
/**
|
|
46
|
+
* FNV-1a 64-bit hash of canonical primary-key bytes, mapping to a shard.
|
|
47
|
+
* Must match `shard_for_key` in the Rust program.
|
|
48
|
+
*/
|
|
49
|
+
export declare function shardForKey(keyBytes: Buffer | Uint8Array, shardCount: number): number;
|
|
50
|
+
/** Maps canonical index-key bytes to an index bucket (same hash family). */
|
|
51
|
+
export declare function bucketForKey(keyBytes: Buffer | Uint8Array, bucketCount: number): number;
|
|
52
|
+
/** Canonical bytes of a value used for shard/index placement. */
|
|
53
|
+
export declare function keyBytesOf(value: unknown): Buffer;
|
|
54
|
+
/** Stable on-chain organization address; ownership transfers do not change it. */
|
|
55
|
+
export declare function organizationPda(creator: PublicKey, organizationId: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
56
|
+
/** `["pd", "extension", table]` — opt-in CPI extension policy. */
|
|
57
|
+
export declare function extensionPolicyPda(table: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
58
|
+
export declare function organizationMemberPda(organization: PublicKey, principal: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
59
|
+
/** `["pd", "org-policy", organization]` — opt-in governance policy. */
|
|
60
|
+
export declare function organizationPolicyPda(organization: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
61
|
+
/** `["pd", "org-prop", organization, proposalId]` — pending approval. */
|
|
62
|
+
export declare function organizationProposalPda(organization: PublicKey, proposalId: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
63
|
+
/** `["pd", "storage-v3", table]` — storage descriptor. */
|
|
64
|
+
export declare function storagePdaV3(table: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
65
|
+
/** Domain-separated digest of a canonical parent-key value for FK counters. */
|
|
66
|
+
export declare function fkKeyDigest(canonicalKey: Uint8Array): Buffer;
|
|
67
|
+
/** `["pd", "history-v3", table, pk_digest, revision_u64_LE]` */
|
|
68
|
+
export declare function historyPdaV3(table: PublicKey, pkDigest: Buffer | Uint8Array, revision: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
69
|
+
/** `["pd", "fk-count", parent_table, key_digest]` */
|
|
70
|
+
export declare function fkCounterPdaV3(parentTable: PublicKey, keyDigest: Uint8Array, programId?: PublicKey): [PublicKey, number];
|
|
71
|
+
/**
|
|
72
|
+
* `["pd", "active-v3", table]`.
|
|
73
|
+
*
|
|
74
|
+
* Its existence is the program-enforced retirement of the v2 layout, so v2 DML
|
|
75
|
+
* builders always pass it (empty for tables that were never cut over).
|
|
76
|
+
*/
|
|
77
|
+
export declare function activationPdaV3(table: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
78
|
+
/** `["pd", "record-v3", table, epoch_u64_LE, D]`. */
|
|
79
|
+
export declare function recordPdaV3(table: PublicKey, epoch: number | bigint, digest: Uint8Array | Buffer, programId?: PublicKey): [PublicKey, number];
|
|
80
|
+
/** `["pd", "dirlane-v3", table, epoch_u64_LE, lane_u32_LE]`. */
|
|
81
|
+
export declare function dirLanePdaV3(table: PublicKey, epoch: number | bigint, lane: number, programId?: PublicKey): [PublicKey, number];
|
|
82
|
+
/** `["pd", "dirpage-v3", table, epoch_u64_LE, lane_u32_LE, page_u64_LE]`. */
|
|
83
|
+
export declare function dirPagePdaV3(table: PublicKey, epoch: number | bigint, lane: number, page: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
84
|
+
/** `["pd", "unique-v3", table, index_id_digest32, U]`. */
|
|
85
|
+
export declare function uniqueClaimPdaV3(table: PublicKey, indexIdDigest: Uint8Array | Buffer, digest: Uint8Array | Buffer, programId?: PublicKey): [PublicKey, number];
|
|
86
|
+
/** `["pd", "postlane-v3", table, index_id_digest32, value_digest32, lane_u32_LE]`. */
|
|
87
|
+
export declare function postLanePdaV3(table: PublicKey, indexIdDigest: Uint8Array | Buffer, valueDigest: Uint8Array | Buffer, lane: number, programId?: PublicKey): [PublicKey, number];
|
|
88
|
+
/** `["pd", "postpage-v3", table, index_id_digest32, value_digest32, lane_u32_LE, page_u64_LE]`. */
|
|
89
|
+
export declare function postPagePdaV3(table: PublicKey, indexIdDigest: Uint8Array | Buffer, valueDigest: Uint8Array | Buffer, lane: number, page: number | bigint, programId?: PublicKey): [PublicKey, number];
|
|
90
|
+
/** `["pd", "extgate-v3", database]`. */
|
|
91
|
+
export declare function extGatePdaV3(database: PublicKey, programId?: PublicKey): [PublicKey, number];
|
|
92
|
+
/** `IDX = SHA256("peridotbase:index-id:v3" || table || epoch_u64_LE || index_id_u64_LE)`. */
|
|
93
|
+
export declare function indexIdDigestV3(table: PublicKey, epoch: number | bigint, indexId: number | bigint): Buffer;
|
|
94
|
+
/** `lane = first_u32_LE(digest) & (laneCount - 1)`; laneCount must be power-of-two. */
|
|
95
|
+
export declare function laneForDigest(digest: Uint8Array | Buffer, laneCount: number): number;
|
package/dist/pda.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PROGRAM_ID = void 0;
|
|
4
|
+
exports.u64le = u64le;
|
|
5
|
+
exports.u16le = u16le;
|
|
6
|
+
exports.u32le = u32le;
|
|
7
|
+
exports.protocolPda = protocolPda;
|
|
8
|
+
exports.projectPda = projectPda;
|
|
9
|
+
exports.databasePda = databasePda;
|
|
10
|
+
exports.tablePda = tablePda;
|
|
11
|
+
exports.typePda = typePda;
|
|
12
|
+
exports.schemaPda = schemaPda;
|
|
13
|
+
exports.tableNameRegistryPda = tableNameRegistryPda;
|
|
14
|
+
exports.tableNameHash = tableNameHash;
|
|
15
|
+
exports.dataPagePda = dataPagePda;
|
|
16
|
+
exports.shardDirectoryPda = shardDirectoryPda;
|
|
17
|
+
exports.pkIndexPagePda = pkIndexPagePda;
|
|
18
|
+
exports.overflowBlobPda = overflowBlobPda;
|
|
19
|
+
exports.overflowChunkPda = overflowChunkPda;
|
|
20
|
+
exports.indexCatalogPda = indexCatalogPda;
|
|
21
|
+
exports.indexPagePda = indexPagePda;
|
|
22
|
+
exports.auditTrailPda = auditTrailPda;
|
|
23
|
+
exports.sponsorAllowancePda = sponsorAllowancePda;
|
|
24
|
+
exports.auditEventPda = auditEventPda;
|
|
25
|
+
exports.roleGrantPda = roleGrantPda;
|
|
26
|
+
exports.shardForKey = shardForKey;
|
|
27
|
+
exports.bucketForKey = bucketForKey;
|
|
28
|
+
exports.keyBytesOf = keyBytesOf;
|
|
29
|
+
exports.organizationPda = organizationPda;
|
|
30
|
+
exports.extensionPolicyPda = extensionPolicyPda;
|
|
31
|
+
exports.organizationMemberPda = organizationMemberPda;
|
|
32
|
+
exports.organizationPolicyPda = organizationPolicyPda;
|
|
33
|
+
exports.organizationProposalPda = organizationProposalPda;
|
|
34
|
+
exports.storagePdaV3 = storagePdaV3;
|
|
35
|
+
exports.fkKeyDigest = fkKeyDigest;
|
|
36
|
+
exports.historyPdaV3 = historyPdaV3;
|
|
37
|
+
exports.fkCounterPdaV3 = fkCounterPdaV3;
|
|
38
|
+
exports.activationPdaV3 = activationPdaV3;
|
|
39
|
+
exports.recordPdaV3 = recordPdaV3;
|
|
40
|
+
exports.dirLanePdaV3 = dirLanePdaV3;
|
|
41
|
+
exports.dirPagePdaV3 = dirPagePdaV3;
|
|
42
|
+
exports.uniqueClaimPdaV3 = uniqueClaimPdaV3;
|
|
43
|
+
exports.postLanePdaV3 = postLanePdaV3;
|
|
44
|
+
exports.postPagePdaV3 = postPagePdaV3;
|
|
45
|
+
exports.extGatePdaV3 = extGatePdaV3;
|
|
46
|
+
exports.indexIdDigestV3 = indexIdDigestV3;
|
|
47
|
+
exports.laneForDigest = laneForDigest;
|
|
48
|
+
const crypto_1 = require("crypto");
|
|
49
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
50
|
+
const values_1 = require("./values");
|
|
51
|
+
/** Program ID of the deployed shared DB Core Program. */
|
|
52
|
+
exports.PROGRAM_ID = new web3_js_1.PublicKey("AzhEnpdLzZHf7ro8NfqGpmuSqBHxA7bViTRezFKha6X3");
|
|
53
|
+
const PREFIX = Buffer.from("pd");
|
|
54
|
+
function u64le(value) {
|
|
55
|
+
const buffer = Buffer.alloc(8);
|
|
56
|
+
buffer.writeBigUInt64LE(BigInt(value.toString()));
|
|
57
|
+
return buffer;
|
|
58
|
+
}
|
|
59
|
+
function u16le(value) {
|
|
60
|
+
const buffer = Buffer.alloc(2);
|
|
61
|
+
buffer.writeUInt16LE(value);
|
|
62
|
+
return buffer;
|
|
63
|
+
}
|
|
64
|
+
function u32le(value) {
|
|
65
|
+
const buffer = Buffer.alloc(4);
|
|
66
|
+
buffer.writeUInt32LE(value);
|
|
67
|
+
return buffer;
|
|
68
|
+
}
|
|
69
|
+
/** `["protocol"]` — one per deployed program. */
|
|
70
|
+
function protocolPda(programId = exports.PROGRAM_ID) {
|
|
71
|
+
return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("protocol")], programId);
|
|
72
|
+
}
|
|
73
|
+
/** `["pd", "project", organization, project_id LE]` */
|
|
74
|
+
function projectPda(organization, projectId, programId = exports.PROGRAM_ID) {
|
|
75
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("project"), organization.toBuffer(), u64le(projectId)], programId);
|
|
76
|
+
}
|
|
77
|
+
/** `["pd", "database", project, database_id LE]` */
|
|
78
|
+
function databasePda(project, databaseId, programId = exports.PROGRAM_ID) {
|
|
79
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("database"), project.toBuffer(), u64le(databaseId)], programId);
|
|
80
|
+
}
|
|
81
|
+
/** `["pd", "table", database, table_id LE]` */
|
|
82
|
+
function tablePda(database, tableId, programId = exports.PROGRAM_ID) {
|
|
83
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("table"), database.toBuffer(), u64le(tableId)], programId);
|
|
84
|
+
}
|
|
85
|
+
/** `["pd", "tablename", database, name_hash]` */
|
|
86
|
+
/** `["pd", "type", database, type_name_hash]` */
|
|
87
|
+
function typePda(database, nameHash, programId = exports.PROGRAM_ID) {
|
|
88
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("type"), database.toBuffer(), Buffer.from(nameHash)], programId);
|
|
89
|
+
}
|
|
90
|
+
/** `["pd", "schema", database, schema_name_hash]` */
|
|
91
|
+
function schemaPda(database, nameHash, programId = exports.PROGRAM_ID) {
|
|
92
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("schema"), database.toBuffer(), Buffer.from(nameHash)], programId);
|
|
93
|
+
}
|
|
94
|
+
function tableNameRegistryPda(database, schema, nameHash, programId = exports.PROGRAM_ID) {
|
|
95
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
96
|
+
PREFIX,
|
|
97
|
+
Buffer.from("tablename"),
|
|
98
|
+
database.toBuffer(),
|
|
99
|
+
schema.toBuffer(),
|
|
100
|
+
Buffer.from(nameHash),
|
|
101
|
+
], programId);
|
|
102
|
+
}
|
|
103
|
+
/** SHA-256 of the lowercased name; must match `table_name_hash` on-chain. */
|
|
104
|
+
async function tableNameHash(name) {
|
|
105
|
+
const bytes = new TextEncoder().encode(name.toLowerCase());
|
|
106
|
+
const subtle = globalThis.crypto?.subtle;
|
|
107
|
+
if (subtle)
|
|
108
|
+
return new Uint8Array(await subtle.digest("SHA-256", bytes));
|
|
109
|
+
throw new Error("SHA-256 requires WebCrypto; use Node 19+ or a browser");
|
|
110
|
+
}
|
|
111
|
+
/** `["pd", "page", table, shard_id LE, page_no LE]` */
|
|
112
|
+
function dataPagePda(table, shardId, pageNo, programId = exports.PROGRAM_ID) {
|
|
113
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
114
|
+
PREFIX,
|
|
115
|
+
Buffer.from("page"),
|
|
116
|
+
table.toBuffer(),
|
|
117
|
+
u16le(shardId),
|
|
118
|
+
u16le(pageNo),
|
|
119
|
+
], programId);
|
|
120
|
+
}
|
|
121
|
+
/** `["pd", "shards", table]` */
|
|
122
|
+
function shardDirectoryPda(table, programId = exports.PROGRAM_ID) {
|
|
123
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("shards"), table.toBuffer()], programId);
|
|
124
|
+
}
|
|
125
|
+
/** `["pd", "pkidx", table, bucket_id LE, chain_no LE]` */
|
|
126
|
+
function pkIndexPagePda(table, bucketId, chainNo, programId = exports.PROGRAM_ID) {
|
|
127
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
128
|
+
PREFIX,
|
|
129
|
+
Buffer.from("pkidx"),
|
|
130
|
+
table.toBuffer(),
|
|
131
|
+
u16le(bucketId),
|
|
132
|
+
u16le(chainNo),
|
|
133
|
+
], programId);
|
|
134
|
+
}
|
|
135
|
+
/** `["pd", "ovfb", table, key]` */
|
|
136
|
+
function overflowBlobPda(table, key, programId = exports.PROGRAM_ID) {
|
|
137
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("ovfb"), table.toBuffer(), Buffer.from(key)], programId);
|
|
138
|
+
}
|
|
139
|
+
/** `["pd", "ovf", table, key, chunk_no LE]` */
|
|
140
|
+
function overflowChunkPda(table, key, chunkNo, programId = exports.PROGRAM_ID) {
|
|
141
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
142
|
+
PREFIX,
|
|
143
|
+
Buffer.from("ovf"),
|
|
144
|
+
table.toBuffer(),
|
|
145
|
+
Buffer.from(key),
|
|
146
|
+
u16le(chunkNo),
|
|
147
|
+
], programId);
|
|
148
|
+
}
|
|
149
|
+
/** `["pd", "index", table, index_id LE]` */
|
|
150
|
+
function indexCatalogPda(table, indexId, programId = exports.PROGRAM_ID) {
|
|
151
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("index"), table.toBuffer(), u64le(indexId)], programId);
|
|
152
|
+
}
|
|
153
|
+
/** `["pd", "ipage", table, index_id LE, bucket_id LE, page_no LE]` */
|
|
154
|
+
function indexPagePda(table, indexId, bucketId, pageNo, programId = exports.PROGRAM_ID) {
|
|
155
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
156
|
+
PREFIX,
|
|
157
|
+
Buffer.from("ipage"),
|
|
158
|
+
table.toBuffer(),
|
|
159
|
+
u64le(indexId),
|
|
160
|
+
u16le(bucketId),
|
|
161
|
+
u16le(pageNo),
|
|
162
|
+
], programId);
|
|
163
|
+
}
|
|
164
|
+
/** `["pd", "audit-trail", database]` */
|
|
165
|
+
function auditTrailPda(database, programId = exports.PROGRAM_ID) {
|
|
166
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("audit-trail"), database.toBuffer()], programId);
|
|
167
|
+
}
|
|
168
|
+
/** `["pd", "sponsor-allowance", sponsor, grantee]` */
|
|
169
|
+
function sponsorAllowancePda(sponsor, grantee, programId = exports.PROGRAM_ID) {
|
|
170
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("sponsor-allowance"), sponsor.toBuffer(), grantee.toBuffer()], programId);
|
|
171
|
+
}
|
|
172
|
+
/** `["pd", "audit-event", database, sequence_u64_LE]` */
|
|
173
|
+
function auditEventPda(database, sequence, programId = exports.PROGRAM_ID) {
|
|
174
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("audit-event"), database.toBuffer(), u64le(sequence)], programId);
|
|
175
|
+
}
|
|
176
|
+
/** `["pd", "role", database, principal]` */
|
|
177
|
+
function roleGrantPda(database, principal, programId = exports.PROGRAM_ID) {
|
|
178
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("role"), database.toBuffer(), principal.toBuffer()], programId);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* FNV-1a 64-bit hash of canonical primary-key bytes, mapping to a shard.
|
|
182
|
+
* Must match `shard_for_key` in the Rust program.
|
|
183
|
+
*/
|
|
184
|
+
function shardForKey(keyBytes, shardCount) {
|
|
185
|
+
let hash = 0xcbf29ce484222325n;
|
|
186
|
+
const mask = (1n << 64n) - 1n;
|
|
187
|
+
for (const byte of keyBytes) {
|
|
188
|
+
hash ^= BigInt(byte);
|
|
189
|
+
hash = (hash * 0x100000001b3n) & mask;
|
|
190
|
+
}
|
|
191
|
+
const count = BigInt(Math.max(1, shardCount));
|
|
192
|
+
return Number(hash % count);
|
|
193
|
+
}
|
|
194
|
+
/** Maps canonical index-key bytes to an index bucket (same hash family). */
|
|
195
|
+
function bucketForKey(keyBytes, bucketCount) {
|
|
196
|
+
return shardForKey(keyBytes, bucketCount);
|
|
197
|
+
}
|
|
198
|
+
/** Canonical bytes of a value used for shard/index placement. */
|
|
199
|
+
function keyBytesOf(value) {
|
|
200
|
+
return (0, values_1.encodeTypedValue)(value);
|
|
201
|
+
}
|
|
202
|
+
/** Stable on-chain organization address; ownership transfers do not change it. */
|
|
203
|
+
function organizationPda(creator, organizationId, programId = exports.PROGRAM_ID) {
|
|
204
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("organization"), creator.toBuffer(), u64le(organizationId)], programId);
|
|
205
|
+
}
|
|
206
|
+
/** `["pd", "extension", table]` — opt-in CPI extension policy. */
|
|
207
|
+
function extensionPolicyPda(table, programId = exports.PROGRAM_ID) {
|
|
208
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("extension"), table.toBuffer()], programId);
|
|
209
|
+
}
|
|
210
|
+
function organizationMemberPda(organization, principal, programId = exports.PROGRAM_ID) {
|
|
211
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("org-member"), organization.toBuffer(), principal.toBuffer()], programId);
|
|
212
|
+
}
|
|
213
|
+
/** `["pd", "org-policy", organization]` — opt-in governance policy. */
|
|
214
|
+
function organizationPolicyPda(organization, programId = exports.PROGRAM_ID) {
|
|
215
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("org-policy"), organization.toBuffer()], programId);
|
|
216
|
+
}
|
|
217
|
+
/** `["pd", "org-prop", organization, proposalId]` — pending approval. */
|
|
218
|
+
function organizationProposalPda(organization, proposalId, programId = exports.PROGRAM_ID) {
|
|
219
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("org-prop"), organization.toBuffer(), u64le(proposalId)], programId);
|
|
220
|
+
}
|
|
221
|
+
// --- Storage v3 (record-PDA layout) ---------------------------------------
|
|
222
|
+
/** `["pd", "storage-v3", table]` — storage descriptor. */
|
|
223
|
+
function storagePdaV3(table, programId = exports.PROGRAM_ID) {
|
|
224
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("storage-v3"), table.toBuffer()], programId);
|
|
225
|
+
}
|
|
226
|
+
/** Domain-separated digest of a canonical parent-key value for FK counters. */
|
|
227
|
+
function fkKeyDigest(canonicalKey) {
|
|
228
|
+
return (0, crypto_1.createHash)("sha256")
|
|
229
|
+
.update(Buffer.concat([Buffer.from("peridotbase:fk-counter:v1"), Buffer.from(canonicalKey)]))
|
|
230
|
+
.digest();
|
|
231
|
+
}
|
|
232
|
+
/** `["pd", "history-v3", table, pk_digest, revision_u64_LE]` */
|
|
233
|
+
function historyPdaV3(table, pkDigest, revision, programId = exports.PROGRAM_ID) {
|
|
234
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
235
|
+
PREFIX,
|
|
236
|
+
Buffer.from("history-v3"),
|
|
237
|
+
table.toBuffer(),
|
|
238
|
+
Buffer.from(pkDigest),
|
|
239
|
+
u64le(revision),
|
|
240
|
+
], programId);
|
|
241
|
+
}
|
|
242
|
+
/** `["pd", "fk-count", parent_table, key_digest]` */
|
|
243
|
+
function fkCounterPdaV3(parentTable, keyDigest, programId = exports.PROGRAM_ID) {
|
|
244
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("fk-count"), parentTable.toBuffer(), Buffer.from(keyDigest)], programId);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* `["pd", "active-v3", table]`.
|
|
248
|
+
*
|
|
249
|
+
* Its existence is the program-enforced retirement of the v2 layout, so v2 DML
|
|
250
|
+
* builders always pass it (empty for tables that were never cut over).
|
|
251
|
+
*/
|
|
252
|
+
function activationPdaV3(table, programId = exports.PROGRAM_ID) {
|
|
253
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("active-v3"), table.toBuffer()], programId);
|
|
254
|
+
}
|
|
255
|
+
/** `["pd", "record-v3", table, epoch_u64_LE, D]`. */
|
|
256
|
+
function recordPdaV3(table, epoch, digest, programId = exports.PROGRAM_ID) {
|
|
257
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("record-v3"), table.toBuffer(), u64le(epoch), Buffer.from(digest)], programId);
|
|
258
|
+
}
|
|
259
|
+
/** `["pd", "dirlane-v3", table, epoch_u64_LE, lane_u32_LE]`. */
|
|
260
|
+
function dirLanePdaV3(table, epoch, lane, programId = exports.PROGRAM_ID) {
|
|
261
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("dirlane-v3"), table.toBuffer(), u64le(epoch), u32le(lane)], programId);
|
|
262
|
+
}
|
|
263
|
+
/** `["pd", "dirpage-v3", table, epoch_u64_LE, lane_u32_LE, page_u64_LE]`. */
|
|
264
|
+
function dirPagePdaV3(table, epoch, lane, page, programId = exports.PROGRAM_ID) {
|
|
265
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
266
|
+
PREFIX,
|
|
267
|
+
Buffer.from("dirpage-v3"),
|
|
268
|
+
table.toBuffer(),
|
|
269
|
+
u64le(epoch),
|
|
270
|
+
u32le(lane),
|
|
271
|
+
u64le(page),
|
|
272
|
+
], programId);
|
|
273
|
+
}
|
|
274
|
+
/** `["pd", "unique-v3", table, index_id_digest32, U]`. */
|
|
275
|
+
function uniqueClaimPdaV3(table, indexIdDigest, digest, programId = exports.PROGRAM_ID) {
|
|
276
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
277
|
+
PREFIX,
|
|
278
|
+
Buffer.from("unique-v3"),
|
|
279
|
+
table.toBuffer(),
|
|
280
|
+
Buffer.from(indexIdDigest),
|
|
281
|
+
Buffer.from(digest),
|
|
282
|
+
], programId);
|
|
283
|
+
}
|
|
284
|
+
/** `["pd", "postlane-v3", table, index_id_digest32, value_digest32, lane_u32_LE]`. */
|
|
285
|
+
function postLanePdaV3(table, indexIdDigest, valueDigest, lane, programId = exports.PROGRAM_ID) {
|
|
286
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
287
|
+
PREFIX,
|
|
288
|
+
Buffer.from("postlane-v3"),
|
|
289
|
+
table.toBuffer(),
|
|
290
|
+
Buffer.from(indexIdDigest),
|
|
291
|
+
Buffer.from(valueDigest),
|
|
292
|
+
u32le(lane),
|
|
293
|
+
], programId);
|
|
294
|
+
}
|
|
295
|
+
/** `["pd", "postpage-v3", table, index_id_digest32, value_digest32, lane_u32_LE, page_u64_LE]`. */
|
|
296
|
+
function postPagePdaV3(table, indexIdDigest, valueDigest, lane, page, programId = exports.PROGRAM_ID) {
|
|
297
|
+
return web3_js_1.PublicKey.findProgramAddressSync([
|
|
298
|
+
PREFIX,
|
|
299
|
+
Buffer.from("postpage-v3"),
|
|
300
|
+
table.toBuffer(),
|
|
301
|
+
Buffer.from(indexIdDigest),
|
|
302
|
+
Buffer.from(valueDigest),
|
|
303
|
+
u32le(lane),
|
|
304
|
+
u64le(page),
|
|
305
|
+
], programId);
|
|
306
|
+
}
|
|
307
|
+
/** `["pd", "extgate-v3", database]`. */
|
|
308
|
+
function extGatePdaV3(database, programId = exports.PROGRAM_ID) {
|
|
309
|
+
return web3_js_1.PublicKey.findProgramAddressSync([PREFIX, Buffer.from("extgate-v3"), database.toBuffer()], programId);
|
|
310
|
+
}
|
|
311
|
+
/** `IDX = SHA256("peridotbase:index-id:v3" || table || epoch_u64_LE || index_id_u64_LE)`. */
|
|
312
|
+
function indexIdDigestV3(table, epoch, indexId) {
|
|
313
|
+
return (0, crypto_1.createHash)("sha256")
|
|
314
|
+
.update(values_1.INDEX_ID_V3_DOMAIN)
|
|
315
|
+
.update(table.toBuffer())
|
|
316
|
+
.update(u64le(epoch))
|
|
317
|
+
.update(u64le(indexId))
|
|
318
|
+
.digest();
|
|
319
|
+
}
|
|
320
|
+
/** `lane = first_u32_LE(digest) & (laneCount - 1)`; laneCount must be power-of-two. */
|
|
321
|
+
function laneForDigest(digest, laneCount) {
|
|
322
|
+
if (laneCount <= 0 || (laneCount & (laneCount - 1)) !== 0) {
|
|
323
|
+
throw new Error(`laneCount must be a positive power of two, got ${laneCount}`);
|
|
324
|
+
}
|
|
325
|
+
const view = Buffer.from(digest.buffer, digest.byteOffset, digest.byteLength);
|
|
326
|
+
return (view.readUInt32LE(0) & (laneCount - 1)) >>> 0;
|
|
327
|
+
}
|