@originals/sdk 1.4.3 → 1.4.5

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.
Files changed (222) hide show
  1. package/dist/adapters/FeeOracleMock.d.ts +6 -0
  2. package/dist/adapters/FeeOracleMock.js +8 -0
  3. package/dist/adapters/index.d.ts +4 -0
  4. package/dist/adapters/index.js +4 -0
  5. package/dist/adapters/providers/OrdHttpProvider.d.ts +56 -0
  6. package/dist/adapters/providers/OrdHttpProvider.js +110 -0
  7. package/dist/adapters/providers/OrdMockProvider.d.ts +70 -0
  8. package/dist/adapters/providers/OrdMockProvider.js +75 -0
  9. package/dist/adapters/types.d.ts +71 -0
  10. package/dist/adapters/types.js +1 -0
  11. package/dist/bitcoin/BitcoinManager.d.ts +15 -0
  12. package/dist/bitcoin/BitcoinManager.js +262 -0
  13. package/dist/bitcoin/BroadcastClient.d.ts +30 -0
  14. package/dist/bitcoin/BroadcastClient.js +35 -0
  15. package/dist/bitcoin/OrdinalsClient.d.ts +21 -0
  16. package/dist/bitcoin/OrdinalsClient.js +105 -0
  17. package/dist/bitcoin/PSBTBuilder.d.ts +24 -0
  18. package/dist/bitcoin/PSBTBuilder.js +80 -0
  19. package/dist/bitcoin/fee-calculation.d.ts +14 -0
  20. package/dist/bitcoin/fee-calculation.js +31 -0
  21. package/dist/bitcoin/providers/OrdNodeProvider.d.ts +38 -0
  22. package/dist/bitcoin/providers/OrdNodeProvider.js +67 -0
  23. package/dist/bitcoin/providers/OrdinalsProvider.d.ts +33 -0
  24. package/dist/bitcoin/providers/OrdinalsProvider.js +50 -0
  25. package/dist/bitcoin/providers/types.d.ts +63 -0
  26. package/dist/bitcoin/providers/types.js +1 -0
  27. package/dist/bitcoin/transactions/commit.d.ts +89 -0
  28. package/dist/bitcoin/transactions/commit.js +311 -0
  29. package/dist/bitcoin/transactions/index.d.ts +7 -0
  30. package/dist/bitcoin/transactions/index.js +8 -0
  31. package/dist/bitcoin/transfer.d.ts +9 -0
  32. package/dist/bitcoin/transfer.js +26 -0
  33. package/dist/bitcoin/utxo-selection.d.ts +78 -0
  34. package/dist/bitcoin/utxo-selection.js +237 -0
  35. package/dist/bitcoin/utxo.d.ts +26 -0
  36. package/dist/bitcoin/utxo.js +78 -0
  37. package/dist/contexts/credentials-v1.json +195 -0
  38. package/dist/contexts/credentials-v2-examples.json +5 -0
  39. package/dist/contexts/credentials-v2.json +301 -0
  40. package/dist/contexts/credentials.json +195 -0
  41. package/dist/contexts/data-integrity-v2.json +81 -0
  42. package/dist/contexts/dids.json +57 -0
  43. package/dist/contexts/ed255192020.json +93 -0
  44. package/dist/contexts/ordinals-plus.json +23 -0
  45. package/dist/contexts/originals.json +22 -0
  46. package/dist/core/OriginalsSDK.d.ts +158 -0
  47. package/dist/core/OriginalsSDK.js +274 -0
  48. package/dist/crypto/Multikey.d.ts +30 -0
  49. package/dist/crypto/Multikey.js +149 -0
  50. package/dist/crypto/Signer.d.ts +21 -0
  51. package/dist/crypto/Signer.js +196 -0
  52. package/dist/crypto/noble-init.d.ts +18 -0
  53. package/dist/crypto/noble-init.js +106 -0
  54. package/dist/did/BtcoDidResolver.d.ts +57 -0
  55. package/dist/did/BtcoDidResolver.js +166 -0
  56. package/dist/did/DIDManager.d.ts +101 -0
  57. package/dist/did/DIDManager.js +493 -0
  58. package/dist/did/Ed25519Verifier.d.ts +30 -0
  59. package/dist/did/Ed25519Verifier.js +59 -0
  60. package/dist/did/KeyManager.d.ts +17 -0
  61. package/dist/did/KeyManager.js +207 -0
  62. package/dist/did/WebVHManager.d.ts +100 -0
  63. package/dist/did/WebVHManager.js +312 -0
  64. package/dist/did/createBtcoDidDocument.d.ts +10 -0
  65. package/dist/did/createBtcoDidDocument.js +42 -0
  66. package/dist/did/providers/OrdinalsClientProviderAdapter.d.ts +23 -0
  67. package/dist/did/providers/OrdinalsClientProviderAdapter.js +51 -0
  68. package/dist/events/EventEmitter.d.ts +115 -0
  69. package/dist/events/EventEmitter.js +198 -0
  70. package/dist/events/index.d.ts +7 -0
  71. package/dist/events/index.js +6 -0
  72. package/dist/events/types.d.ts +286 -0
  73. package/dist/events/types.js +9 -0
  74. package/dist/examples/basic-usage.d.ts +3 -0
  75. package/dist/examples/basic-usage.js +62 -0
  76. package/dist/examples/create-module-original.d.ts +32 -0
  77. package/dist/examples/create-module-original.js +376 -0
  78. package/dist/examples/full-lifecycle-flow.d.ts +56 -0
  79. package/dist/examples/full-lifecycle-flow.js +419 -0
  80. package/dist/examples/run.d.ts +12 -0
  81. package/dist/examples/run.js +51 -0
  82. package/dist/index.d.ts +43 -0
  83. package/dist/index.js +52 -0
  84. package/dist/kinds/KindRegistry.d.ts +76 -0
  85. package/dist/kinds/KindRegistry.js +216 -0
  86. package/dist/kinds/index.d.ts +33 -0
  87. package/dist/kinds/index.js +36 -0
  88. package/dist/kinds/types.d.ts +363 -0
  89. package/dist/kinds/types.js +25 -0
  90. package/dist/kinds/validators/AgentValidator.d.ts +14 -0
  91. package/dist/kinds/validators/AgentValidator.js +155 -0
  92. package/dist/kinds/validators/AppValidator.d.ts +14 -0
  93. package/dist/kinds/validators/AppValidator.js +135 -0
  94. package/dist/kinds/validators/DatasetValidator.d.ts +14 -0
  95. package/dist/kinds/validators/DatasetValidator.js +148 -0
  96. package/dist/kinds/validators/DocumentValidator.d.ts +14 -0
  97. package/dist/kinds/validators/DocumentValidator.js +180 -0
  98. package/dist/kinds/validators/MediaValidator.d.ts +14 -0
  99. package/dist/kinds/validators/MediaValidator.js +172 -0
  100. package/dist/kinds/validators/ModuleValidator.d.ts +14 -0
  101. package/dist/kinds/validators/ModuleValidator.js +140 -0
  102. package/dist/kinds/validators/base.d.ts +96 -0
  103. package/dist/kinds/validators/base.js +218 -0
  104. package/dist/kinds/validators/index.d.ts +10 -0
  105. package/dist/kinds/validators/index.js +10 -0
  106. package/dist/lifecycle/BatchOperations.d.ts +147 -0
  107. package/dist/lifecycle/BatchOperations.js +251 -0
  108. package/dist/lifecycle/LifecycleManager.d.ts +362 -0
  109. package/dist/lifecycle/LifecycleManager.js +1692 -0
  110. package/dist/lifecycle/OriginalsAsset.d.ts +164 -0
  111. package/dist/lifecycle/OriginalsAsset.js +380 -0
  112. package/dist/lifecycle/ProvenanceQuery.d.ts +126 -0
  113. package/dist/lifecycle/ProvenanceQuery.js +220 -0
  114. package/dist/lifecycle/ResourceVersioning.d.ts +73 -0
  115. package/dist/lifecycle/ResourceVersioning.js +127 -0
  116. package/dist/migration/MigrationManager.d.ts +86 -0
  117. package/dist/migration/MigrationManager.js +412 -0
  118. package/dist/migration/audit/AuditLogger.d.ts +51 -0
  119. package/dist/migration/audit/AuditLogger.js +156 -0
  120. package/dist/migration/checkpoint/CheckpointManager.d.ts +31 -0
  121. package/dist/migration/checkpoint/CheckpointManager.js +96 -0
  122. package/dist/migration/checkpoint/CheckpointStorage.d.ts +26 -0
  123. package/dist/migration/checkpoint/CheckpointStorage.js +89 -0
  124. package/dist/migration/index.d.ts +22 -0
  125. package/dist/migration/index.js +27 -0
  126. package/dist/migration/operations/BaseMigration.d.ts +48 -0
  127. package/dist/migration/operations/BaseMigration.js +83 -0
  128. package/dist/migration/operations/PeerToBtcoMigration.d.ts +25 -0
  129. package/dist/migration/operations/PeerToBtcoMigration.js +67 -0
  130. package/dist/migration/operations/PeerToWebvhMigration.d.ts +19 -0
  131. package/dist/migration/operations/PeerToWebvhMigration.js +46 -0
  132. package/dist/migration/operations/WebvhToBtcoMigration.d.ts +25 -0
  133. package/dist/migration/operations/WebvhToBtcoMigration.js +67 -0
  134. package/dist/migration/rollback/RollbackManager.d.ts +29 -0
  135. package/dist/migration/rollback/RollbackManager.js +146 -0
  136. package/dist/migration/state/StateMachine.d.ts +25 -0
  137. package/dist/migration/state/StateMachine.js +76 -0
  138. package/dist/migration/state/StateTracker.d.ts +36 -0
  139. package/dist/migration/state/StateTracker.js +123 -0
  140. package/dist/migration/types.d.ts +306 -0
  141. package/dist/migration/types.js +33 -0
  142. package/dist/migration/validation/BitcoinValidator.d.ts +13 -0
  143. package/dist/migration/validation/BitcoinValidator.js +83 -0
  144. package/dist/migration/validation/CredentialValidator.d.ts +13 -0
  145. package/dist/migration/validation/CredentialValidator.js +46 -0
  146. package/dist/migration/validation/DIDCompatibilityValidator.d.ts +16 -0
  147. package/dist/migration/validation/DIDCompatibilityValidator.js +127 -0
  148. package/dist/migration/validation/LifecycleValidator.d.ts +10 -0
  149. package/dist/migration/validation/LifecycleValidator.js +52 -0
  150. package/dist/migration/validation/StorageValidator.d.ts +10 -0
  151. package/dist/migration/validation/StorageValidator.js +65 -0
  152. package/dist/migration/validation/ValidationPipeline.d.ts +29 -0
  153. package/dist/migration/validation/ValidationPipeline.js +180 -0
  154. package/dist/resources/ResourceManager.d.ts +231 -0
  155. package/dist/resources/ResourceManager.js +573 -0
  156. package/dist/resources/index.d.ts +11 -0
  157. package/dist/resources/index.js +10 -0
  158. package/dist/resources/types.d.ts +93 -0
  159. package/dist/resources/types.js +80 -0
  160. package/dist/storage/LocalStorageAdapter.d.ts +11 -0
  161. package/dist/storage/LocalStorageAdapter.js +53 -0
  162. package/dist/storage/MemoryStorageAdapter.d.ts +6 -0
  163. package/dist/storage/MemoryStorageAdapter.js +21 -0
  164. package/dist/storage/StorageAdapter.d.ts +16 -0
  165. package/dist/storage/StorageAdapter.js +1 -0
  166. package/dist/storage/index.d.ts +2 -0
  167. package/dist/storage/index.js +2 -0
  168. package/dist/types/bitcoin.d.ts +84 -0
  169. package/dist/types/bitcoin.js +1 -0
  170. package/dist/types/common.d.ts +82 -0
  171. package/dist/types/common.js +1 -0
  172. package/dist/types/credentials.d.ts +75 -0
  173. package/dist/types/credentials.js +1 -0
  174. package/dist/types/did.d.ts +26 -0
  175. package/dist/types/did.js +1 -0
  176. package/dist/types/index.d.ts +5 -0
  177. package/dist/types/index.js +5 -0
  178. package/dist/types/network.d.ts +78 -0
  179. package/dist/types/network.js +145 -0
  180. package/dist/utils/EventLogger.d.ts +71 -0
  181. package/dist/utils/EventLogger.js +232 -0
  182. package/dist/utils/Logger.d.ts +106 -0
  183. package/dist/utils/Logger.js +257 -0
  184. package/dist/utils/MetricsCollector.d.ts +110 -0
  185. package/dist/utils/MetricsCollector.js +264 -0
  186. package/dist/utils/bitcoin-address.d.ts +38 -0
  187. package/dist/utils/bitcoin-address.js +113 -0
  188. package/dist/utils/cbor.d.ts +2 -0
  189. package/dist/utils/cbor.js +9 -0
  190. package/dist/utils/encoding.d.ts +37 -0
  191. package/dist/utils/encoding.js +120 -0
  192. package/dist/utils/hash.d.ts +1 -0
  193. package/dist/utils/hash.js +5 -0
  194. package/dist/utils/retry.d.ts +10 -0
  195. package/dist/utils/retry.js +35 -0
  196. package/dist/utils/satoshi-validation.d.ts +60 -0
  197. package/dist/utils/satoshi-validation.js +156 -0
  198. package/dist/utils/serialization.d.ts +14 -0
  199. package/dist/utils/serialization.js +76 -0
  200. package/dist/utils/telemetry.d.ts +17 -0
  201. package/dist/utils/telemetry.js +24 -0
  202. package/dist/utils/validation.d.ts +5 -0
  203. package/dist/utils/validation.js +98 -0
  204. package/dist/vc/CredentialManager.d.ts +329 -0
  205. package/dist/vc/CredentialManager.js +615 -0
  206. package/dist/vc/Issuer.d.ts +27 -0
  207. package/dist/vc/Issuer.js +70 -0
  208. package/dist/vc/Verifier.d.ts +16 -0
  209. package/dist/vc/Verifier.js +50 -0
  210. package/dist/vc/cryptosuites/bbs.d.ts +44 -0
  211. package/dist/vc/cryptosuites/bbs.js +213 -0
  212. package/dist/vc/cryptosuites/bbsSimple.d.ts +9 -0
  213. package/dist/vc/cryptosuites/bbsSimple.js +12 -0
  214. package/dist/vc/cryptosuites/eddsa.d.ts +30 -0
  215. package/dist/vc/cryptosuites/eddsa.js +81 -0
  216. package/dist/vc/documentLoader.d.ts +16 -0
  217. package/dist/vc/documentLoader.js +59 -0
  218. package/dist/vc/proofs/data-integrity.d.ts +21 -0
  219. package/dist/vc/proofs/data-integrity.js +15 -0
  220. package/dist/vc/utils/jsonld.d.ts +2 -0
  221. package/dist/vc/utils/jsonld.js +15 -0
  222. package/package.json +2 -1
@@ -0,0 +1,70 @@
1
+ import { multikey } from '../crypto/Multikey';
2
+ import { createDocumentLoader } from './documentLoader';
3
+ import { DataIntegrityProofManager } from './proofs/data-integrity';
4
+ export class Issuer {
5
+ constructor(didManager, verificationMethod) {
6
+ this.didManager = didManager;
7
+ this.verificationMethod = verificationMethod;
8
+ }
9
+ inferKeyType(publicKeyMultibase) {
10
+ try {
11
+ return multikey.decodePublicKey(publicKeyMultibase).type;
12
+ }
13
+ catch {
14
+ return 'Ed25519';
15
+ }
16
+ }
17
+ async issueCredential(unsigned, options) {
18
+ const documentLoader = options.documentLoader || createDocumentLoader(this.didManager);
19
+ await documentLoader(this.verificationMethod.id);
20
+ const issuerId = typeof unsigned.issuer === 'string' ? unsigned.issuer : unsigned.issuer?.id;
21
+ const credential = {
22
+ ...unsigned,
23
+ '@context': ['https://www.w3.org/ns/credentials/v2'],
24
+ issuer: issuerId || this.verificationMethod.controller,
25
+ proof: undefined
26
+ };
27
+ if (!this.verificationMethod.secretKeyMultibase) {
28
+ throw new Error('Missing secretKeyMultibase for issuance');
29
+ }
30
+ const keyType = this.inferKeyType(this.verificationMethod.publicKeyMultibase);
31
+ if (keyType !== 'Ed25519') {
32
+ throw new Error('Only Ed25519 supported for eddsa-rdfc-2022');
33
+ }
34
+ const proof = await DataIntegrityProofManager.createProof(credential, {
35
+ verificationMethod: this.verificationMethod.id,
36
+ proofPurpose: options.proofPurpose,
37
+ cryptosuite: 'eddsa-rdfc-2022',
38
+ type: 'DataIntegrityProof',
39
+ privateKey: this.verificationMethod.secretKeyMultibase,
40
+ documentLoader
41
+ });
42
+ return { ...credential, proof };
43
+ }
44
+ async issuePresentation(presentation, options) {
45
+ const documentLoader = options.documentLoader || createDocumentLoader(this.didManager);
46
+ await documentLoader(this.verificationMethod.id);
47
+ if (!this.verificationMethod.secretKeyMultibase) {
48
+ throw new Error('Missing secretKeyMultibase for issuance');
49
+ }
50
+ const keyType = this.inferKeyType(this.verificationMethod.publicKeyMultibase);
51
+ if (keyType !== 'Ed25519') {
52
+ throw new Error('Only Ed25519 supported for eddsa-rdfc-2022');
53
+ }
54
+ const proof = await DataIntegrityProofManager.createProof({ ...presentation, '@context': ['https://www.w3.org/ns/credentials/v2'] }, {
55
+ verificationMethod: this.verificationMethod.id,
56
+ proofPurpose: options.proofPurpose,
57
+ cryptosuite: 'eddsa-rdfc-2022',
58
+ type: 'DataIntegrityProof',
59
+ privateKey: this.verificationMethod.secretKeyMultibase,
60
+ challenge: options.challenge,
61
+ domain: options.domain,
62
+ documentLoader
63
+ });
64
+ return {
65
+ ...presentation,
66
+ '@context': ['https://www.w3.org/ns/credentials/v2'],
67
+ proof
68
+ };
69
+ }
70
+ }
@@ -0,0 +1,16 @@
1
+ import { VerifiableCredential, VerifiablePresentation } from '../types';
2
+ import { DIDManager } from '../did/DIDManager';
3
+ export type VerificationResult = {
4
+ verified: boolean;
5
+ errors: string[];
6
+ };
7
+ export declare class Verifier {
8
+ private didManager;
9
+ constructor(didManager: DIDManager);
10
+ verifyCredential(vc: VerifiableCredential, options?: {
11
+ documentLoader?: (iri: string) => Promise<any>;
12
+ }): Promise<VerificationResult>;
13
+ verifyPresentation(vp: VerifiablePresentation, options?: {
14
+ documentLoader?: (iri: string) => Promise<any>;
15
+ }): Promise<VerificationResult>;
16
+ }
@@ -0,0 +1,50 @@
1
+ import { createDocumentLoader } from './documentLoader';
2
+ import { DataIntegrityProofManager } from './proofs/data-integrity';
3
+ export class Verifier {
4
+ constructor(didManager) {
5
+ this.didManager = didManager;
6
+ }
7
+ async verifyCredential(vc, options = {}) {
8
+ try {
9
+ if (!vc || !vc['@context'] || !vc.type)
10
+ throw new Error('Invalid credential');
11
+ if (!vc.proof)
12
+ throw new Error('Credential has no proof');
13
+ const loader = options.documentLoader || createDocumentLoader(this.didManager);
14
+ const ctxs = Array.isArray(vc['@context']) ? vc['@context'] : [vc['@context']];
15
+ for (const c of ctxs)
16
+ await loader(c);
17
+ const proof = Array.isArray(vc.proof) ? vc.proof[0] : vc.proof;
18
+ const result = await DataIntegrityProofManager.verifyProof(vc, proof, { documentLoader: loader });
19
+ return result.verified ? { verified: true, errors: [] } : { verified: false, errors: result.errors ?? ['Verification failed'] };
20
+ }
21
+ catch (e) {
22
+ return { verified: false, errors: [e?.message ?? 'Unknown error in verifyCredential'] };
23
+ }
24
+ }
25
+ async verifyPresentation(vp, options = {}) {
26
+ try {
27
+ if (!vp || !vp['@context'] || !vp.type)
28
+ throw new Error('Invalid presentation');
29
+ if (!vp.proof)
30
+ throw new Error('Presentation has no proof');
31
+ const loader = options.documentLoader || createDocumentLoader(this.didManager);
32
+ const ctxs = Array.isArray(vp['@context']) ? vp['@context'] : [vp['@context']];
33
+ for (const c of ctxs)
34
+ await loader(c);
35
+ if (vp.verifiableCredential) {
36
+ for (const c of vp.verifiableCredential) {
37
+ const res = await this.verifyCredential(c, { documentLoader: loader });
38
+ if (!res.verified)
39
+ return res;
40
+ }
41
+ }
42
+ const proof = Array.isArray(vp.proof) ? vp.proof[0] : vp.proof;
43
+ const result = await DataIntegrityProofManager.verifyProof(vp, proof, { documentLoader: loader });
44
+ return result.verified ? { verified: true, errors: [] } : { verified: false, errors: result.errors ?? ['Verification failed'] };
45
+ }
46
+ catch (e) {
47
+ return { verified: false, errors: [e?.message ?? 'Unknown error in verifyPresentation'] };
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Minimal BBS utility methods ported from legacy for working with
3
+ * Data Integrity BBS (bbs-2023) base and derived proof value encoding.
4
+ *
5
+ * Notes:
6
+ * - This module focuses on serialization/parsing helpers used by callers
7
+ * to pack/unpack proof values. It does not perform signing or verification.
8
+ * - All methods operate on Uint8Array inputs and return multibase strings
9
+ * (base64url with 'u' prefix) where applicable to match the spec.
10
+ */
11
+ export declare class BBSCryptosuiteUtils {
12
+ private static encodeBase64urlNoPad;
13
+ private static decodeBase64urlNoPad;
14
+ private static compareBytes;
15
+ private static concatBytes;
16
+ static serializeBaseProofValue(bbsSignature: Uint8Array, bbsHeader: Uint8Array, publicKey: Uint8Array, hmacKey: Uint8Array, mandatoryPointers: string[], featureOption: 'baseline' | 'anonymous_holder_binding' | 'pseudonym_issuer_pid' | 'pseudonym_hidden_pid', pid?: Uint8Array, signerBlind?: Uint8Array): string;
17
+ static parseBaseProofValue(proofValue: string): {
18
+ bbsSignature: Uint8Array;
19
+ bbsHeader: Uint8Array;
20
+ publicKey: Uint8Array;
21
+ hmacKey: Uint8Array;
22
+ mandatoryPointers: string[];
23
+ featureOption: 'baseline' | 'anonymous_holder_binding' | 'pseudonym_issuer_pid' | 'pseudonym_hidden_pid' | 'base_proof';
24
+ pid?: Uint8Array;
25
+ signerBlind?: Uint8Array;
26
+ };
27
+ private static compressLabelMap;
28
+ private static decompressLabelMap;
29
+ static serializeDerivedProofValue(bbsProof: Uint8Array, labelMap: {
30
+ [key: string]: string;
31
+ }, mandatoryIndexes: number[], selectiveIndexes: number[], presentationHeader: Uint8Array, featureOption: 'baseline' | 'anonymous_holder_binding' | 'pseudonym', pseudonym?: string, lengthBBSMessages?: number): string;
32
+ static parseDerivedProofValue(proofValue: string): {
33
+ bbsProof: Uint8Array;
34
+ labelMap: {
35
+ [key: string]: string;
36
+ };
37
+ mandatoryIndexes: number[];
38
+ selectiveIndexes: number[];
39
+ presentationHeader: Uint8Array;
40
+ featureOption: 'baseline' | 'anonymous_holder_binding' | 'pseudonym';
41
+ pseudonym?: string;
42
+ lengthBBSMessages?: number;
43
+ };
44
+ }
@@ -0,0 +1,213 @@
1
+ import * as cbor from 'cbor-js';
2
+ /**
3
+ * Minimal BBS utility methods ported from legacy for working with
4
+ * Data Integrity BBS (bbs-2023) base and derived proof value encoding.
5
+ *
6
+ * Notes:
7
+ * - This module focuses on serialization/parsing helpers used by callers
8
+ * to pack/unpack proof values. It does not perform signing or verification.
9
+ * - All methods operate on Uint8Array inputs and return multibase strings
10
+ * (base64url with 'u' prefix) where applicable to match the spec.
11
+ */
12
+ export class BBSCryptosuiteUtils {
13
+ static encodeBase64urlNoPad(bytes) {
14
+ const b64 = Buffer.from(bytes).toString('base64');
15
+ const b64url = b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
16
+ return 'u' + b64url;
17
+ }
18
+ static decodeBase64urlNoPad(s) {
19
+ if (!s.startsWith('u'))
20
+ throw new Error('Not a multibase base64url (u- prefixed) string');
21
+ const raw = s.slice(1);
22
+ const b64 = raw.replace(/-/g, '+').replace(/_/g, '/');
23
+ const pad = b64.length % 4 === 2 ? '==' : b64.length % 4 === 3 ? '=' : '';
24
+ return new Uint8Array(Buffer.from(b64 + pad, 'base64'));
25
+ }
26
+ static compareBytes(a, b) {
27
+ if (a.length !== b.length)
28
+ return false;
29
+ for (let i = 0; i < b.length; i++) {
30
+ if (a[i] !== b[i])
31
+ return false;
32
+ }
33
+ return true;
34
+ }
35
+ static concatBytes(a, b) {
36
+ const out = new Uint8Array(a.length + b.length);
37
+ out.set(a, 0);
38
+ out.set(b, a.length);
39
+ return out;
40
+ }
41
+ // ===== Base proof (serialize/parse) =====
42
+ static serializeBaseProofValue(bbsSignature, bbsHeader, publicKey, hmacKey, mandatoryPointers, featureOption, pid, signerBlind) {
43
+ let headerBytes;
44
+ let components;
45
+ switch (featureOption) {
46
+ case 'baseline':
47
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x02]);
48
+ components = [bbsSignature, bbsHeader, publicKey, hmacKey, mandatoryPointers];
49
+ break;
50
+ case 'anonymous_holder_binding':
51
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x04]);
52
+ if (!signerBlind)
53
+ throw new Error('signerBlind is required for anonymous_holder_binding');
54
+ components = [bbsSignature, bbsHeader, publicKey, hmacKey, mandatoryPointers, signerBlind];
55
+ break;
56
+ case 'pseudonym_issuer_pid':
57
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x06]);
58
+ if (!pid)
59
+ throw new Error('pid is required for pseudonym_issuer_pid');
60
+ components = [bbsSignature, bbsHeader, publicKey, hmacKey, mandatoryPointers, pid];
61
+ break;
62
+ case 'pseudonym_hidden_pid':
63
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x08]);
64
+ if (!signerBlind)
65
+ throw new Error('signerBlind is required for pseudonym_hidden_pid');
66
+ components = [bbsSignature, bbsHeader, publicKey, hmacKey, mandatoryPointers, signerBlind];
67
+ break;
68
+ default:
69
+ throw new Error(`Unsupported feature option: ${featureOption}`);
70
+ }
71
+ const encodedComponents = cbor.encode(components);
72
+ const proofBytes = BBSCryptosuiteUtils.concatBytes(headerBytes, new Uint8Array(encodedComponents));
73
+ return BBSCryptosuiteUtils.encodeBase64urlNoPad(proofBytes);
74
+ }
75
+ static parseBaseProofValue(proofValue) {
76
+ const decoded = BBSCryptosuiteUtils.decodeBase64urlNoPad(proofValue);
77
+ const header = decoded.slice(0, 3);
78
+ let featureOption;
79
+ if (this.compareBytes(header, [0xd9, 0x5d, 0x02]))
80
+ featureOption = 'baseline';
81
+ else if (this.compareBytes(header, [0xd9, 0x5d, 0x04]))
82
+ featureOption = 'anonymous_holder_binding';
83
+ else if (this.compareBytes(header, [0xd9, 0x5d, 0x06]))
84
+ featureOption = 'pseudonym_issuer_pid';
85
+ else if (this.compareBytes(header, [0xd9, 0x5d, 0x08]))
86
+ featureOption = 'pseudonym_hidden_pid';
87
+ else if (this.compareBytes(header, [0xd9, 0x5d, 0x03]))
88
+ featureOption = 'base_proof';
89
+ else
90
+ throw new Error('Invalid BBS base proof header');
91
+ const components = cbor.decode(decoded.slice(3).buffer);
92
+ const base = {
93
+ bbsSignature: components[0],
94
+ bbsHeader: components[1],
95
+ publicKey: components[2],
96
+ hmacKey: components[3],
97
+ mandatoryPointers: components[4],
98
+ featureOption
99
+ };
100
+ if (featureOption === 'anonymous_holder_binding' || featureOption === 'pseudonym_hidden_pid') {
101
+ base.signerBlind = components[5];
102
+ }
103
+ if (featureOption === 'pseudonym_issuer_pid') {
104
+ base.pid = components[5];
105
+ }
106
+ return base;
107
+ }
108
+ // ===== Label map compression helpers =====
109
+ static compressLabelMap(labelMap) {
110
+ const map = {};
111
+ for (const [k, v] of Object.entries(labelMap)) {
112
+ const c14nMatch = k.match(/^c14n(\d+)$/);
113
+ const bMatch = v.match(/^b(\d+)$/);
114
+ if (!c14nMatch || !bMatch) {
115
+ throw new Error(`Invalid label map entry: ${k} -> ${v}`);
116
+ }
117
+ const key = parseInt(c14nMatch[1], 10);
118
+ const value = parseInt(bMatch[1], 10);
119
+ map[key] = value.toString();
120
+ }
121
+ return map;
122
+ }
123
+ static decompressLabelMap(compressed) {
124
+ const map = {};
125
+ for (const [k, v] of Object.entries(compressed)) {
126
+ map[`c14n${k}`] = `b${v}`;
127
+ }
128
+ return map;
129
+ }
130
+ // ===== Derived proof (serialize/parse) =====
131
+ static serializeDerivedProofValue(bbsProof, labelMap, mandatoryIndexes, selectiveIndexes, presentationHeader, featureOption, pseudonym, lengthBBSMessages) {
132
+ const compressedLabelMap = this.compressLabelMap(labelMap);
133
+ let headerBytes;
134
+ let components;
135
+ switch (featureOption) {
136
+ case 'baseline':
137
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x03]);
138
+ components = [
139
+ bbsProof,
140
+ compressedLabelMap,
141
+ mandatoryIndexes,
142
+ selectiveIndexes,
143
+ presentationHeader
144
+ ];
145
+ break;
146
+ case 'anonymous_holder_binding':
147
+ if (typeof lengthBBSMessages !== 'number') {
148
+ throw new Error('lengthBBSMessages is required for anonymous_holder_binding');
149
+ }
150
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x05]);
151
+ components = [
152
+ bbsProof,
153
+ compressedLabelMap,
154
+ mandatoryIndexes,
155
+ selectiveIndexes,
156
+ presentationHeader,
157
+ lengthBBSMessages
158
+ ];
159
+ break;
160
+ case 'pseudonym':
161
+ if (!pseudonym || typeof lengthBBSMessages !== 'number') {
162
+ throw new Error('pseudonym and lengthBBSMessages are required for pseudonym features');
163
+ }
164
+ headerBytes = new Uint8Array([0xd9, 0x5d, 0x07]);
165
+ components = [
166
+ bbsProof,
167
+ compressedLabelMap,
168
+ mandatoryIndexes,
169
+ selectiveIndexes,
170
+ presentationHeader,
171
+ pseudonym,
172
+ lengthBBSMessages
173
+ ];
174
+ break;
175
+ default:
176
+ throw new Error(`Unsupported feature option: ${featureOption}`);
177
+ }
178
+ const encodedComponents = cbor.encode(components);
179
+ const proofBytes = this.concatBytes(headerBytes, new Uint8Array(encodedComponents));
180
+ return this.encodeBase64urlNoPad(proofBytes);
181
+ }
182
+ static parseDerivedProofValue(proofValue) {
183
+ const decoded = this.decodeBase64urlNoPad(proofValue);
184
+ const header = decoded.slice(0, 3);
185
+ let featureOption;
186
+ if (this.compareBytes(header, [0xd9, 0x5d, 0x03]))
187
+ featureOption = 'baseline';
188
+ else if (this.compareBytes(header, [0xd9, 0x5d, 0x05]))
189
+ featureOption = 'anonymous_holder_binding';
190
+ else if (this.compareBytes(header, [0xd9, 0x5d, 0x07]))
191
+ featureOption = 'pseudonym';
192
+ else
193
+ throw new Error('Invalid BBS derived proof header');
194
+ const components = cbor.decode(decoded.slice(3).buffer);
195
+ const decompressedLabelMap = this.decompressLabelMap(components[1]);
196
+ const result = {
197
+ bbsProof: components[0],
198
+ labelMap: decompressedLabelMap,
199
+ mandatoryIndexes: components[2],
200
+ selectiveIndexes: components[3],
201
+ presentationHeader: components[4],
202
+ featureOption
203
+ };
204
+ if (featureOption === 'anonymous_holder_binding') {
205
+ result.lengthBBSMessages = components[5];
206
+ }
207
+ else if (featureOption === 'pseudonym') {
208
+ result.pseudonym = components[5];
209
+ result.lengthBBSMessages = components[6];
210
+ }
211
+ return result;
212
+ }
213
+ }
@@ -0,0 +1,9 @@
1
+ export type BbsKeyPair = {
2
+ publicKey: Uint8Array;
3
+ privateKey: Uint8Array;
4
+ };
5
+ export declare class BbsSimple {
6
+ static readonly CIPHERSUITE = "BLS12-381-SHA-256";
7
+ static sign(messages: Uint8Array[], keypair: BbsKeyPair, header?: Uint8Array): Promise<Uint8Array>;
8
+ static verify(messages: Uint8Array[], signature: Uint8Array, publicKey: Uint8Array, header?: Uint8Array): Promise<boolean>;
9
+ }
@@ -0,0 +1,12 @@
1
+ import { sha256 } from '@noble/hashes/sha2.js';
2
+ export class BbsSimple {
3
+ static async sign(messages, keypair, header) {
4
+ const headerBytes = header ?? new Uint8Array(sha256(new Uint8Array(0)));
5
+ throw new Error('BbsSimple.sign is not implemented');
6
+ }
7
+ static async verify(messages, signature, publicKey, header) {
8
+ const headerBytes = header ?? new Uint8Array(sha256(new Uint8Array(0)));
9
+ throw new Error('BbsSimple.verify is not implemented');
10
+ }
11
+ }
12
+ BbsSimple.CIPHERSUITE = 'BLS12-381-SHA-256';
@@ -0,0 +1,30 @@
1
+ export interface DataIntegrityProof {
2
+ type: 'DataIntegrityProof';
3
+ cryptosuite: string;
4
+ created?: string;
5
+ verificationMethod: string;
6
+ proofPurpose: string;
7
+ proofValue: string;
8
+ id?: string;
9
+ previousProof?: string | string[];
10
+ }
11
+ export interface VerificationResult {
12
+ verified: boolean;
13
+ errors?: string[];
14
+ }
15
+ export declare class EdDSACryptosuiteManager {
16
+ static createProof(document: any, options: any): Promise<DataIntegrityProof>;
17
+ static verifyProof(document: any, proof: DataIntegrityProof, options: any): Promise<VerificationResult>;
18
+ private static createProofConfiguration;
19
+ private static transform;
20
+ private static hash;
21
+ static sign({ data, privateKey }: {
22
+ data: Uint8Array;
23
+ privateKey: Uint8Array;
24
+ }): Promise<Uint8Array>;
25
+ static verify({ data, signature, publicKey }: {
26
+ data: Uint8Array;
27
+ signature: Uint8Array;
28
+ publicKey: Uint8Array;
29
+ }): Promise<boolean>;
30
+ }
@@ -0,0 +1,81 @@
1
+ import { base58 } from '@scure/base';
2
+ import * as ed25519 from '@noble/ed25519';
3
+ import { canonize, canonizeProof } from '../utils/jsonld';
4
+ import { multikey } from '../../crypto/Multikey';
5
+ import { sha256Bytes } from '../../utils/hash';
6
+ export class EdDSACryptosuiteManager {
7
+ static async createProof(document, options) {
8
+ const proofConfig = await this.createProofConfiguration(options);
9
+ const transformedData = await this.transform(document, options);
10
+ const hashData = await this.hash(transformedData, proofConfig, options);
11
+ let privateKey;
12
+ if (typeof options.privateKey === 'string') {
13
+ const dec = multikey.decodePrivateKey(options.privateKey);
14
+ if (dec.type !== 'Ed25519')
15
+ throw new Error('Invalid key type for EdDSA');
16
+ privateKey = dec.key;
17
+ }
18
+ else if (options.privateKey instanceof Uint8Array) {
19
+ privateKey = options.privateKey;
20
+ }
21
+ else {
22
+ throw new Error('Invalid private key format');
23
+ }
24
+ const proofValueBytes = await this.sign({ data: hashData, privateKey });
25
+ delete proofConfig['@context'];
26
+ return { ...proofConfig, proofValue: base58.encode(proofValueBytes) };
27
+ }
28
+ static async verifyProof(document, proof, options) {
29
+ try {
30
+ const documentToVerify = { ...document };
31
+ delete documentToVerify.proof;
32
+ const transformedData = await this.transform(documentToVerify, options);
33
+ const hashData = await this.hash(transformedData, { '@context': document['@context'], ...proof }, options);
34
+ const vmDoc = await options.documentLoader(proof.verificationMethod);
35
+ const pk = vmDoc.document.publicKeyMultibase;
36
+ const dec = multikey.decodePublicKey(pk);
37
+ if (dec.type !== 'Ed25519')
38
+ throw new Error('Invalid key type for EdDSA');
39
+ const signature = base58.decode(proof.proofValue);
40
+ const verified = await this.verify({ data: hashData, signature, publicKey: dec.key });
41
+ return verified ? { verified: true } : { verified: false, errors: ['Proof verification failed'] };
42
+ }
43
+ catch (e) {
44
+ return { verified: false, errors: [e?.message ?? 'Unknown verification error'] };
45
+ }
46
+ }
47
+ static async createProofConfiguration(options) {
48
+ return {
49
+ '@context': 'https://w3id.org/security/data-integrity/v2',
50
+ type: 'DataIntegrityProof',
51
+ cryptosuite: 'eddsa-rdfc-2022',
52
+ created: new Date().toISOString(),
53
+ verificationMethod: options.verificationMethod,
54
+ proofPurpose: options.proofPurpose || 'assertionMethod',
55
+ ...(options.challenge && { challenge: options.challenge }),
56
+ ...(options.domain && { domain: options.domain })
57
+ };
58
+ }
59
+ static async transform(document, options) {
60
+ return await canonize(document, { documentLoader: options.documentLoader });
61
+ }
62
+ static async hash(transformedData, proofConfig, options) {
63
+ const canonicalProofConfig = await canonizeProof(proofConfig, { documentLoader: options.documentLoader });
64
+ const proofConfigHash = await sha256Bytes(canonicalProofConfig);
65
+ const documentHash = await sha256Bytes(transformedData);
66
+ return new Uint8Array([...proofConfigHash, ...documentHash]);
67
+ }
68
+ static async sign({ data, privateKey }) {
69
+ if (privateKey.length !== 32) {
70
+ if (privateKey.length === 64)
71
+ privateKey = privateKey.slice(32);
72
+ else
73
+ throw new Error('Invalid private key length');
74
+ }
75
+ const signature = await ed25519.signAsync(Buffer.from(data).toString('hex'), Buffer.from(privateKey).toString('hex'));
76
+ return signature;
77
+ }
78
+ static async verify({ data, signature, publicKey }) {
79
+ return await ed25519.verifyAsync(Buffer.from(signature).toString('hex'), Buffer.from(data).toString('hex'), Buffer.from(publicKey).toString('hex'));
80
+ }
81
+ }
@@ -0,0 +1,16 @@
1
+ import { DIDManager } from '../did/DIDManager';
2
+ type LoadedDocument = {
3
+ document: any;
4
+ documentUrl: string;
5
+ contextUrl: string | null;
6
+ };
7
+ export declare class DocumentLoader {
8
+ private didManager;
9
+ constructor(didManager: DIDManager);
10
+ load(iri: string): Promise<LoadedDocument>;
11
+ private resolveDID;
12
+ }
13
+ export declare const createDocumentLoader: (didManager: DIDManager) => (iri: string) => Promise<LoadedDocument>;
14
+ export declare const verificationMethodRegistry: Map<string, any>;
15
+ export declare function registerVerificationMethod(vm: any): void;
16
+ export {};
@@ -0,0 +1,59 @@
1
+ const CONTEXTS = {
2
+ // Provide 1.1-compatible stubs for jsonld canonize
3
+ 'https://www.w3.org/ns/credentials/v2': { '@context': { '@version': 1.1 } },
4
+ 'https://w3id.org/security/data-integrity/v2': { '@context': { '@version': 1.1 } }
5
+ };
6
+ export class DocumentLoader {
7
+ constructor(didManager) {
8
+ this.didManager = didManager;
9
+ }
10
+ async load(iri) {
11
+ if (iri.startsWith('did:')) {
12
+ return this.resolveDID(iri);
13
+ }
14
+ const doc = CONTEXTS[iri];
15
+ if (doc) {
16
+ return { document: doc, documentUrl: iri, contextUrl: null };
17
+ }
18
+ throw new Error(`Document not found: ${iri}`);
19
+ }
20
+ async resolveDID(didUrl) {
21
+ const [did, fragment] = didUrl.split('#');
22
+ const didDoc = await this.didManager.resolveDID(did);
23
+ if (!didDoc) {
24
+ throw new Error(`DID not resolved: ${did}`);
25
+ }
26
+ if (fragment) {
27
+ // If a VM was registered explicitly, prefer it
28
+ const cached = verificationMethodRegistry.get(didUrl);
29
+ if (cached) {
30
+ return {
31
+ document: { '@context': didDoc['@context'], ...cached },
32
+ documentUrl: didUrl,
33
+ contextUrl: null
34
+ };
35
+ }
36
+ const vms = didDoc.verificationMethod;
37
+ const vm = vms?.find((m) => m.id === didUrl);
38
+ if (vm) {
39
+ return {
40
+ document: { '@context': didDoc['@context'], ...vm },
41
+ documentUrl: didUrl,
42
+ contextUrl: null
43
+ };
44
+ }
45
+ return {
46
+ document: { '@context': didDoc['@context'], id: didUrl },
47
+ documentUrl: didUrl,
48
+ contextUrl: null
49
+ };
50
+ }
51
+ return { document: didDoc, documentUrl: didUrl, contextUrl: null };
52
+ }
53
+ }
54
+ export const createDocumentLoader = (didManager) => (iri) => new DocumentLoader(didManager).load(iri);
55
+ export const verificationMethodRegistry = new Map();
56
+ export function registerVerificationMethod(vm) {
57
+ if (vm?.id)
58
+ verificationMethodRegistry.set(vm.id, vm);
59
+ }
@@ -0,0 +1,21 @@
1
+ import { type DataIntegrityProof } from '../cryptosuites/eddsa';
2
+ export interface VerificationResult {
3
+ verified: boolean;
4
+ errors?: string[];
5
+ }
6
+ export interface ProofOptions {
7
+ verificationMethod: string;
8
+ proofPurpose: string;
9
+ privateKey?: Uint8Array | string;
10
+ type: 'DataIntegrityProof';
11
+ created?: string;
12
+ cryptosuite: string;
13
+ documentLoader?: (url: string) => Promise<any>;
14
+ previousProof?: string | string[];
15
+ challenge?: string;
16
+ domain?: string;
17
+ }
18
+ export declare class DataIntegrityProofManager {
19
+ static createProof(document: any, options: ProofOptions): Promise<DataIntegrityProof>;
20
+ static verifyProof(document: any, proof: DataIntegrityProof, options: any): Promise<VerificationResult>;
21
+ }
@@ -0,0 +1,15 @@
1
+ import { EdDSACryptosuiteManager } from '../cryptosuites/eddsa';
2
+ export class DataIntegrityProofManager {
3
+ static async createProof(document, options) {
4
+ if (options.cryptosuite !== 'eddsa-rdfc-2022') {
5
+ throw new Error(`Unsupported cryptosuite: ${options.cryptosuite}`);
6
+ }
7
+ return await EdDSACryptosuiteManager.createProof(document, options);
8
+ }
9
+ static async verifyProof(document, proof, options) {
10
+ if (proof.cryptosuite !== 'eddsa-rdfc-2022') {
11
+ return { verified: false, errors: [`Unsupported cryptosuite: ${proof.cryptosuite}`] };
12
+ }
13
+ return await EdDSACryptosuiteManager.verifyProof(document, proof, options);
14
+ }
15
+ }
@@ -0,0 +1,2 @@
1
+ export declare function canonize(input: any, { documentLoader }: any): Promise<string>;
2
+ export declare function canonizeProof(proof: any, { documentLoader }: any): Promise<string>;
@@ -0,0 +1,15 @@
1
+ import jsonld from 'jsonld';
2
+ export async function canonize(input, { documentLoader }) {
3
+ return await jsonld.canonize(input, {
4
+ algorithm: 'URDNA2015',
5
+ format: 'application/n-quads',
6
+ documentLoader,
7
+ safe: false,
8
+ useNative: false,
9
+ rdfDirection: 'i18n-datatype'
10
+ });
11
+ }
12
+ export async function canonizeProof(proof, { documentLoader }) {
13
+ const { jws, signatureValue, proofValue, ...rest } = proof;
14
+ return await canonize(rest, { documentLoader });
15
+ }