1
- import { StorageSpec } from "../../src/logs/storage/storage-spec.ts" ;
2
- import {
3
- LogEvent ,
4
- Signer ,
5
- VerifierCallback ,
6
- } from "../../src/logs/log.types.ts" ;
1
+ import { LogEvent } from "../../src/logs/log.types.ts" ;
7
2
import { IDLogManager } from "../../src/logs/log-manager" ;
8
3
import { generateUuid } from "../../src/utils/uuid" ;
9
4
import { describe , expect , test , expectTypeOf } from "vitest" ;
10
5
import { hash } from "../../src/utils/hash" ;
11
6
import nacl from "tweetnacl" ;
12
- import {
13
- uint8ArrayToHex ,
14
- stringToUint8Array ,
15
- hexToUint8Array ,
16
- } from "../../src/utils/codec" ;
17
- import { base58btc } from "multiformats/bases/base58" ;
7
+ import { uint8ArrayToHex } from "../../src/utils/codec" ;
18
8
import falso from "@ngneat/falso" ;
19
9
import {
20
10
BadNextKeySpecifiedError ,
@@ -23,44 +13,9 @@ import {
23
13
MalformedHashChainError ,
24
14
MalformedIndexChainError ,
25
15
} from "../../src/errors/errors.ts" ;
16
+ import { InMemoryStorage } from "../utils/store.ts" ;
17
+ import { createSigner , verifierCallback } from "../utils/crypto.ts" ;
26
18
27
- export class InMemoryStorage < T extends LogEvent , K extends LogEvent >
28
- implements StorageSpec < T , K >
29
- {
30
- private data : K [ ] = [ ] ;
31
-
32
- public static build < T extends LogEvent , K extends LogEvent > ( ) : StorageSpec <
33
- T ,
34
- K
35
- > {
36
- return new InMemoryStorage < T , K > ( ) ;
37
- }
38
-
39
- public async create ( body : T ) : Promise < K > {
40
- const entry = body as unknown as K ;
41
- this . data . push ( entry ) ;
42
- return entry ;
43
- }
44
-
45
- public async findOne ( options : Partial < K > ) : Promise < K > {
46
- const result = this . data . find ( ( item ) =>
47
- Object . entries ( options ) . every (
48
- ( [ key , value ] ) => item [ key as keyof K ] === value ,
49
- ) ,
50
- ) ;
51
-
52
- if ( ! result ) throw new Error ( "Not found" ) ;
53
- return result ;
54
- }
55
-
56
- public async findMany ( options : Partial < K > ) : Promise < K [ ] > {
57
- return this . data . filter ( ( item ) =>
58
- Object . entries ( options ) . every (
59
- ( [ key , value ] ) => item [ key as keyof K ] === value ,
60
- ) ,
61
- ) ;
62
- }
63
- }
64
19
const keyPair = nacl . sign . keyPair ( ) ;
65
20
let currNextKey = nacl . sign . keyPair ( ) ;
66
21
@@ -69,36 +24,6 @@ const signer = createSigner(keyPair);
69
24
const logManager = new IDLogManager ( InMemoryStorage . build ( ) , signer ) ;
70
25
const w3id = `@${ generateUuid ( "asdfa" ) } ` ;
71
26
72
- export const verifierCallback : VerifierCallback = async (
73
- message : string ,
74
- signature : string ,
75
- pubKey : string ,
76
- ) => {
77
- const signatureBuffer = base58btc . decode ( signature ) ;
78
- const messageBuffer = stringToUint8Array ( message ) ;
79
- const publicKey = hexToUint8Array ( pubKey ) ;
80
- const isValid = nacl . sign . detached . verify (
81
- messageBuffer ,
82
- signatureBuffer ,
83
- publicKey ,
84
- ) ;
85
-
86
- return isValid ;
87
- } ;
88
-
89
- export function createSigner ( keyPair : nacl . SignKeyPair ) : Signer {
90
- const publicKey = uint8ArrayToHex ( keyPair . publicKey ) ;
91
- const signer : Signer = {
92
- pubKey : publicKey ,
93
- sign : ( str : string ) => {
94
- const buffer = stringToUint8Array ( str ) ;
95
- const signature = nacl . sign . detached ( buffer , keyPair . secretKey ) ;
96
- return base58btc . encode ( signature ) ;
97
- } ,
98
- } ;
99
- return signer ;
100
- }
101
-
102
27
describe ( "LogManager" , async ( ) => {
103
28
test ( "GenesisEvent: [Throw at Bad Options]" , async ( ) => {
104
29
const nextKeyHash = await hash ( uint8ArrayToHex ( currNextKey . publicKey ) ) ;
0 commit comments