@fide.work/fcp 0.0.1-alpha.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/README.md +18 -0
- package/dist/attestation/create.d.ts +173 -0
- package/dist/attestation/create.js +179 -0
- package/dist/attestation/index.d.ts +7 -0
- package/dist/attestation/index.js +7 -0
- package/dist/attestation/verify.d.ts +56 -0
- package/dist/attestation/verify.js +94 -0
- package/dist/broadcasting/config.d.ts +27 -0
- package/dist/broadcasting/config.js +22 -0
- package/dist/broadcasting/index.d.ts +7 -0
- package/dist/broadcasting/index.js +7 -0
- package/dist/broadcasting/registry.d.ts +111 -0
- package/dist/broadcasting/registry.js +99 -0
- package/dist/experimental/attestation/create.d.ts +173 -0
- package/dist/experimental/attestation/create.js +188 -0
- package/dist/experimental/attestation/index.d.ts +7 -0
- package/dist/experimental/attestation/index.js +7 -0
- package/dist/experimental/attestation/verify.d.ts +56 -0
- package/dist/experimental/attestation/verify.js +94 -0
- package/dist/experimental/broadcasting/config.d.ts +27 -0
- package/dist/experimental/broadcasting/config.js +22 -0
- package/dist/experimental/broadcasting/index.d.ts +7 -0
- package/dist/experimental/broadcasting/index.js +7 -0
- package/dist/experimental/broadcasting/registry.d.ts +111 -0
- package/dist/experimental/broadcasting/registry.js +99 -0
- package/dist/experimental/index.d.ts +9 -0
- package/dist/experimental/index.js +13 -0
- package/dist/experimental/merkle/index.d.ts +6 -0
- package/dist/experimental/merkle/index.js +6 -0
- package/dist/experimental/merkle/tree.d.ts +72 -0
- package/dist/experimental/merkle/tree.js +154 -0
- package/dist/experimental/signing/ed25519.d.ts +116 -0
- package/dist/experimental/signing/ed25519.js +161 -0
- package/dist/experimental/signing/eip191.d.ts +50 -0
- package/dist/experimental/signing/eip191.js +96 -0
- package/dist/experimental/signing/eip712.d.ts +112 -0
- package/dist/experimental/signing/eip712.js +187 -0
- package/dist/experimental/signing/index.d.ts +8 -0
- package/dist/experimental/signing/index.js +11 -0
- package/dist/fide-id/calculateFideId.d.ts +21 -0
- package/dist/fide-id/calculateFideId.js +53 -0
- package/dist/fide-id/calculateStatementFideId.d.ts +21 -0
- package/dist/fide-id/calculateStatementFideId.js +38 -0
- package/dist/fide-id/constants.d.ts +43 -0
- package/dist/fide-id/constants.js +55 -0
- package/dist/fide-id/index.d.ts +10 -0
- package/dist/fide-id/index.js +12 -0
- package/dist/fide-id/types.d.ts +52 -0
- package/dist/fide-id/types.js +5 -0
- package/dist/fide-id/utils.d.ts +30 -0
- package/dist/fide-id/utils.js +65 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +19 -0
- package/dist/merkle/index.d.ts +6 -0
- package/dist/merkle/index.js +6 -0
- package/dist/merkle/tree.d.ts +72 -0
- package/dist/merkle/tree.js +154 -0
- package/dist/schema/evaluations.d.ts +31 -0
- package/dist/schema/evaluations.js +31 -0
- package/dist/schema/index.d.ts +6 -0
- package/dist/schema/index.js +6 -0
- package/dist/schema/predicates.d.ts +110 -0
- package/dist/schema/predicates.js +122 -0
- package/dist/signing/ed25519.d.ts +116 -0
- package/dist/signing/ed25519.js +161 -0
- package/dist/signing/eip191.d.ts +50 -0
- package/dist/signing/eip191.js +96 -0
- package/dist/signing/eip712.d.ts +112 -0
- package/dist/signing/eip712.js +187 -0
- package/dist/signing/index.d.ts +8 -0
- package/dist/signing/index.js +11 -0
- package/dist/statement/build.d.ts +99 -0
- package/dist/statement/build.js +71 -0
- package/dist/statement/index.d.ts +6 -0
- package/dist/statement/index.js +6 -0
- package/dist/statement/policy.d.ts +33 -0
- package/dist/statement/policy.js +183 -0
- package/package.json +64 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK EIP-712 Signing Utilities
|
|
3
|
+
* Ethereum-compatible signing using viem (optional peer dependency)
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Default FCP EIP-712 Domain
|
|
7
|
+
*/
|
|
8
|
+
export const FCP_EIP712_DOMAIN = {
|
|
9
|
+
name: 'Fide Context Protocol',
|
|
10
|
+
version: '1',
|
|
11
|
+
chainId: 1, // Mainnet (should be configured per chain)
|
|
12
|
+
verifyingContract: '0x0000000000000000000000000000000000000000'
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Detect which package manager is being used
|
|
16
|
+
*/
|
|
17
|
+
function detectPackageManager() {
|
|
18
|
+
// Check for package manager environment variables
|
|
19
|
+
const userAgent = process.env.npm_config_user_agent || '';
|
|
20
|
+
if (userAgent.includes('pnpm'))
|
|
21
|
+
return 'pnpm add viem';
|
|
22
|
+
if (userAgent.includes('yarn'))
|
|
23
|
+
return 'yarn add viem';
|
|
24
|
+
if (userAgent.includes('bun'))
|
|
25
|
+
return 'bun add viem';
|
|
26
|
+
if (userAgent.includes('npm'))
|
|
27
|
+
return 'npm install viem';
|
|
28
|
+
// Default to npm if we can't detect
|
|
29
|
+
return 'npm install viem';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Check if viem is installed
|
|
33
|
+
*/
|
|
34
|
+
async function checkViemInstalled() {
|
|
35
|
+
try {
|
|
36
|
+
await import('viem');
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
const installCommand = detectPackageManager();
|
|
40
|
+
throw new Error('EIP-712 signing requires viem to be installed.\n\n' +
|
|
41
|
+
`Install it with: ${installCommand}\n\n` +
|
|
42
|
+
'Or use the zero-dependency Ed25519 signing instead:\n' +
|
|
43
|
+
' import { generateEd25519KeyPair, signEd25519 } from \'@fide.work/fcp\'');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get Ethereum address from private key
|
|
48
|
+
*
|
|
49
|
+
* Requires viem to be installed as a peer dependency.
|
|
50
|
+
*
|
|
51
|
+
* @param privateKey - The private key for signing (0x-prefixed hex)
|
|
52
|
+
* @returns Ethereum address
|
|
53
|
+
* @throws Error if viem is not installed
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* Derives the Ethereum address from the private key using:
|
|
57
|
+
* - ECDSA public key derivation
|
|
58
|
+
* - Keccak256 hashing
|
|
59
|
+
* - Last 20 bytes as the address
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const address = await getEthereumAddress(privateKey);
|
|
64
|
+
* // Result: "0x742d35Cc6634C0532925a3b844Bc9e7595f42e"
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export async function getEthereumAddress(privateKey) {
|
|
68
|
+
await checkViemInstalled();
|
|
69
|
+
const { privateKeyToAddress } = await import('viem/accounts');
|
|
70
|
+
return privateKeyToAddress(privateKey);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Sign data using EIP-712 typed data signing
|
|
74
|
+
*
|
|
75
|
+
* Requires viem to be installed as a peer dependency.
|
|
76
|
+
*
|
|
77
|
+
* @param data - The data/message to sign or verify (hex string for EIP standards, plaintext for EIP-191)
|
|
78
|
+
* @param privateKey - The private key for signing (0x-prefixed hex)
|
|
79
|
+
* @param domain - EIP-712 domain (defaults to FCP domain)
|
|
80
|
+
* @returns Hex-encoded signature
|
|
81
|
+
* @throws Error if viem is not installed
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* This function signs data using EIP-712 typed data signing:
|
|
85
|
+
* - Structures the message in FideAttestation format
|
|
86
|
+
* - Uses the provided domain (or defaults to FCP_EIP712_DOMAIN)
|
|
87
|
+
* - Returns an ECDSA signature over the structured hash
|
|
88
|
+
*
|
|
89
|
+
* Supports custom domains for integration with different smart contracts.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const signature = await signEip712(merkleRoot, privateKey);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export async function signEip712(data, privateKey, domain = FCP_EIP712_DOMAIN) {
|
|
97
|
+
await checkViemInstalled();
|
|
98
|
+
const { privateKeyToAccount } = await import('viem/accounts');
|
|
99
|
+
const { hashTypedData, keccak256, toHex } = await import('viem');
|
|
100
|
+
const account = privateKeyToAccount(privateKey);
|
|
101
|
+
// Define the EIP-712 typed data structure
|
|
102
|
+
const types = {
|
|
103
|
+
FideAttestation: [
|
|
104
|
+
{ name: 'data', type: 'string' }
|
|
105
|
+
]
|
|
106
|
+
};
|
|
107
|
+
const message = {
|
|
108
|
+
data
|
|
109
|
+
};
|
|
110
|
+
// Sign using EIP-712
|
|
111
|
+
const signature = await account.signTypedData({
|
|
112
|
+
domain,
|
|
113
|
+
types,
|
|
114
|
+
primaryType: 'FideAttestation',
|
|
115
|
+
message
|
|
116
|
+
});
|
|
117
|
+
return signature;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Verify an EIP-712 signature
|
|
121
|
+
*
|
|
122
|
+
* Requires viem to be installed as a peer dependency.
|
|
123
|
+
*
|
|
124
|
+
* @param data - The data/message to sign or verify (hex string for EIP standards, plaintext for EIP-191)
|
|
125
|
+
* @param signature - The cryptographic signature (0x-prefixed hex string)
|
|
126
|
+
* @param address - The signer's Ethereum address (0x-prefixed hex)
|
|
127
|
+
* @param domain - EIP-712 domain (defaults to FCP domain)
|
|
128
|
+
* @returns True if signature is valid
|
|
129
|
+
* @throws Error if viem is not installed
|
|
130
|
+
*
|
|
131
|
+
* @remarks
|
|
132
|
+
* This function verifies an EIP-712 typed data signature by:
|
|
133
|
+
* - Reconstructing the FideAttestation message structure
|
|
134
|
+
* - Using the provided domain (or defaults to FCP_EIP712_DOMAIN)
|
|
135
|
+
* - Verifying the ECDSA signature against the expected signer address
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const isValid = await verifyEip712(merkleRoot, signature, address);
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export async function verifyEip712(data, signature, address, domain = FCP_EIP712_DOMAIN) {
|
|
143
|
+
await checkViemInstalled();
|
|
144
|
+
const { verifyTypedData } = await import('viem');
|
|
145
|
+
const types = {
|
|
146
|
+
FideAttestation: [
|
|
147
|
+
{ name: 'data', type: 'string' }
|
|
148
|
+
]
|
|
149
|
+
};
|
|
150
|
+
const message = {
|
|
151
|
+
data
|
|
152
|
+
};
|
|
153
|
+
return await verifyTypedData({
|
|
154
|
+
address,
|
|
155
|
+
domain,
|
|
156
|
+
types,
|
|
157
|
+
primaryType: 'FideAttestation',
|
|
158
|
+
message,
|
|
159
|
+
signature
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Create a CAIP-10 identifier for an Ethereum address
|
|
164
|
+
*
|
|
165
|
+
* @param address - The signer's Ethereum address (0x-prefixed hex)
|
|
166
|
+
* @param chainId - Chain ID (defaults to 1 for mainnet)
|
|
167
|
+
* @returns CAIP-10 identifier (e.g., "eip155:1:0x...")
|
|
168
|
+
*
|
|
169
|
+
* @remarks
|
|
170
|
+
* CAIP-10 (Chain Agnostic Improvement Proposal 10) is a standard format for
|
|
171
|
+
* representing blockchain addresses across different chains. This function creates
|
|
172
|
+
* identifiers in the format: `eip155:{chainId}:{address}`
|
|
173
|
+
*
|
|
174
|
+
* Common chainIds:
|
|
175
|
+
* - 1: Ethereum Mainnet
|
|
176
|
+
* - 5: Goerli Testnet
|
|
177
|
+
* - 11155111: Sepolia Testnet
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const caip10 = createEthereumCaip10(address, 1);
|
|
182
|
+
* // Result: "eip155:1:0x742d35cc6634c0532925a3b844bc9e7595f42e"
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export function createEthereumCaip10(address, chainId = 1) {
|
|
186
|
+
return `eip155:${chainId}:${address.toLowerCase()}`;
|
|
187
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK - Signing Module
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all signing utilities from submodules.
|
|
5
|
+
*/
|
|
6
|
+
export { generateEd25519KeyPair, exportEd25519Keys, importEd25519Keys, signEd25519, verifyEd25519, type Ed25519KeyPair, type ExportedEd25519Keys } from "./ed25519.js";
|
|
7
|
+
export { getEthereumAddress, signEip712, verifyEip712, createEthereumCaip10, FCP_EIP712_DOMAIN, type Eip712Domain } from "./eip712.js";
|
|
8
|
+
export { signEip191, verifyEip191 } from "./eip191.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK - Signing Module
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all signing utilities from submodules.
|
|
5
|
+
*/
|
|
6
|
+
// Ed25519 Signing (native, zero-dependency)
|
|
7
|
+
export { generateEd25519KeyPair, exportEd25519Keys, importEd25519Keys, signEd25519, verifyEd25519 } from "./ed25519.js";
|
|
8
|
+
// EIP-712 Signing (requires viem peer dependency)
|
|
9
|
+
export { getEthereumAddress, signEip712, verifyEip712, createEthereumCaip10, FCP_EIP712_DOMAIN } from "./eip712.js";
|
|
10
|
+
// EIP-191 Signing (requires viem peer dependency)
|
|
11
|
+
export { signEip191, verifyEip191 } from "./eip191.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FIDE_ENTITY_TYPE_MAP } from "./constants.js";
|
|
2
|
+
import type { FideEntityType } from "./types.js";
|
|
3
|
+
export { FIDE_ENTITY_TYPE_MAP };
|
|
4
|
+
export type { FideEntityType };
|
|
5
|
+
/**
|
|
6
|
+
* Calculate a Fide ID for an entity from its type, source type, and raw identifier.
|
|
7
|
+
*
|
|
8
|
+
* Generates a unique identifier by creating a SHA-256 hash of the raw identifier,
|
|
9
|
+
* then constructing a Fide ID with type and source characters followed by the last 38 hex characters.
|
|
10
|
+
*
|
|
11
|
+
* @param entityType The entity type.
|
|
12
|
+
* @param sourceEntityType The source entity type.
|
|
13
|
+
* @param rawIdentifier The raw identifier string to hash.
|
|
14
|
+
* @paramDefault entityType Person
|
|
15
|
+
* @paramDefault sourceEntityType Product
|
|
16
|
+
* @paramDefault rawIdentifier https://x.com/alice
|
|
17
|
+
* @returns Promise resolving to the calculated Fide ID with format `did:fide:0x{typeChar}{sourceChar}{fingerprint}`
|
|
18
|
+
* @throws TypeError if rawIdentifier is not a string
|
|
19
|
+
* @throws Error if entityType or sourceEntityType are invalid, or if protocol constraints are violated
|
|
20
|
+
*/
|
|
21
|
+
export declare function calculateFideId(entityType: FideEntityType, sourceEntityType: FideEntityType, rawIdentifier: string): Promise<`did:fide:0x${string}`>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { FIDE_ENTITY_TYPE_MAP } from "./constants.js";
|
|
2
|
+
// Re-export for backward compatibility
|
|
3
|
+
export { FIDE_ENTITY_TYPE_MAP };
|
|
4
|
+
async function getSubtleCrypto() {
|
|
5
|
+
if (globalThis.crypto?.subtle) {
|
|
6
|
+
return globalThis.crypto.subtle;
|
|
7
|
+
}
|
|
8
|
+
const { webcrypto } = await import("node:crypto");
|
|
9
|
+
return webcrypto.subtle;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Calculate a Fide ID for an entity from its type, source type, and raw identifier.
|
|
13
|
+
*
|
|
14
|
+
* Generates a unique identifier by creating a SHA-256 hash of the raw identifier,
|
|
15
|
+
* then constructing a Fide ID with type and source characters followed by the last 38 hex characters.
|
|
16
|
+
*
|
|
17
|
+
* @param entityType The entity type.
|
|
18
|
+
* @param sourceEntityType The source entity type.
|
|
19
|
+
* @param rawIdentifier The raw identifier string to hash.
|
|
20
|
+
* @paramDefault entityType Person
|
|
21
|
+
* @paramDefault sourceEntityType Product
|
|
22
|
+
* @paramDefault rawIdentifier https://x.com/alice
|
|
23
|
+
* @returns Promise resolving to the calculated Fide ID with format `did:fide:0x{typeChar}{sourceChar}{fingerprint}`
|
|
24
|
+
* @throws TypeError if rawIdentifier is not a string
|
|
25
|
+
* @throws Error if entityType or sourceEntityType are invalid, or if protocol constraints are violated
|
|
26
|
+
*/
|
|
27
|
+
export async function calculateFideId(entityType, sourceEntityType, rawIdentifier) {
|
|
28
|
+
if (typeof rawIdentifier !== "string") {
|
|
29
|
+
throw new TypeError(`Invalid rawIdentifier: expected string, got ${typeof rawIdentifier}`);
|
|
30
|
+
}
|
|
31
|
+
if (entityType === "Statement" && sourceEntityType !== "Statement") {
|
|
32
|
+
throw new Error(`Protocol entity ${entityType} must be self-sourced. Expected source type: ${entityType}, got: ${sourceEntityType}`);
|
|
33
|
+
}
|
|
34
|
+
if (sourceEntityType === "Statement" && entityType !== "Statement") {
|
|
35
|
+
throw new Error(`Invalid Statement source for ${entityType}: protocol disallows 0xX0 (Statement-derived) IDs for non-Statement entities. Use a concrete source (e.g. Product, Organization) instead.`);
|
|
36
|
+
}
|
|
37
|
+
const entityTypeCode = FIDE_ENTITY_TYPE_MAP[entityType];
|
|
38
|
+
if (!entityTypeCode) {
|
|
39
|
+
throw new Error(`Invalid entityType: ${String(entityType)}`);
|
|
40
|
+
}
|
|
41
|
+
const sourceEntityTypeCode = FIDE_ENTITY_TYPE_MAP[sourceEntityType];
|
|
42
|
+
if (!sourceEntityTypeCode) {
|
|
43
|
+
throw new Error(`Invalid sourceEntityType: ${String(sourceEntityType)}`);
|
|
44
|
+
}
|
|
45
|
+
const subtle = await getSubtleCrypto();
|
|
46
|
+
const bytes = new TextEncoder().encode(rawIdentifier);
|
|
47
|
+
const digest = await subtle.digest("SHA-256", bytes);
|
|
48
|
+
const hashHex = Array.from(new Uint8Array(digest))
|
|
49
|
+
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
50
|
+
.join("");
|
|
51
|
+
const fingerprint = hashHex.slice(-38);
|
|
52
|
+
return `did:fide:0x${entityTypeCode}${sourceEntityTypeCode}${fingerprint}`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate a Fide ID for a Statement from its RDF triple components.
|
|
3
|
+
*
|
|
4
|
+
* Creates a statement Fide ID by hashing a canonical JSON representation of the subject-predicate-object triple.
|
|
5
|
+
* The resulting Fide ID always has type and source both set to Statement (0x00...).
|
|
6
|
+
*
|
|
7
|
+
* @param subjectFideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
8
|
+
* @param predicateFideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
9
|
+
* @param objectFideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
10
|
+
* @paramDefault subjectFideId did:fide:0x152f02f1d1c1e62b2e569e11818420c1968be3d9
|
|
11
|
+
* @paramDefault predicateFideId did:fide:0x6524b049fa7069dd318c44531214a955c3f1fa37
|
|
12
|
+
* @paramDefault objectFideId did:fide:0x66a023246354ad7e064b1e4e009ec8a0699a3043
|
|
13
|
+
* @returns Promise resolving to the calculated statement Fide ID with format `did:fide:0x00{fingerprint}`
|
|
14
|
+
* @throws TypeError if any Fide ID is not a string
|
|
15
|
+
* @throws Error if any Fide ID format is invalid or not in canonical form
|
|
16
|
+
* @remarks
|
|
17
|
+
* This function only validates Fide ID format and canonicalizes triple hashing.
|
|
18
|
+
* It does not enforce statement role policy (for example allowed predicate entity/source combos).
|
|
19
|
+
* Role policy checks are enforced by `createStatement`.
|
|
20
|
+
*/
|
|
21
|
+
export declare function calculateStatementFideId(subjectFideId: string, predicateFideId: string, objectFideId: string): Promise<`did:fide:0x${string}`>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { calculateFideId } from "./calculateFideId.js";
|
|
2
|
+
function assertFideId(value) {
|
|
3
|
+
if (typeof value !== "string") {
|
|
4
|
+
throw new TypeError(`Invalid Fide ID: expected string, got ${typeof value}`);
|
|
5
|
+
}
|
|
6
|
+
if (/^did:fide:0x[0-9a-f]{40}$/.test(value)) {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
throw new Error(`Invalid Fide ID format: ${value}. Expected full Fide ID (e.g., did:fide:0x...)`);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Calculate a Fide ID for a Statement from its RDF triple components.
|
|
13
|
+
*
|
|
14
|
+
* Creates a statement Fide ID by hashing a canonical JSON representation of the subject-predicate-object triple.
|
|
15
|
+
* The resulting Fide ID always has type and source both set to Statement (0x00...).
|
|
16
|
+
*
|
|
17
|
+
* @param subjectFideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
18
|
+
* @param predicateFideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
19
|
+
* @param objectFideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
20
|
+
* @paramDefault subjectFideId did:fide:0x152f02f1d1c1e62b2e569e11818420c1968be3d9
|
|
21
|
+
* @paramDefault predicateFideId did:fide:0x6524b049fa7069dd318c44531214a955c3f1fa37
|
|
22
|
+
* @paramDefault objectFideId did:fide:0x66a023246354ad7e064b1e4e009ec8a0699a3043
|
|
23
|
+
* @returns Promise resolving to the calculated statement Fide ID with format `did:fide:0x00{fingerprint}`
|
|
24
|
+
* @throws TypeError if any Fide ID is not a string
|
|
25
|
+
* @throws Error if any Fide ID format is invalid or not in canonical form
|
|
26
|
+
* @remarks
|
|
27
|
+
* This function only validates Fide ID format and canonicalizes triple hashing.
|
|
28
|
+
* It does not enforce statement role policy (for example allowed predicate entity/source combos).
|
|
29
|
+
* Role policy checks are enforced by `createStatement`.
|
|
30
|
+
*/
|
|
31
|
+
export async function calculateStatementFideId(subjectFideId, predicateFideId, objectFideId) {
|
|
32
|
+
const rawIdentifier = JSON.stringify({
|
|
33
|
+
o: assertFideId(objectFideId),
|
|
34
|
+
p: assertFideId(predicateFideId),
|
|
35
|
+
s: assertFideId(subjectFideId)
|
|
36
|
+
});
|
|
37
|
+
return calculateFideId("Statement", "Statement", rawIdentifier);
|
|
38
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK Constants
|
|
3
|
+
* Central location for all protocol constants
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Entity Type Map
|
|
7
|
+
* Maps FCP entity type names to their single-character hex codes.
|
|
8
|
+
* Used as both Part 1 (Entity Type) and Part 2 (Identifier Source) in Fide IDs.
|
|
9
|
+
*/
|
|
10
|
+
export declare const FIDE_ENTITY_TYPE_MAP: {
|
|
11
|
+
readonly Statement: "0";
|
|
12
|
+
readonly Person: "1";
|
|
13
|
+
readonly Organization: "2";
|
|
14
|
+
readonly AutonomousAgent: "7";
|
|
15
|
+
readonly Place: "3";
|
|
16
|
+
readonly Event: "4";
|
|
17
|
+
readonly Product: "5";
|
|
18
|
+
readonly CreativeWork: "6";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Reverse lookup: character code to entity type name
|
|
22
|
+
*/
|
|
23
|
+
export declare const FIDE_CHAR_TO_ENTITY_TYPE: Record<string, keyof typeof FIDE_ENTITY_TYPE_MAP>;
|
|
24
|
+
/**
|
|
25
|
+
* Fide ID Prefix
|
|
26
|
+
* All Fide IDs start with this constant prefix (W3C DID format)
|
|
27
|
+
*/
|
|
28
|
+
export declare const FIDE_ID_PREFIX: "did:fide:0x";
|
|
29
|
+
/**
|
|
30
|
+
* Fide ID Length (excluding prefix)
|
|
31
|
+
* 40 hex characters: 1 (type) + 1 (source) + 38 (fingerprint)
|
|
32
|
+
*/
|
|
33
|
+
export declare const FIDE_ID_HEX_LENGTH = 40;
|
|
34
|
+
/**
|
|
35
|
+
* Fide ID Length (including prefix)
|
|
36
|
+
* "did:fide:0x" (11) + 40 hex = 51 characters
|
|
37
|
+
*/
|
|
38
|
+
export declare const FIDE_ID_LENGTH: number;
|
|
39
|
+
/**
|
|
40
|
+
* Fingerprint Length
|
|
41
|
+
* Last 38 hex characters (19 bytes) of SHA-256 hash
|
|
42
|
+
*/
|
|
43
|
+
export declare const FIDE_ID_FINGERPRINT_LENGTH = 38;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK Constants
|
|
3
|
+
* Central location for all protocol constants
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Entity Type Map
|
|
7
|
+
* Maps FCP entity type names to their single-character hex codes.
|
|
8
|
+
* Used as both Part 1 (Entity Type) and Part 2 (Identifier Source) in Fide IDs.
|
|
9
|
+
*/
|
|
10
|
+
export const FIDE_ENTITY_TYPE_MAP = {
|
|
11
|
+
// Fide Context Protocol Types
|
|
12
|
+
Statement: "0",
|
|
13
|
+
// Active Entities
|
|
14
|
+
Person: "1",
|
|
15
|
+
Organization: "2",
|
|
16
|
+
AutonomousAgent: "7",
|
|
17
|
+
// Inactive Entities
|
|
18
|
+
Place: "3",
|
|
19
|
+
Event: "4",
|
|
20
|
+
Product: "5",
|
|
21
|
+
CreativeWork: "6"
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Reverse lookup: character code to entity type name
|
|
25
|
+
*/
|
|
26
|
+
export const FIDE_CHAR_TO_ENTITY_TYPE = {
|
|
27
|
+
"0": "Statement",
|
|
28
|
+
"1": "Person",
|
|
29
|
+
"2": "Organization",
|
|
30
|
+
"7": "AutonomousAgent",
|
|
31
|
+
"3": "Place",
|
|
32
|
+
"4": "Event",
|
|
33
|
+
"5": "Product",
|
|
34
|
+
"6": "CreativeWork"
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Fide ID Prefix
|
|
38
|
+
* All Fide IDs start with this constant prefix (W3C DID format)
|
|
39
|
+
*/
|
|
40
|
+
export const FIDE_ID_PREFIX = "did:fide:0x";
|
|
41
|
+
/**
|
|
42
|
+
* Fide ID Length (excluding prefix)
|
|
43
|
+
* 40 hex characters: 1 (type) + 1 (source) + 38 (fingerprint)
|
|
44
|
+
*/
|
|
45
|
+
export const FIDE_ID_HEX_LENGTH = 40;
|
|
46
|
+
/**
|
|
47
|
+
* Fide ID Length (including prefix)
|
|
48
|
+
* "did:fide:0x" (11) + 40 hex = 51 characters
|
|
49
|
+
*/
|
|
50
|
+
export const FIDE_ID_LENGTH = FIDE_ID_PREFIX.length + FIDE_ID_HEX_LENGTH;
|
|
51
|
+
/**
|
|
52
|
+
* Fingerprint Length
|
|
53
|
+
* Last 38 hex characters (19 bytes) of SHA-256 hash
|
|
54
|
+
*/
|
|
55
|
+
export const FIDE_ID_FINGERPRINT_LENGTH = 38;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK - Fide ID Module
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all Fide ID utilities from submodules.
|
|
5
|
+
*/
|
|
6
|
+
export { calculateFideId } from "./calculateFideId.js";
|
|
7
|
+
export { calculateStatementFideId } from "./calculateStatementFideId.js";
|
|
8
|
+
export { assertFideId, parseFideId, } from "./utils.js";
|
|
9
|
+
export { FIDE_ENTITY_TYPE_MAP, FIDE_CHAR_TO_ENTITY_TYPE, FIDE_ID_PREFIX, FIDE_ID_HEX_LENGTH, FIDE_ID_LENGTH, FIDE_ID_FINGERPRINT_LENGTH } from "./constants.js";
|
|
10
|
+
export type { FideEntityType, FideStatementPredicateEntityType, FideStatementPredicateSourceType, FideEntityTypeChar, FideId, FideFingerprint, ParsedFideId } from "./types.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK - Fide ID Module
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all Fide ID utilities from submodules.
|
|
5
|
+
*/
|
|
6
|
+
// Core calculation functions
|
|
7
|
+
export { calculateFideId } from "./calculateFideId.js";
|
|
8
|
+
export { calculateStatementFideId } from "./calculateStatementFideId.js";
|
|
9
|
+
// Utility functions
|
|
10
|
+
export { assertFideId, parseFideId, } from "./utils.js";
|
|
11
|
+
// Constants
|
|
12
|
+
export { FIDE_ENTITY_TYPE_MAP, FIDE_CHAR_TO_ENTITY_TYPE, FIDE_ID_PREFIX, FIDE_ID_HEX_LENGTH, FIDE_ID_LENGTH, FIDE_ID_FINGERPRINT_LENGTH } from "./constants.js";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK Types
|
|
3
|
+
* Central location for all TypeScript type definitions
|
|
4
|
+
*/
|
|
5
|
+
import { FIDE_ENTITY_TYPE_MAP } from "./constants.js";
|
|
6
|
+
/**
|
|
7
|
+
* Valid FCP entity type names derived from `FIDE_ENTITY_TYPE_MAP`.
|
|
8
|
+
* @docs /fcp/docs/entities
|
|
9
|
+
*/
|
|
10
|
+
export type FideEntityType = keyof typeof FIDE_ENTITY_TYPE_MAP;
|
|
11
|
+
/**
|
|
12
|
+
* Allowed entity types for statement predicates.
|
|
13
|
+
*
|
|
14
|
+
* - `CreativeWork`: schema/ontology predicate IRIs
|
|
15
|
+
* @docs /fcp/docs/schema/#predicate
|
|
16
|
+
*/
|
|
17
|
+
export type FideStatementPredicateEntityType = "CreativeWork";
|
|
18
|
+
/**
|
|
19
|
+
* Allowed source types for statement predicates.
|
|
20
|
+
*
|
|
21
|
+
* - `Product`: predicate IRIs are treated as product-sourced vocabulary terms
|
|
22
|
+
*/
|
|
23
|
+
export type FideStatementPredicateSourceType = "Product";
|
|
24
|
+
/**
|
|
25
|
+
* Single-character FCP type codes (hex) derived from `FIDE_ENTITY_TYPE_MAP`.
|
|
26
|
+
*/
|
|
27
|
+
export type FideEntityTypeChar = typeof FIDE_ENTITY_TYPE_MAP[FideEntityType];
|
|
28
|
+
/**
|
|
29
|
+
* Full Fide ID format (with did:fide:0x prefix)
|
|
30
|
+
*/
|
|
31
|
+
export type FideId = `did:fide:0x${string}`;
|
|
32
|
+
/**
|
|
33
|
+
* Fide ID fingerprint segment (38 hex characters, 19 bytes).
|
|
34
|
+
*/
|
|
35
|
+
export type FideFingerprint = string;
|
|
36
|
+
/**
|
|
37
|
+
* Parsed Fide ID components.
|
|
38
|
+
*/
|
|
39
|
+
export interface ParsedFideId {
|
|
40
|
+
/** The full Fide ID string */
|
|
41
|
+
fideId: FideId;
|
|
42
|
+
/** Entity type code (1 hex char from Part 1 of the Fide ID). */
|
|
43
|
+
typeChar: FideEntityTypeChar;
|
|
44
|
+
/** Source type code (1 hex char from Part 2 of the Fide ID). */
|
|
45
|
+
sourceChar: FideEntityTypeChar;
|
|
46
|
+
/** Resolved entity type name from `FIDE_CHAR_TO_ENTITY_TYPE[typeChar]`. */
|
|
47
|
+
entityType: FideEntityType;
|
|
48
|
+
/** Resolved source type name from `FIDE_CHAR_TO_ENTITY_TYPE[sourceChar]`. */
|
|
49
|
+
sourceType: FideEntityType;
|
|
50
|
+
/** Fingerprint segment (Part 3, 38 hex chars). */
|
|
51
|
+
fingerprint: FideFingerprint;
|
|
52
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK Utilities
|
|
3
|
+
* Helper functions for working with Fide IDs
|
|
4
|
+
*/
|
|
5
|
+
import type { FideId, ParsedFideId } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Assert that a value is a properly formatted Fide ID.
|
|
8
|
+
*
|
|
9
|
+
* Valid format is:
|
|
10
|
+
* `did:fide:0x` prefix followed by exactly 40 hexadecimal characters (case-insensitive).
|
|
11
|
+
*
|
|
12
|
+
* @param value The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
13
|
+
* @paramDefault value did:fide:0x152f02f1d1c1e62b2e569e11818420c1968be3d9
|
|
14
|
+
* @returns void
|
|
15
|
+
* @throws TypeError if value is not a string
|
|
16
|
+
* @throws Error if value is not a valid Fide ID format
|
|
17
|
+
*/
|
|
18
|
+
export declare function assertFideId(value: string): asserts value is FideId;
|
|
19
|
+
/**
|
|
20
|
+
* Parse a Fide ID into its constituent components.
|
|
21
|
+
*
|
|
22
|
+
* Decomposes a Fide ID into entity type, source type, and fingerprint, converting hex characters
|
|
23
|
+
* to their corresponding entity type names.
|
|
24
|
+
*
|
|
25
|
+
* @param fideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
26
|
+
* @paramDefault fideId did:fide:0x152f02f1d1c1e62b2e569e11818420c1968be3d9
|
|
27
|
+
* @returns Parsed Fide ID components. `entityType` and `sourceType` are resolved via `FIDE_CHAR_TO_ENTITY_TYPE`.
|
|
28
|
+
* @throws Error if invalid Fide ID format or if type characters do not map to known entity types
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseFideId(fideId: FideId): ParsedFideId;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FCP SDK Utilities
|
|
3
|
+
* Helper functions for working with Fide IDs
|
|
4
|
+
*/
|
|
5
|
+
import { FIDE_ID_PREFIX, FIDE_ID_LENGTH, FIDE_CHAR_TO_ENTITY_TYPE } from "./constants.js";
|
|
6
|
+
/**
|
|
7
|
+
* Assert that a value is a properly formatted Fide ID.
|
|
8
|
+
*
|
|
9
|
+
* Valid format is:
|
|
10
|
+
* `did:fide:0x` prefix followed by exactly 40 hexadecimal characters (case-insensitive).
|
|
11
|
+
*
|
|
12
|
+
* @param value The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
13
|
+
* @paramDefault value did:fide:0x152f02f1d1c1e62b2e569e11818420c1968be3d9
|
|
14
|
+
* @returns void
|
|
15
|
+
* @throws TypeError if value is not a string
|
|
16
|
+
* @throws Error if value is not a valid Fide ID format
|
|
17
|
+
*/
|
|
18
|
+
export function assertFideId(value) {
|
|
19
|
+
if (typeof value !== "string") {
|
|
20
|
+
throw new TypeError(`Invalid Fide ID: expected string, got ${typeof value}`);
|
|
21
|
+
}
|
|
22
|
+
if (value.length !== FIDE_ID_LENGTH) {
|
|
23
|
+
throw new Error(`Invalid Fide ID format: ${value}. Expected ${FIDE_ID_PREFIX}... (${FIDE_ID_LENGTH} chars)`);
|
|
24
|
+
}
|
|
25
|
+
if (!value.startsWith(FIDE_ID_PREFIX)) {
|
|
26
|
+
throw new Error(`Invalid Fide ID format: ${value}. Expected ${FIDE_ID_PREFIX}... (${FIDE_ID_LENGTH} chars)`);
|
|
27
|
+
}
|
|
28
|
+
const hex = value.slice(FIDE_ID_PREFIX.length);
|
|
29
|
+
if (!/^[0-9a-f]{40}$/i.test(hex)) {
|
|
30
|
+
throw new Error(`Invalid Fide ID format: ${value}. Expected ${FIDE_ID_PREFIX}... (${FIDE_ID_LENGTH} chars)`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parse a Fide ID into its constituent components.
|
|
35
|
+
*
|
|
36
|
+
* Decomposes a Fide ID into entity type, source type, and fingerprint, converting hex characters
|
|
37
|
+
* to their corresponding entity type names.
|
|
38
|
+
*
|
|
39
|
+
* @param fideId The Fide ID reference (format: did:fide:0x... or 0x...)
|
|
40
|
+
* @paramDefault fideId did:fide:0x152f02f1d1c1e62b2e569e11818420c1968be3d9
|
|
41
|
+
* @returns Parsed Fide ID components. `entityType` and `sourceType` are resolved via `FIDE_CHAR_TO_ENTITY_TYPE`.
|
|
42
|
+
* @throws Error if invalid Fide ID format or if type characters do not map to known entity types
|
|
43
|
+
*/
|
|
44
|
+
export function parseFideId(fideId) {
|
|
45
|
+
assertFideId(fideId);
|
|
46
|
+
const typeChar = fideId[FIDE_ID_PREFIX.length];
|
|
47
|
+
const sourceChar = fideId[FIDE_ID_PREFIX.length + 1];
|
|
48
|
+
const fingerprint = fideId.slice(FIDE_ID_PREFIX.length + 2);
|
|
49
|
+
const entityType = FIDE_CHAR_TO_ENTITY_TYPE[typeChar];
|
|
50
|
+
const sourceType = FIDE_CHAR_TO_ENTITY_TYPE[sourceChar];
|
|
51
|
+
if (!entityType) {
|
|
52
|
+
throw new Error(`Unknown entity type character: ${typeChar}`);
|
|
53
|
+
}
|
|
54
|
+
if (!sourceType) {
|
|
55
|
+
throw new Error(`Unknown source type character: ${sourceChar}`);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
fideId,
|
|
59
|
+
typeChar,
|
|
60
|
+
sourceChar,
|
|
61
|
+
entityType,
|
|
62
|
+
sourceType,
|
|
63
|
+
fingerprint
|
|
64
|
+
};
|
|
65
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fide.work/fcp - Fide Context Protocol SDK
|
|
3
|
+
*
|
|
4
|
+
* Core functions for calculating Fide IDs and working with
|
|
5
|
+
* the FCP protocol in JavaScript/TypeScript.
|
|
6
|
+
*/
|
|
7
|
+
export { calculateFideId, calculateStatementFideId } from "./fide-id/index.js";
|
|
8
|
+
export { assertFideId, parseFideId, } from "./fide-id/index.js";
|
|
9
|
+
export { FIDE_ENTITY_TYPE_MAP, FIDE_CHAR_TO_ENTITY_TYPE, FIDE_ID_PREFIX, FIDE_ID_HEX_LENGTH, FIDE_ID_LENGTH, FIDE_ID_FINGERPRINT_LENGTH } from "./fide-id/index.js";
|
|
10
|
+
export type { FideEntityType, FideStatementPredicateEntityType, FideStatementPredicateSourceType, FideEntityTypeChar, FideId, FideFingerprint, ParsedFideId } from "./fide-id/index.js";
|
|
11
|
+
export { createStatement, batchStatementsWithRoot, type StatementInput, type Statement, type StatementBatchWithRoot } from "./statement/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fide.work/fcp - Fide Context Protocol SDK
|
|
3
|
+
*
|
|
4
|
+
* Core functions for calculating Fide IDs and working with
|
|
5
|
+
* the FCP protocol in JavaScript/TypeScript.
|
|
6
|
+
*/
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// FIDE ID MODULE
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Core calculation functions
|
|
11
|
+
export { calculateFideId, calculateStatementFideId } from "./fide-id/index.js";
|
|
12
|
+
// Utility functions
|
|
13
|
+
export { assertFideId, parseFideId, } from "./fide-id/index.js";
|
|
14
|
+
// Constants
|
|
15
|
+
export { FIDE_ENTITY_TYPE_MAP, FIDE_CHAR_TO_ENTITY_TYPE, FIDE_ID_PREFIX, FIDE_ID_HEX_LENGTH, FIDE_ID_LENGTH, FIDE_ID_FINGERPRINT_LENGTH } from "./fide-id/index.js";
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// STATEMENT MODULE
|
|
18
|
+
// ============================================================================
|
|
19
|
+
export { createStatement, batchStatementsWithRoot } from "./statement/index.js";
|