@originals/sdk 1.2.0 → 1.4.2

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 (244) hide show
  1. package/package.json +1 -1
  2. package/src/did/DIDManager.ts +1 -1
  3. package/src/did/WebVHManager.ts +11 -2
  4. package/src/examples/create-module-original.ts +435 -0
  5. package/src/examples/full-lifecycle-flow.ts +514 -0
  6. package/src/examples/run.ts +59 -4
  7. package/src/index.ts +69 -3
  8. package/src/kinds/KindRegistry.ts +290 -0
  9. package/src/kinds/index.ts +74 -0
  10. package/src/kinds/types.ts +470 -0
  11. package/src/kinds/validators/AgentValidator.ts +257 -0
  12. package/src/kinds/validators/AppValidator.ts +211 -0
  13. package/src/kinds/validators/DatasetValidator.ts +242 -0
  14. package/src/kinds/validators/DocumentValidator.ts +311 -0
  15. package/src/kinds/validators/MediaValidator.ts +269 -0
  16. package/src/kinds/validators/ModuleValidator.ts +225 -0
  17. package/src/kinds/validators/base.ts +276 -0
  18. package/src/kinds/validators/index.ts +12 -0
  19. package/src/lifecycle/LifecycleManager.ts +909 -1
  20. package/src/resources/ResourceManager.ts +655 -0
  21. package/src/resources/index.ts +21 -0
  22. package/src/resources/types.ts +202 -0
  23. package/src/types/common.ts +1 -1
  24. package/src/vc/CredentialManager.ts +647 -2
  25. package/tests/integration/createTypedOriginal.test.ts +379 -0
  26. package/tests/performance/BatchOperations.perf.test.ts +2 -2
  27. package/tests/unit/kinds/KindRegistry.test.ts +329 -0
  28. package/tests/unit/kinds/types.test.ts +409 -0
  29. package/tests/unit/kinds/validators.test.ts +651 -0
  30. package/tests/unit/lifecycle/LifecycleManager.cleanapi.test.ts +441 -0
  31. package/tests/unit/resources/ResourceManager.test.ts +740 -0
  32. package/tests/unit/vc/CredentialManager.helpers.test.ts +527 -0
  33. package/.turbo/turbo-build.log +0 -1
  34. package/dist/adapters/FeeOracleMock.d.ts +0 -6
  35. package/dist/adapters/FeeOracleMock.js +0 -8
  36. package/dist/adapters/index.d.ts +0 -4
  37. package/dist/adapters/index.js +0 -4
  38. package/dist/adapters/providers/OrdHttpProvider.d.ts +0 -56
  39. package/dist/adapters/providers/OrdHttpProvider.js +0 -110
  40. package/dist/adapters/providers/OrdMockProvider.d.ts +0 -70
  41. package/dist/adapters/providers/OrdMockProvider.js +0 -75
  42. package/dist/adapters/types.d.ts +0 -71
  43. package/dist/adapters/types.js +0 -1
  44. package/dist/bitcoin/BitcoinManager.d.ts +0 -15
  45. package/dist/bitcoin/BitcoinManager.js +0 -262
  46. package/dist/bitcoin/BroadcastClient.d.ts +0 -30
  47. package/dist/bitcoin/BroadcastClient.js +0 -35
  48. package/dist/bitcoin/OrdinalsClient.d.ts +0 -21
  49. package/dist/bitcoin/OrdinalsClient.js +0 -105
  50. package/dist/bitcoin/PSBTBuilder.d.ts +0 -24
  51. package/dist/bitcoin/PSBTBuilder.js +0 -80
  52. package/dist/bitcoin/fee-calculation.d.ts +0 -14
  53. package/dist/bitcoin/fee-calculation.js +0 -31
  54. package/dist/bitcoin/providers/OrdNodeProvider.d.ts +0 -38
  55. package/dist/bitcoin/providers/OrdNodeProvider.js +0 -67
  56. package/dist/bitcoin/providers/OrdinalsProvider.d.ts +0 -33
  57. package/dist/bitcoin/providers/OrdinalsProvider.js +0 -50
  58. package/dist/bitcoin/providers/types.d.ts +0 -63
  59. package/dist/bitcoin/providers/types.js +0 -1
  60. package/dist/bitcoin/transactions/commit.d.ts +0 -89
  61. package/dist/bitcoin/transactions/commit.js +0 -311
  62. package/dist/bitcoin/transactions/index.d.ts +0 -7
  63. package/dist/bitcoin/transactions/index.js +0 -8
  64. package/dist/bitcoin/transfer.d.ts +0 -9
  65. package/dist/bitcoin/transfer.js +0 -26
  66. package/dist/bitcoin/utxo-selection.d.ts +0 -78
  67. package/dist/bitcoin/utxo-selection.js +0 -237
  68. package/dist/bitcoin/utxo.d.ts +0 -26
  69. package/dist/bitcoin/utxo.js +0 -78
  70. package/dist/contexts/credentials-v1.json +0 -195
  71. package/dist/contexts/credentials-v2-examples.json +0 -5
  72. package/dist/contexts/credentials-v2.json +0 -301
  73. package/dist/contexts/credentials.json +0 -195
  74. package/dist/contexts/data-integrity-v2.json +0 -81
  75. package/dist/contexts/dids.json +0 -57
  76. package/dist/contexts/ed255192020.json +0 -93
  77. package/dist/contexts/ordinals-plus.json +0 -23
  78. package/dist/contexts/originals.json +0 -22
  79. package/dist/core/OriginalsSDK.d.ts +0 -158
  80. package/dist/core/OriginalsSDK.js +0 -274
  81. package/dist/crypto/Multikey.d.ts +0 -30
  82. package/dist/crypto/Multikey.js +0 -149
  83. package/dist/crypto/Signer.d.ts +0 -21
  84. package/dist/crypto/Signer.js +0 -196
  85. package/dist/crypto/noble-init.d.ts +0 -18
  86. package/dist/crypto/noble-init.js +0 -106
  87. package/dist/did/BtcoDidResolver.d.ts +0 -57
  88. package/dist/did/BtcoDidResolver.js +0 -166
  89. package/dist/did/DIDManager.d.ts +0 -101
  90. package/dist/did/DIDManager.js +0 -493
  91. package/dist/did/Ed25519Verifier.d.ts +0 -30
  92. package/dist/did/Ed25519Verifier.js +0 -59
  93. package/dist/did/KeyManager.d.ts +0 -17
  94. package/dist/did/KeyManager.js +0 -207
  95. package/dist/did/WebVHManager.d.ts +0 -100
  96. package/dist/did/WebVHManager.js +0 -304
  97. package/dist/did/createBtcoDidDocument.d.ts +0 -10
  98. package/dist/did/createBtcoDidDocument.js +0 -42
  99. package/dist/did/providers/OrdinalsClientProviderAdapter.d.ts +0 -23
  100. package/dist/did/providers/OrdinalsClientProviderAdapter.js +0 -51
  101. package/dist/events/EventEmitter.d.ts +0 -115
  102. package/dist/events/EventEmitter.js +0 -198
  103. package/dist/events/index.d.ts +0 -7
  104. package/dist/events/index.js +0 -6
  105. package/dist/events/types.d.ts +0 -286
  106. package/dist/events/types.js +0 -9
  107. package/dist/examples/basic-usage.d.ts +0 -3
  108. package/dist/examples/basic-usage.js +0 -62
  109. package/dist/examples/run.d.ts +0 -1
  110. package/dist/examples/run.js +0 -4
  111. package/dist/index.d.ts +0 -39
  112. package/dist/index.js +0 -47
  113. package/dist/lifecycle/BatchOperations.d.ts +0 -147
  114. package/dist/lifecycle/BatchOperations.js +0 -251
  115. package/dist/lifecycle/LifecycleManager.d.ts +0 -116
  116. package/dist/lifecycle/LifecycleManager.js +0 -971
  117. package/dist/lifecycle/OriginalsAsset.d.ts +0 -164
  118. package/dist/lifecycle/OriginalsAsset.js +0 -380
  119. package/dist/lifecycle/ProvenanceQuery.d.ts +0 -126
  120. package/dist/lifecycle/ProvenanceQuery.js +0 -220
  121. package/dist/lifecycle/ResourceVersioning.d.ts +0 -73
  122. package/dist/lifecycle/ResourceVersioning.js +0 -127
  123. package/dist/migration/MigrationManager.d.ts +0 -86
  124. package/dist/migration/MigrationManager.js +0 -412
  125. package/dist/migration/audit/AuditLogger.d.ts +0 -51
  126. package/dist/migration/audit/AuditLogger.js +0 -156
  127. package/dist/migration/checkpoint/CheckpointManager.d.ts +0 -31
  128. package/dist/migration/checkpoint/CheckpointManager.js +0 -96
  129. package/dist/migration/checkpoint/CheckpointStorage.d.ts +0 -26
  130. package/dist/migration/checkpoint/CheckpointStorage.js +0 -89
  131. package/dist/migration/index.d.ts +0 -22
  132. package/dist/migration/index.js +0 -27
  133. package/dist/migration/operations/BaseMigration.d.ts +0 -48
  134. package/dist/migration/operations/BaseMigration.js +0 -83
  135. package/dist/migration/operations/PeerToBtcoMigration.d.ts +0 -25
  136. package/dist/migration/operations/PeerToBtcoMigration.js +0 -67
  137. package/dist/migration/operations/PeerToWebvhMigration.d.ts +0 -19
  138. package/dist/migration/operations/PeerToWebvhMigration.js +0 -46
  139. package/dist/migration/operations/WebvhToBtcoMigration.d.ts +0 -25
  140. package/dist/migration/operations/WebvhToBtcoMigration.js +0 -67
  141. package/dist/migration/rollback/RollbackManager.d.ts +0 -29
  142. package/dist/migration/rollback/RollbackManager.js +0 -146
  143. package/dist/migration/state/StateMachine.d.ts +0 -25
  144. package/dist/migration/state/StateMachine.js +0 -76
  145. package/dist/migration/state/StateTracker.d.ts +0 -36
  146. package/dist/migration/state/StateTracker.js +0 -123
  147. package/dist/migration/types.d.ts +0 -306
  148. package/dist/migration/types.js +0 -33
  149. package/dist/migration/validation/BitcoinValidator.d.ts +0 -13
  150. package/dist/migration/validation/BitcoinValidator.js +0 -83
  151. package/dist/migration/validation/CredentialValidator.d.ts +0 -13
  152. package/dist/migration/validation/CredentialValidator.js +0 -46
  153. package/dist/migration/validation/DIDCompatibilityValidator.d.ts +0 -16
  154. package/dist/migration/validation/DIDCompatibilityValidator.js +0 -127
  155. package/dist/migration/validation/LifecycleValidator.d.ts +0 -10
  156. package/dist/migration/validation/LifecycleValidator.js +0 -52
  157. package/dist/migration/validation/StorageValidator.d.ts +0 -10
  158. package/dist/migration/validation/StorageValidator.js +0 -65
  159. package/dist/migration/validation/ValidationPipeline.d.ts +0 -29
  160. package/dist/migration/validation/ValidationPipeline.js +0 -180
  161. package/dist/storage/LocalStorageAdapter.d.ts +0 -11
  162. package/dist/storage/LocalStorageAdapter.js +0 -53
  163. package/dist/storage/MemoryStorageAdapter.d.ts +0 -6
  164. package/dist/storage/MemoryStorageAdapter.js +0 -21
  165. package/dist/storage/StorageAdapter.d.ts +0 -16
  166. package/dist/storage/StorageAdapter.js +0 -1
  167. package/dist/storage/index.d.ts +0 -2
  168. package/dist/storage/index.js +0 -2
  169. package/dist/types/bitcoin.d.ts +0 -84
  170. package/dist/types/bitcoin.js +0 -1
  171. package/dist/types/common.d.ts +0 -82
  172. package/dist/types/common.js +0 -1
  173. package/dist/types/credentials.d.ts +0 -75
  174. package/dist/types/credentials.js +0 -1
  175. package/dist/types/did.d.ts +0 -26
  176. package/dist/types/did.js +0 -1
  177. package/dist/types/index.d.ts +0 -5
  178. package/dist/types/index.js +0 -5
  179. package/dist/types/network.d.ts +0 -78
  180. package/dist/types/network.js +0 -145
  181. package/dist/utils/EventLogger.d.ts +0 -71
  182. package/dist/utils/EventLogger.js +0 -232
  183. package/dist/utils/Logger.d.ts +0 -106
  184. package/dist/utils/Logger.js +0 -257
  185. package/dist/utils/MetricsCollector.d.ts +0 -110
  186. package/dist/utils/MetricsCollector.js +0 -264
  187. package/dist/utils/bitcoin-address.d.ts +0 -38
  188. package/dist/utils/bitcoin-address.js +0 -113
  189. package/dist/utils/cbor.d.ts +0 -2
  190. package/dist/utils/cbor.js +0 -9
  191. package/dist/utils/encoding.d.ts +0 -37
  192. package/dist/utils/encoding.js +0 -120
  193. package/dist/utils/hash.d.ts +0 -1
  194. package/dist/utils/hash.js +0 -5
  195. package/dist/utils/retry.d.ts +0 -10
  196. package/dist/utils/retry.js +0 -35
  197. package/dist/utils/satoshi-validation.d.ts +0 -60
  198. package/dist/utils/satoshi-validation.js +0 -156
  199. package/dist/utils/serialization.d.ts +0 -14
  200. package/dist/utils/serialization.js +0 -76
  201. package/dist/utils/telemetry.d.ts +0 -17
  202. package/dist/utils/telemetry.js +0 -24
  203. package/dist/utils/validation.d.ts +0 -5
  204. package/dist/utils/validation.js +0 -98
  205. package/dist/vc/CredentialManager.d.ts +0 -22
  206. package/dist/vc/CredentialManager.js +0 -227
  207. package/dist/vc/Issuer.d.ts +0 -27
  208. package/dist/vc/Issuer.js +0 -70
  209. package/dist/vc/Verifier.d.ts +0 -16
  210. package/dist/vc/Verifier.js +0 -50
  211. package/dist/vc/cryptosuites/bbs.d.ts +0 -44
  212. package/dist/vc/cryptosuites/bbs.js +0 -213
  213. package/dist/vc/cryptosuites/bbsSimple.d.ts +0 -9
  214. package/dist/vc/cryptosuites/bbsSimple.js +0 -12
  215. package/dist/vc/cryptosuites/eddsa.d.ts +0 -30
  216. package/dist/vc/cryptosuites/eddsa.js +0 -81
  217. package/dist/vc/documentLoader.d.ts +0 -16
  218. package/dist/vc/documentLoader.js +0 -59
  219. package/dist/vc/proofs/data-integrity.d.ts +0 -21
  220. package/dist/vc/proofs/data-integrity.js +0 -15
  221. package/dist/vc/utils/jsonld.d.ts +0 -2
  222. package/dist/vc/utils/jsonld.js +0 -15
  223. package/test/logs/did_webvh_QmNTn9Kkp8dQ75WrF9xqJ2kuDp9QhKc3aPiERRMj8XoTBN_example_com.jsonl +0 -1
  224. package/test/logs/did_webvh_QmNu4MNr8Lr5txx5gYNhuhZDchXsZEu3hJXKYuphpWTPDp_example_com_users_etc_passwd.jsonl +0 -1
  225. package/test/logs/did_webvh_QmR9MrGZACzjKETA8SBRNCKG11HxU85c4bVR2qN5eDCfsD_example_com.jsonl +0 -1
  226. package/test/logs/did_webvh_QmUc5suaqRM2P4nrXxZwqYMfqzhdMqjuL7oJaJbEpCQVCd_example_com_users_etc_passwd.jsonl +0 -1
  227. package/test/logs/did_webvh_QmUkiB2RCV2VZ1RTXsCebWN25Eiy9TLvpzDWAJNjhgvB4X_example_com_etc_passwd.jsonl +0 -1
  228. package/test/logs/did_webvh_QmUoRTe8UMwpAQXZSAW7pjAgZK1tq2X3C6Kfxq3UXGcaGy_example_com_secret.jsonl +0 -1
  229. package/test/logs/did_webvh_QmWWot3chx1t6KwTmcE5i2FeDZ5JMkQw3qXycsKDVmJ9Be_example_com_users_alice.jsonl +0 -1
  230. package/test/logs/did_webvh_QmWvVgALL5kjZdpgR7KZay7J8UiiUr834kkRmWeFAxjAuC_example_com_users_etc_passwd.jsonl +0 -1
  231. package/test/logs/did_webvh_QmWwaRQHUZAFcKihFC6xR6tRTTrQhHPTku6azf1egWbpy1_example_com_users_alice.jsonl +0 -1
  232. package/test/logs/did_webvh_QmXJLtkz23r7AozbtXsZMKWnVU6rd38CkVtjdWuATU3Yp6_example_com_users_alice123_profile.jsonl +0 -1
  233. package/test/logs/did_webvh_QmYsce448po14oDE1wXbyaP6wY9HQgHSKLwdezn1k577SF_example_com_my_org_user_name_test_123.jsonl +0 -1
  234. package/test/logs/did_webvh_QmZBeNzzqajxdfwcDUPZ4P8C5YSXyRztrAwmPiKuKUxmAK_example_com.jsonl +0 -1
  235. package/test/logs/did_webvh_QmZhJsqxizwVbRtqCUkmE6XQunSxtxMt3gbTYadVBNAaEq_example_com.jsonl +0 -1
  236. package/test/logs/did_webvh_QmZk7NHU2D57RzzbMq4tWW9gBa9AqtVTWfiRM6RFdwGVj2_example_com.jsonl +0 -1
  237. package/test/logs/did_webvh_QmZshSXp9w8ovH62zGGBS1b5pGGPsuYiu1VQ935sga2hWF_example_com_level1_level2.jsonl +0 -1
  238. package/test/logs/did_webvh_QmbWAmw7HQL7vKJyCsctZihXf1rmT4sGvggKCPKWcUWjw1_example_com.jsonl +0 -1
  239. package/test/logs/did_webvh_QmbdLUMbYs3juR39TLB6hhrFWLcNg45ybUzeBJCS1MhCh1_example_com_C_Windows_System32.jsonl +0 -1
  240. package/test/logs/did_webvh_QmcaQ1Ma4gkSbae85aCm8Mv4rvdT2Sb2RR3JzYwrm5XBq8_example_com_etc_passwd.jsonl +0 -1
  241. package/test/logs/did_webvh_QmcbA7WQhsBqZSoDpKJHjV8Q5o53h8vmgJhQfo6rqTY5ho_example_com.jsonl +0 -1
  242. package/test/logs/did_webvh_Qmdy8uWr2gkUJrXsThynAug3DASTWwb3onEj89LKmMGZYB_example_com.jsonl +0 -1
  243. package/tests/e2e/README.md +0 -97
  244. package/tests/e2e/example.spec.ts +0 -78
@@ -1,274 +0,0 @@
1
- import { DIDManager } from '../did/DIDManager';
2
- import { CredentialManager } from '../vc/CredentialManager';
3
- import { LifecycleManager } from '../lifecycle/LifecycleManager';
4
- import { BitcoinManager } from '../bitcoin/BitcoinManager';
5
- import { DEFAULT_WEBVH_NETWORK } from '../types/network';
6
- import { emitTelemetry, StructuredError } from '../utils/telemetry';
7
- import { Logger } from '../utils/Logger';
8
- import { MetricsCollector } from '../utils/MetricsCollector';
9
- import { EventLogger } from '../utils/EventLogger';
10
- import { createDID } from 'didwebvh-ts';
11
- export class OriginalsSDK {
12
- constructor(config, keyStore) {
13
- // Input validation
14
- if (!config || typeof config !== 'object') {
15
- throw new Error('Configuration object is required');
16
- }
17
- if (!config.network || !['mainnet', 'regtest', 'signet'].includes(config.network)) {
18
- throw new Error('Invalid network: must be mainnet, regtest, or signet');
19
- }
20
- if (!config.defaultKeyType || !['ES256K', 'Ed25519', 'ES256'].includes(config.defaultKeyType)) {
21
- throw new Error('Invalid defaultKeyType: must be ES256K, Ed25519, or ES256');
22
- }
23
- this.config = config;
24
- // Initialize logger and metrics
25
- this.logger = new Logger('SDK', config);
26
- this.metrics = new MetricsCollector();
27
- this.eventLogger = new EventLogger(this.logger.child('Events'), this.metrics);
28
- // Log SDK initialization
29
- this.logger.info('Initializing Originals SDK', {
30
- network: config.network,
31
- keyType: config.defaultKeyType
32
- });
33
- emitTelemetry(config.telemetry, { name: 'sdk.init', attributes: { network: config.network } });
34
- // Initialize managers
35
- this.did = new DIDManager(config);
36
- this.credentials = new CredentialManager(config, this.did);
37
- this.lifecycle = new LifecycleManager(config, this.did, this.credentials, undefined, keyStore);
38
- this.bitcoin = new BitcoinManager(config);
39
- // Set up event logging integration
40
- this.setupEventLogging();
41
- this.logger.info('SDK initialized successfully');
42
- }
43
- /**
44
- * Set up event logging integration
45
- */
46
- setupEventLogging() {
47
- // Configure event logging from config
48
- if (this.config.logging?.eventLogging) {
49
- this.eventLogger.configureEventLogging(this.config.logging.eventLogging);
50
- }
51
- // Subscribe to lifecycle events
52
- this.eventLogger.subscribeToEvents(this.lifecycle.eventEmitter);
53
- }
54
- /**
55
- * Validates that the SDK is properly configured for Bitcoin operations.
56
- * Throws a StructuredError if ordinalsProvider is not configured.
57
- *
58
- * @throws {StructuredError} When ordinalsProvider is not configured
59
- */
60
- validateBitcoinConfig() {
61
- if (!this.config.ordinalsProvider) {
62
- throw new StructuredError('ORD_PROVIDER_REQUIRED', 'Bitcoin operations require an ordinalsProvider to be configured. ' +
63
- 'Please provide an ordinalsProvider when creating the SDK. ' +
64
- 'See README.md for configuration examples.');
65
- }
66
- }
67
- static create(options) {
68
- const { keyStore, ...configOptions } = options || {};
69
- const defaultConfig = {
70
- network: 'mainnet',
71
- defaultKeyType: 'ES256K',
72
- enableLogging: false,
73
- webvhNetwork: DEFAULT_WEBVH_NETWORK, // Default to 'pichu' (production)
74
- };
75
- return new OriginalsSDK({ ...defaultConfig, ...configOptions }, keyStore);
76
- }
77
- /**
78
- * Prepare data for signing using didwebvh-ts's canonical approach
79
- * This is a public static helper method that wraps didwebvh-ts's prepareDataForSigning
80
- * to ensure didwebvh-ts is only imported within the SDK
81
- */
82
- static async prepareDIDDataForSigning(document, proof) {
83
- // Dynamically import didwebvh-ts to avoid module resolution issues
84
- const mod = await import('didwebvh-ts');
85
- const { prepareDataForSigning } = mod;
86
- // Runtime validation
87
- if (typeof prepareDataForSigning !== 'function') {
88
- throw new Error('Failed to load didwebvh-ts: prepareDataForSigning is not a function');
89
- }
90
- return prepareDataForSigning(document, proof);
91
- }
92
- /**
93
- * Verify a DID signature using Ed25519
94
- * This is a public static helper method that provides browser-compatible Ed25519 verification
95
- * Works with Uint8Array inputs (no Buffer required)
96
- *
97
- * @param signature - The signature bytes (Uint8Array)
98
- * @param message - The message bytes that were signed (Uint8Array)
99
- * @param publicKey - The public key bytes (Uint8Array, should be 32 bytes for Ed25519)
100
- * @returns True if the signature is valid
101
- */
102
- static async verifyDIDSignature(signature, message, publicKey) {
103
- // Dynamically import @noble/ed25519 to avoid module resolution issues
104
- const ed25519Mod = await import('@noble/ed25519');
105
- // Ed25519 public keys must be exactly 32 bytes
106
- // Some keys may have a version byte prefix, so remove it if present
107
- let ed25519PublicKey = publicKey;
108
- if (publicKey.length === 33) {
109
- ed25519PublicKey = publicKey.slice(1);
110
- }
111
- else if (publicKey.length !== 32) {
112
- throw new Error(`Invalid Ed25519 public key length: ${publicKey.length} (expected 32 bytes)`);
113
- }
114
- // Verify using @noble/ed25519 with Uint8Array (browser-compatible)
115
- // ed25519.verifyAsync accepts Uint8Array directly
116
- try {
117
- return await ed25519Mod.verifyAsync(signature, message, ed25519PublicKey);
118
- }
119
- catch (error) {
120
- // Verification failed or error occurred
121
- return false;
122
- }
123
- }
124
- /**
125
- * Create a new Original
126
- * This is a convenience proxy that routes to the appropriate specialized creation method.
127
- * Currently proxies to createDIDOriginal(), but can be extended for other Original types.
128
- *
129
- * @param options - Creation options (discriminated union by type)
130
- * @returns Created DID, document, log, and metadata
131
- */
132
- static async createOriginal(options) {
133
- // Route based on type discriminator
134
- switch (options.type) {
135
- case 'did':
136
- return OriginalsSDK.createDIDOriginal(options);
137
- default:
138
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
139
- throw new Error(`Unsupported Original type: ${options.type}`);
140
- }
141
- }
142
- /**
143
- * Create a new DID-based Original (DID document/log)
144
- * This wraps didwebvh-ts's createDID function to ensure didwebvh-ts is only imported within the SDK.
145
- *
146
- * A DID-based Original represents a decentralized identity that can be used as the foundation
147
- * for other Originals (e.g., asset Originals, credential Originals, etc.).
148
- *
149
- * @param options - Creation options matching didwebvh-ts createDID interface
150
- * @returns Created DID, document, log, and metadata
151
- */
152
- static async createDIDOriginal(options) {
153
- // Dynamically import didwebvh-ts to avoid module resolution issues
154
- // const mod = await import('didwebvh-ts') as unknown as {
155
- // createDID: (options: Record<string, unknown>) => Promise<{
156
- // did: string;
157
- // doc: Record<string, unknown>;
158
- // log: DIDLog;
159
- // meta: DIDResolutionMeta;
160
- // }>;
161
- // };
162
- // const { createDID } = mod;
163
- // Runtime validation
164
- if (typeof createDID !== 'function') {
165
- throw new Error('Failed to load didwebvh-ts: createDID is not a function');
166
- }
167
- // Create the DID using didwebvh-ts
168
- const result = await createDID({
169
- domain: options.domain,
170
- signer: options.signer,
171
- verifier: options.verifier,
172
- paths: options.paths,
173
- updateKeys: options.updateKeys,
174
- verificationMethods: options.verificationMethods,
175
- context: options.context || [
176
- 'https://www.w3.org/ns/did/v1',
177
- 'https://w3id.org/security/multikey/v1'
178
- ],
179
- });
180
- return {
181
- did: result.did,
182
- doc: result.doc,
183
- log: result.log,
184
- meta: result.meta
185
- };
186
- }
187
- /**
188
- * Update an existing Original
189
- * This is a convenience proxy that routes to the appropriate specialized update method.
190
- * Currently proxies to updateDIDOriginal(), but can be extended for other Original types.
191
- *
192
- * @param options - Update options (discriminated union by type)
193
- * @returns Updated DID, document, log, and metadata
194
- */
195
- static async updateOriginal(options) {
196
- // Route based on type discriminator
197
- switch (options.type) {
198
- case 'did':
199
- return OriginalsSDK.updateDIDOriginal(options);
200
- default:
201
- // TypeScript exhaustiveness check
202
- const _exhaustive = options;
203
- throw new Error(`Unsupported Original type: ${_exhaustive.type}`);
204
- }
205
- }
206
- /**
207
- * Update an existing DID-based Original (DID document/log)
208
- * This wraps didwebvh-ts's updateDID function to ensure didwebvh-ts is only imported within the SDK.
209
- *
210
- * @param options - Update options matching didwebvh-ts updateDID interface
211
- * @returns Updated DID, document, log, and metadata
212
- */
213
- static async updateDIDOriginal(options) {
214
- // Dynamically import didwebvh-ts to avoid module resolution issues
215
- const mod = await import('didwebvh-ts');
216
- const { updateDID } = mod;
217
- // Runtime validation
218
- if (typeof updateDID !== 'function') {
219
- throw new Error('Failed to load didwebvh-ts: updateDID is not a function');
220
- }
221
- // Prepare options for updateDID
222
- const updateOptions = {
223
- log: options.log,
224
- signer: options.signer,
225
- verifier: options.verifier || options.signer, // Use signer as verifier if not provided
226
- };
227
- // Add optional parameters
228
- if (options.updateKeys !== undefined)
229
- updateOptions.updateKeys = options.updateKeys;
230
- if (options.verificationMethods !== undefined)
231
- updateOptions.verificationMethods = options.verificationMethods;
232
- if (options.services !== undefined)
233
- updateOptions.services = options.services;
234
- if (options.controller !== undefined)
235
- updateOptions.controller = options.controller;
236
- if (options.context !== undefined)
237
- updateOptions.context = options.context;
238
- if (options.alsoKnownAs !== undefined)
239
- updateOptions.alsoKnownAs = options.alsoKnownAs;
240
- if (options.portable !== undefined)
241
- updateOptions.portable = options.portable;
242
- if (options.nextKeyHashes !== undefined)
243
- updateOptions.nextKeyHashes = options.nextKeyHashes;
244
- if (options.authentication !== undefined)
245
- updateOptions.authentication = options.authentication;
246
- if (options.assertionMethod !== undefined)
247
- updateOptions.assertionMethod = options.assertionMethod;
248
- if (options.keyAgreement !== undefined)
249
- updateOptions.keyAgreement = options.keyAgreement;
250
- if (options.domain !== undefined)
251
- updateOptions.domain = options.domain;
252
- // Update the DID using didwebvh-ts
253
- const result = await updateDID(updateOptions);
254
- // Extract DID from the log if not returned directly
255
- let did;
256
- if (result.did) {
257
- did = result.did;
258
- }
259
- else if (result.log && result.log.length > 0) {
260
- // Extract DID from the document in the log
261
- const latestDoc = result.log[result.log.length - 1]?.state;
262
- did = latestDoc?.id || '';
263
- }
264
- else {
265
- throw new Error('Cannot determine DID from update result');
266
- }
267
- return {
268
- did,
269
- doc: result.doc,
270
- log: result.log,
271
- meta: result.meta
272
- };
273
- }
274
- }
@@ -1,30 +0,0 @@
1
- export declare const MULTICODEC_ED25519_PUB_HEADER: Uint8Array<ArrayBuffer>;
2
- export declare const MULTICODEC_ED25519_PRIV_HEADER: Uint8Array<ArrayBuffer>;
3
- export declare const MULTICODEC_SECP256K1_PUB_HEADER: Uint8Array<ArrayBuffer>;
4
- export declare const MULTICODEC_SECP256K1_PRIV_HEADER: Uint8Array<ArrayBuffer>;
5
- export declare const MULTICODEC_BLS12381_G2_PUB_HEADER: Uint8Array<ArrayBuffer>;
6
- export declare const MULTICODEC_BLS12381_G2_PRIV_HEADER: Uint8Array<ArrayBuffer>;
7
- export declare const MULTICODEC_P256_PUB_HEADER: Uint8Array<ArrayBuffer>;
8
- export declare const MULTICODEC_P256_PRIV_HEADER: Uint8Array<ArrayBuffer>;
9
- export type MultikeyType = 'Ed25519' | 'Secp256k1' | 'Bls12381G2' | 'P256';
10
- /**
11
- * Validates that a key string uses proper multikey format.
12
- * @param key - The multibase-encoded key string to validate
13
- * @param expectedType - The expected key type (e.g., 'Ed25519', 'Secp256k1')
14
- * @param isPrivate - Whether this is a private key (true) or public key (false)
15
- * @throws Error with descriptive message if validation fails
16
- */
17
- export declare function validateMultikeyFormat(key: string, expectedType: MultikeyType, isPrivate: boolean): void;
18
- export declare const multikey: {
19
- encodePublicKey: (publicKey: Uint8Array, type: MultikeyType) => string;
20
- encodePrivateKey: (privateKey: Uint8Array, type: MultikeyType) => string;
21
- encodeMultibase: (data: Uint8Array | Buffer) => string;
22
- decodePublicKey: (publicKeyMultibase: string) => {
23
- key: Uint8Array;
24
- type: MultikeyType;
25
- };
26
- decodePrivateKey: (privateKeyMultibase: string) => {
27
- key: Uint8Array;
28
- type: MultikeyType;
29
- };
30
- };
@@ -1,149 +0,0 @@
1
- import { base58 } from '@scure/base';
2
- // Multicodec headers (varints) for supported key types
3
- export const MULTICODEC_ED25519_PUB_HEADER = new Uint8Array([0xed, 0x01]);
4
- export const MULTICODEC_ED25519_PRIV_HEADER = new Uint8Array([0x80, 0x26]);
5
- export const MULTICODEC_SECP256K1_PUB_HEADER = new Uint8Array([0xe7, 0x01]);
6
- export const MULTICODEC_SECP256K1_PRIV_HEADER = new Uint8Array([0x13, 0x01]);
7
- export const MULTICODEC_BLS12381_G2_PUB_HEADER = new Uint8Array([0xeb, 0x01]);
8
- export const MULTICODEC_BLS12381_G2_PRIV_HEADER = new Uint8Array([0x82, 0x26]);
9
- export const MULTICODEC_P256_PUB_HEADER = new Uint8Array([0x80, 0x24]);
10
- export const MULTICODEC_P256_PRIV_HEADER = new Uint8Array([0x81, 0x26]);
11
- function concatBytes(a, b) {
12
- const out = new Uint8Array(a.length + b.length);
13
- out.set(a, 0);
14
- out.set(b, a.length);
15
- return out;
16
- }
17
- /**
18
- * Validates that a key string uses proper multikey format.
19
- * @param key - The multibase-encoded key string to validate
20
- * @param expectedType - The expected key type (e.g., 'Ed25519', 'Secp256k1')
21
- * @param isPrivate - Whether this is a private key (true) or public key (false)
22
- * @throws Error with descriptive message if validation fails
23
- */
24
- export function validateMultikeyFormat(key, expectedType, isPrivate) {
25
- // Validate multibase prefix
26
- if (!key || typeof key !== 'string') {
27
- throw new Error('Invalid multibase key format. Key must be a non-empty string.');
28
- }
29
- if (key[0] !== 'z') {
30
- throw new Error('Invalid multibase key format. Keys must use z-base58btc encoding (prefix "z").');
31
- }
32
- // Attempt to decode and validate multicodec header
33
- try {
34
- const mc = base58.decode(key.slice(1));
35
- if (mc.length < 2) {
36
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
37
- }
38
- // Validate header matches expected type
39
- const header = mc.slice(0, 2);
40
- const expectedHeaders = isPrivate
41
- ? {
42
- Ed25519: MULTICODEC_ED25519_PRIV_HEADER,
43
- Secp256k1: MULTICODEC_SECP256K1_PRIV_HEADER,
44
- Bls12381G2: MULTICODEC_BLS12381_G2_PRIV_HEADER,
45
- P256: MULTICODEC_P256_PRIV_HEADER
46
- }
47
- : {
48
- Ed25519: MULTICODEC_ED25519_PUB_HEADER,
49
- Secp256k1: MULTICODEC_SECP256K1_PUB_HEADER,
50
- Bls12381G2: MULTICODEC_BLS12381_G2_PUB_HEADER,
51
- P256: MULTICODEC_P256_PUB_HEADER
52
- };
53
- const expectedHeader = expectedHeaders[expectedType];
54
- if (header[0] !== expectedHeader[0] || header[1] !== expectedHeader[1]) {
55
- throw new Error(`Invalid multibase key format. Expected ${expectedType} ${isPrivate ? 'private' : 'public'} key with multicodec header [0x${expectedHeader[0].toString(16)}, 0x${expectedHeader[1].toString(16)}], but found [0x${header[0].toString(16)}, 0x${header[1].toString(16)}].`);
56
- }
57
- // Validate key length (basic sanity check)
58
- const keyBytes = mc.slice(2);
59
- const expectedLengths = {
60
- Ed25519: { private: 32, public: 32 },
61
- Secp256k1: { private: 32, public: 33 },
62
- P256: { private: 32, public: 33 },
63
- Bls12381G2: { private: 32, public: 96 }
64
- };
65
- const expectedLength = isPrivate
66
- ? expectedLengths[expectedType].private
67
- : expectedLengths[expectedType].public;
68
- if (keyBytes.length !== expectedLength) {
69
- throw new Error(`Invalid multibase key format. Expected ${expectedType} ${isPrivate ? 'private' : 'public'} key to be ${expectedLength} bytes, but found ${keyBytes.length} bytes.`);
70
- }
71
- }
72
- catch (error) {
73
- // Re-throw our own errors as-is
74
- if (error instanceof Error && error.message.startsWith('Invalid multibase key format')) {
75
- throw error;
76
- }
77
- // Base58 decode errors or other unexpected errors
78
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. Decode error: ${error instanceof Error ? error.message : String(error)}`);
79
- }
80
- }
81
- export const multikey = {
82
- encodePublicKey: (publicKey, type) => {
83
- const header = type === 'Ed25519'
84
- ? MULTICODEC_ED25519_PUB_HEADER
85
- : type === 'Secp256k1'
86
- ? MULTICODEC_SECP256K1_PUB_HEADER
87
- : type === 'Bls12381G2'
88
- ? MULTICODEC_BLS12381_G2_PUB_HEADER
89
- : MULTICODEC_P256_PUB_HEADER;
90
- const mcBytes = concatBytes(header, publicKey);
91
- return 'z' + base58.encode(mcBytes);
92
- },
93
- encodePrivateKey: (privateKey, type) => {
94
- const header = type === 'Ed25519'
95
- ? MULTICODEC_ED25519_PRIV_HEADER
96
- : type === 'Secp256k1'
97
- ? MULTICODEC_SECP256K1_PRIV_HEADER
98
- : type === 'Bls12381G2'
99
- ? MULTICODEC_BLS12381_G2_PRIV_HEADER
100
- : MULTICODEC_P256_PRIV_HEADER;
101
- const mcBytes = concatBytes(header, privateKey);
102
- return 'z' + base58.encode(mcBytes);
103
- },
104
- encodeMultibase: (data) => {
105
- return 'z' + base58.encode(data instanceof Buffer ? new Uint8Array(data) : data);
106
- },
107
- decodePublicKey: (publicKeyMultibase) => {
108
- if (!publicKeyMultibase || publicKeyMultibase[0] !== 'z') {
109
- throw new Error('Invalid Multibase encoding');
110
- }
111
- const mc = base58.decode(publicKeyMultibase.slice(1));
112
- const header = mc.slice(0, 2);
113
- const key = mc.slice(2);
114
- if (header[0] === MULTICODEC_ED25519_PUB_HEADER[0] && header[1] === MULTICODEC_ED25519_PUB_HEADER[1]) {
115
- return { key, type: 'Ed25519' };
116
- }
117
- if (header[0] === MULTICODEC_SECP256K1_PUB_HEADER[0] && header[1] === MULTICODEC_SECP256K1_PUB_HEADER[1]) {
118
- return { key, type: 'Secp256k1' };
119
- }
120
- if (header[0] === MULTICODEC_BLS12381_G2_PUB_HEADER[0] && header[1] === MULTICODEC_BLS12381_G2_PUB_HEADER[1]) {
121
- return { key, type: 'Bls12381G2' };
122
- }
123
- if (header[0] === MULTICODEC_P256_PUB_HEADER[0] && header[1] === MULTICODEC_P256_PUB_HEADER[1]) {
124
- return { key, type: 'P256' };
125
- }
126
- throw new Error('Unsupported key type');
127
- },
128
- decodePrivateKey: (privateKeyMultibase) => {
129
- if (!privateKeyMultibase || privateKeyMultibase[0] !== 'z') {
130
- throw new Error('Invalid Multibase encoding');
131
- }
132
- const mc = base58.decode(privateKeyMultibase.slice(1));
133
- const header = mc.slice(0, 2);
134
- const key = mc.slice(2);
135
- if (header[0] === MULTICODEC_ED25519_PRIV_HEADER[0] && header[1] === MULTICODEC_ED25519_PRIV_HEADER[1]) {
136
- return { key, type: 'Ed25519' };
137
- }
138
- if (header[0] === MULTICODEC_SECP256K1_PRIV_HEADER[0] && header[1] === MULTICODEC_SECP256K1_PRIV_HEADER[1]) {
139
- return { key, type: 'Secp256k1' };
140
- }
141
- if (header[0] === MULTICODEC_BLS12381_G2_PRIV_HEADER[0] && header[1] === MULTICODEC_BLS12381_G2_PRIV_HEADER[1]) {
142
- return { key, type: 'Bls12381G2' };
143
- }
144
- if (header[0] === MULTICODEC_P256_PRIV_HEADER[0] && header[1] === MULTICODEC_P256_PRIV_HEADER[1]) {
145
- return { key, type: 'P256' };
146
- }
147
- throw new Error('Unsupported key type');
148
- }
149
- };
@@ -1,21 +0,0 @@
1
- import './noble-init.js';
2
- export declare abstract class Signer {
3
- abstract sign(data: Buffer, privateKeyMultibase: string): Promise<Buffer>;
4
- abstract verify(data: Buffer, signature: Buffer, publicKeyMultibase: string): Promise<boolean>;
5
- }
6
- export declare class ES256KSigner extends Signer {
7
- sign(data: Buffer, privateKeyMultibase: string): Promise<Buffer>;
8
- verify(data: Buffer, signature: Buffer, publicKeyMultibase: string): Promise<boolean>;
9
- }
10
- export declare class Ed25519Signer extends Signer {
11
- sign(data: Buffer, privateKeyMultibase: string): Promise<Buffer>;
12
- verify(data: Buffer, signature: Buffer, publicKeyMultibase: string): Promise<boolean>;
13
- }
14
- export declare class ES256Signer extends Signer {
15
- sign(data: Buffer, privateKeyMultibase: string): Promise<Buffer>;
16
- verify(data: Buffer, signature: Buffer, publicKeyMultibase: string): Promise<boolean>;
17
- }
18
- export declare class Bls12381G2Signer extends Signer {
19
- sign(data: Buffer, privateKeyMultibase: string): Promise<Buffer>;
20
- verify(data: Buffer, signature: Buffer, publicKeyMultibase: string): Promise<boolean>;
21
- }
@@ -1,196 +0,0 @@
1
- // Initialize noble crypto libraries first (idempotent - safe to import multiple times)
2
- import './noble-init.js';
3
- export class Signer {
4
- }
5
- import { bls12_381 as bls } from '@noble/curves/bls12-381';
6
- import { p256 } from '@noble/curves/p256';
7
- import { sha256 } from '@noble/hashes/sha2.js';
8
- import * as secp256k1 from '@noble/secp256k1';
9
- import * as ed25519 from '@noble/ed25519';
10
- import { multikey } from './Multikey';
11
- export class ES256KSigner extends Signer {
12
- async sign(data, privateKeyMultibase) {
13
- if (!privateKeyMultibase || privateKeyMultibase[0] !== 'z') {
14
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
15
- }
16
- let decoded;
17
- try {
18
- decoded = multikey.decodePrivateKey(privateKeyMultibase);
19
- }
20
- catch (error) {
21
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
22
- }
23
- if (decoded.type !== 'Secp256k1') {
24
- throw new Error('Invalid key type for ES256K');
25
- }
26
- const privateKey = decoded.key;
27
- const hash = sha256(data);
28
- const sigAny = await secp256k1.signAsync(hash, privateKey);
29
- const sigBytes = sigAny instanceof Uint8Array
30
- ? sigAny
31
- : typeof sigAny?.toCompactRawBytes === 'function'
32
- ? sigAny.toCompactRawBytes()
33
- : typeof sigAny?.toRawBytes === 'function'
34
- ? sigAny.toRawBytes()
35
- : new Uint8Array(sigAny);
36
- return Buffer.from(sigBytes);
37
- }
38
- async verify(data, signature, publicKeyMultibase) {
39
- if (!publicKeyMultibase || publicKeyMultibase[0] !== 'z') {
40
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
41
- }
42
- let decoded;
43
- try {
44
- decoded = multikey.decodePublicKey(publicKeyMultibase);
45
- }
46
- catch (error) {
47
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
48
- }
49
- if (decoded.type !== 'Secp256k1') {
50
- throw new Error('Invalid key type for ES256K');
51
- }
52
- const publicKey = decoded.key;
53
- const hash = sha256(data);
54
- try {
55
- return secp256k1.verify(signature, hash, publicKey);
56
- }
57
- catch {
58
- return false;
59
- }
60
- }
61
- }
62
- export class Ed25519Signer extends Signer {
63
- async sign(data, privateKeyMultibase) {
64
- if (!privateKeyMultibase || privateKeyMultibase[0] !== 'z') {
65
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
66
- }
67
- let decoded;
68
- try {
69
- decoded = multikey.decodePrivateKey(privateKeyMultibase);
70
- }
71
- catch (error) {
72
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
73
- }
74
- if (decoded.type !== 'Ed25519') {
75
- throw new Error('Invalid key type for Ed25519');
76
- }
77
- const privateKey = decoded.key;
78
- const signature = await ed25519.signAsync(data, privateKey);
79
- return Buffer.from(signature);
80
- }
81
- async verify(data, signature, publicKeyMultibase) {
82
- if (!publicKeyMultibase || publicKeyMultibase[0] !== 'z') {
83
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
84
- }
85
- let decoded;
86
- try {
87
- decoded = multikey.decodePublicKey(publicKeyMultibase);
88
- }
89
- catch (error) {
90
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
91
- }
92
- if (decoded.type !== 'Ed25519') {
93
- throw new Error('Invalid key type for Ed25519');
94
- }
95
- const publicKey = decoded.key;
96
- try {
97
- return await ed25519.verifyAsync(signature, data, publicKey);
98
- }
99
- catch {
100
- return false;
101
- }
102
- }
103
- }
104
- export class ES256Signer extends Signer {
105
- async sign(data, privateKeyMultibase) {
106
- if (!privateKeyMultibase || privateKeyMultibase[0] !== 'z') {
107
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
108
- }
109
- let decoded;
110
- try {
111
- decoded = multikey.decodePrivateKey(privateKeyMultibase);
112
- }
113
- catch (error) {
114
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
115
- }
116
- if (decoded.type !== 'P256') {
117
- throw new Error('Invalid key type for ES256');
118
- }
119
- const privateKey = decoded.key;
120
- const hash = sha256(data);
121
- const sigAny = p256.sign(hash, privateKey);
122
- const sigBytes = sigAny instanceof Uint8Array
123
- ? sigAny
124
- : typeof sigAny?.toCompactRawBytes === 'function'
125
- ? sigAny.toCompactRawBytes()
126
- : typeof sigAny?.toRawBytes === 'function'
127
- ? sigAny.toRawBytes()
128
- : new Uint8Array(sigAny);
129
- return Buffer.from(sigBytes);
130
- }
131
- async verify(data, signature, publicKeyMultibase) {
132
- if (!publicKeyMultibase || publicKeyMultibase[0] !== 'z') {
133
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
134
- }
135
- let decoded;
136
- try {
137
- decoded = multikey.decodePublicKey(publicKeyMultibase);
138
- }
139
- catch (error) {
140
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
141
- }
142
- if (decoded.type !== 'P256') {
143
- throw new Error('Invalid key type for ES256');
144
- }
145
- const publicKey = decoded.key;
146
- const hash = sha256(data);
147
- try {
148
- return p256.verify(signature, hash, publicKey);
149
- }
150
- catch {
151
- return false;
152
- }
153
- }
154
- }
155
- export class Bls12381G2Signer extends Signer {
156
- async sign(data, privateKeyMultibase) {
157
- if (!privateKeyMultibase || privateKeyMultibase[0] !== 'z') {
158
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
159
- }
160
- let decoded;
161
- try {
162
- decoded = multikey.decodePrivateKey(privateKeyMultibase);
163
- }
164
- catch (error) {
165
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
166
- }
167
- if (decoded.type !== 'Bls12381G2') {
168
- throw new Error('Invalid key type for Bls12381G2');
169
- }
170
- const sk = decoded.key;
171
- const sig = await bls.sign(data, sk);
172
- return Buffer.from(sig);
173
- }
174
- async verify(data, signature, publicKeyMultibase) {
175
- if (!publicKeyMultibase || publicKeyMultibase[0] !== 'z') {
176
- throw new Error('Invalid multibase key format. Keys must use multicodec headers.');
177
- }
178
- let decoded;
179
- try {
180
- decoded = multikey.decodePublicKey(publicKeyMultibase);
181
- }
182
- catch (error) {
183
- throw new Error(`Invalid multibase key format. Keys must use multicodec headers. ${error instanceof Error ? error.message : String(error)}`);
184
- }
185
- if (decoded.type !== 'Bls12381G2') {
186
- throw new Error('Invalid key type for Bls12381G2');
187
- }
188
- const pk = decoded.key;
189
- try {
190
- return await bls.verify(signature, data, pk);
191
- }
192
- catch {
193
- return false;
194
- }
195
- }
196
- }