1- import { W3ID as W3IDClass , W3IDBuilder , hash } from "w3id" ;
1+ import type { Driver } from "neo4j-driver" ;
2+ import { W3IDBuilder , type W3ID as W3IDClass } from "w3id" ;
23import { LogService } from "./log-service" ;
3- import { Driver } from "neo4j-driver" ;
4- import nacl from "tweetnacl" ;
5- import { createSigner } from "../utils/signer" ;
6- import { SecretsStore } from "../secrets/secrets-store" ;
7- import { uint8ArrayToHex } from "../utils/codec" ;
84
95export class W3ID {
106 private static instance : W3IDClass ;
11- private static secretsStore : SecretsStore ;
127
13- private constructor ( ) { }
8+ private constructor ( ) { }
149
1510 static async get ( options ?: {
1611 id : string ;
@@ -20,50 +15,16 @@ export class W3ID {
2015 if ( W3ID . instance ) return W3ID . instance ;
2116 if ( ! options )
2217 throw new Error (
23- "No instance of W3ID exists yet, please create it by passing options"
18+ "No instance of W3ID exists yet, please create it by passing options" ,
2419 ) ;
2520
26- // Initialize secrets store if not already done
27- if ( ! W3ID . secretsStore ) {
28- if ( ! options . password ) {
29- throw new Error ( "Password is required for secrets store" ) ;
30- }
31- W3ID . secretsStore = new SecretsStore (
32- process . env . SECRETS_STORE_PATH ! ,
33- options . password
34- ) ;
35- }
36-
3721 const repository = new LogService ( options . driver ) ;
38- const keyId = `w3id-${ options . id } ` ;
39-
40- try {
41- // Try to get existing seed
42- const { seed, nextKeyHash } = await W3ID . secretsStore . getSeed ( keyId ) ;
43- const keyPair = nacl . sign . keyPair . fromSeed ( seed ) ;
44- W3ID . instance = await new W3IDBuilder ( )
45- . withId ( options . id )
46- . withRepository ( repository )
47- . withGlobal ( true )
48- . withSigner ( createSigner ( keyPair ) )
49- . withNextKeyHash ( nextKeyHash )
50- . build ( ) ;
51- } catch {
52- // If no seed exists, create new one
53- const keyPair = nacl . sign . keyPair ( ) ;
54- const nextKeyPair = nacl . sign . keyPair ( ) ;
55- const nextKeyHash = await hash ( uint8ArrayToHex ( nextKeyPair . publicKey ) ) ;
56-
57- // Store the seed
58- await W3ID . secretsStore . storeSeed ( keyId , keyPair . secretKey , nextKeyHash ) ;
5922
60- W3ID . instance = await new W3IDBuilder ( )
61- . withId ( options . id )
62- . withRepository ( repository )
63- . withSigner ( createSigner ( keyPair ) )
64- . withNextKeyHash ( nextKeyHash )
65- . build ( ) ;
66- }
23+ W3ID . instance = await new W3IDBuilder ( )
24+ . withId ( options . id )
25+ . withRepository ( repository )
26+ . withGlobal ( true )
27+ . build ( ) ;
6728
6829 return W3ID . instance ;
6930 }
0 commit comments