@@ -29,9 +29,9 @@ import type {NodeAlias, NodeAliases} from '../../types/aliases.js';
29
29
* Used to construct the nodes data and convert them to JSON
30
30
*/
31
31
export class GenesisNetworkDataConstructor implements ToJSON {
32
- public readonly nodes : Record < NodeAlias , GenesisNetworkNodeDataWrapper > ;
32
+ public readonly nodes : Record < NodeAlias , GenesisNetworkNodeDataWrapper > = { } ;
33
33
34
- public constructor (
34
+ private constructor (
35
35
private readonly nodeAliases : NodeAliases ,
36
36
private readonly keyManager : KeyManager ,
37
37
private readonly keysDir : string ,
@@ -45,23 +45,37 @@ export class GenesisNetworkDataConstructor implements ToJSON {
45
45
} ) ;
46
46
}
47
47
48
+ public static async initialize (
49
+ nodeAliases : NodeAliases ,
50
+ keyManager : KeyManager ,
51
+ keysDir : string ,
52
+ ) : Promise < GenesisNetworkDataConstructor > {
53
+ const instance = new GenesisNetworkDataConstructor ( nodeAliases , keyManager , keysDir ) ;
54
+
55
+ await instance . load ( ) ;
56
+
57
+ return instance ;
58
+ }
59
+
48
60
/**
49
61
* Loads the gossipCaCertificate and grpcCertificateHash
50
62
*/
51
- public async load ( ) {
63
+ private async load ( ) {
52
64
await Promise . all (
53
65
this . nodeAliases . map ( async nodeAlias => {
54
66
const nodeKeys = await this . keyManager . loadSigningKey ( nodeAlias , this . keysDir ) ;
55
67
68
+ //* Convert the certificate to PEM format
56
69
const certPem = nodeKeys . certificate . toString ( ) ;
57
70
71
+ //* Assign the PEM certificate
58
72
this . nodes [ nodeAlias ] . gossipCaCertificate = certPem ;
59
73
74
+ //* Decode the PEM to DER format
60
75
const tlsCertDer = new Uint8Array ( x509 . PemConverter . decode ( certPem ) [ 0 ] ) ;
61
76
62
- const grpcCertificateHash = crypto . createHash ( 'sha384' ) . update ( tlsCertDer ) . digest ( ) ;
63
-
64
- this . nodes [ nodeAlias ] . grpcCertificateHash = grpcCertificateHash . toString ( ) ;
77
+ //* Generate the SHA-384 hash
78
+ this . nodes [ nodeAlias ] . grpcCertificateHash = crypto . createHash ( 'sha384' ) . update ( tlsCertDer ) . digest ( 'hex' ) ;
65
79
} ) ,
66
80
) ;
67
81
}
0 commit comments