1
- import {
2
- Hash ,
3
- newHashFromBigInt ,
4
- testBit ,
5
- Merkletree ,
6
- NodeLeaf ,
7
- Siblings
8
- } from '@iden3/js-merkletree' ;
1
+ import { Hash , testBit , Merkletree , NodeLeaf , Siblings } from '@iden3/js-merkletree' ;
9
2
10
3
import { NODE_TYPE_LEAF } from '@iden3/js-merkletree' ;
11
4
import { hashElems } from '@iden3/js-merkletree' ;
12
5
import { ProofNode } from './status/reverse-sparse-merkle-tree' ;
6
+ import { Iden3SmtRhsCredentialStatusPublisher } from './status/credential-status-publisher' ;
7
+ import { CredentialStatusType } from '../verifiable' ;
13
8
/**
14
9
* Interface to unite contains three trees: claim, revocation and rootOfRoots
15
10
* Also contains the current state of identity
@@ -29,6 +24,7 @@ export interface TreesModel {
29
24
* A reverse hash service (RHS) is a centralized or decentralized service for storing publicly available data about identity.
30
25
* Such data are identity state and state of revocation tree and roots tree root tree.
31
26
*
27
+ * @deprecated Use `pushHashesToReverseHashService` instead.
32
28
* @param {Hash } state - current state of identity
33
29
* @param {TreesModel } trees - current trees of identity (claims, revocation, rootOfRoots )
34
30
* @param {string } rhsUrl - URL of service
@@ -41,6 +37,28 @@ export async function pushHashesToRHS(
41
37
rhsUrl : string ,
42
38
revokedNonces ?: number [ ]
43
39
) : Promise < void > {
40
+ const nodes = await getNodesRepresentation ( revokedNonces , trees , state ) ;
41
+ const publisher = new Iden3SmtRhsCredentialStatusPublisher ( ) ;
42
+ await publisher . publish ( {
43
+ nodes,
44
+ credentialStatusType : CredentialStatusType . Iden3ReverseSparseMerkleTreeProof ,
45
+ rhsUrl : rhsUrl
46
+ } ) ;
47
+ }
48
+
49
+ /**
50
+ * Retrieves the representation of nodes for generating a proof.
51
+ *
52
+ * @param revokedNonces - An array of revoked nonces.
53
+ * @param trees - The TreesModel object containing the necessary trees.
54
+ * @param state - The hash of the state.
55
+ * @returns A Promise that resolves to an array of ProofNode objects.
56
+ */
57
+ export async function getNodesRepresentation (
58
+ revokedNonces : number [ ] | undefined ,
59
+ trees : TreesModel ,
60
+ state : Hash
61
+ ) : Promise < ProofNode [ ] > {
44
62
const nb = new NodesBuilder ( ) ;
45
63
46
64
if ( revokedNonces ) {
@@ -60,16 +78,7 @@ export async function pushHashesToRHS(
60
78
) ;
61
79
}
62
80
63
- if ( nb . nodes . length > 0 ) {
64
- await saveNodes ( nb . nodes , rhsUrl ) ;
65
- }
66
- }
67
-
68
- async function saveNodes ( nodes : ProofNode [ ] , nodeUrl : string ) : Promise < boolean > {
69
- const nodesJSON = nodes . map ( ( n ) => n . toJSON ( ) ) ;
70
- const resp = await fetch ( nodeUrl + '/node' , { method : 'post' , body : JSON . stringify ( nodesJSON ) } ) ;
71
- const status = resp . status ;
72
- return status === 200 ;
81
+ return nb . nodes ;
73
82
}
74
83
75
84
async function addRoRNode ( nb : NodesBuilder , trees : TreesModel ) : Promise < void > {
@@ -78,6 +87,7 @@ async function addRoRNode(nb: NodesBuilder, trees: TreesModel): Promise<void> {
78
87
79
88
return nb . addKey ( currentRootsTree , ( await claimsTree . root ( ) ) . bigInt ( ) ) ;
80
89
}
90
+
81
91
async function addRevocationNode (
82
92
nb : NodesBuilder ,
83
93
trees : TreesModel ,
@@ -105,9 +115,9 @@ class NodesBuilder {
105
115
async addKey ( tree : Merkletree , nodeKey : bigint ) : Promise < void > {
106
116
const { value : nodeValue , siblings } = await tree . get ( nodeKey ) ;
107
117
108
- const nodeKeyHash = newHashFromBigInt ( nodeKey ) ;
118
+ const nodeKeyHash = Hash . fromBigInt ( nodeKey ) ;
109
119
110
- const nodeValueHash = newHashFromBigInt ( nodeValue ) ;
120
+ const nodeValueHash = Hash . fromBigInt ( nodeValue ) ;
111
121
112
122
const node = new NodeLeaf ( nodeKeyHash , nodeValueHash ) ;
113
123
const newNodes : ProofNode [ ] = await buildNodesUp ( siblings , node ) ;
@@ -142,7 +152,7 @@ async function buildNodesUp(siblings: Siblings, node: NodeLeaf): Promise<ProofNo
142
152
nodes [ index ] = new ProofNode ( ) ;
143
153
}
144
154
nodes [ sl ] . hash = prevHash ;
145
- const hashOfOne = newHashFromBigInt ( BigInt ( 1 ) ) ;
155
+ const hashOfOne = Hash . fromBigInt ( BigInt ( 1 ) ) ;
146
156
147
157
nodes [ sl ] . children = [ node . entry [ 0 ] , node . entry [ 1 ] , hashOfOne ] ;
148
158
@@ -157,7 +167,7 @@ async function buildNodesUp(siblings: Siblings, node: NodeLeaf): Promise<ProofNo
157
167
nodes [ i ] . children [ 0 ] = prevHash ;
158
168
nodes [ i ] . children [ 1 ] = siblings [ i ] ;
159
169
}
160
- nodes [ i ] . hash = await hashElems ( [ nodes [ i ] . children [ 0 ] . bigInt ( ) , nodes [ i ] . children [ 1 ] . bigInt ( ) ] ) ;
170
+ nodes [ i ] . hash = hashElems ( [ nodes [ i ] . children [ 0 ] . bigInt ( ) , nodes [ i ] . children [ 1 ] . bigInt ( ) ] ) ;
161
171
162
172
prevHash = nodes [ i ] . hash ;
163
173
}
0 commit comments