@@ -4,17 +4,24 @@ use didkit::error::Error as DIDKitError;
44#[ cfg( doc) ]
55use didkit:: error:: { didkit_error_code, didkit_error_message} ;
66use didkit:: get_verification_method;
7+ use didkit:: jwk_from_tezos_key as tz_to_jwk;
78use didkit:: runtime;
9+ use didkit:: ProofPreparation ;
810use didkit:: Source ;
911use didkit:: VerifiableCredential ;
1012use didkit:: VerifiablePresentation ;
1113use didkit:: DID_METHODS ;
1214use didkit:: JWK ;
15+ use didkit:: URI ;
16+ use didkit:: { Delegation , Invocation } ;
1317use didkit:: { JWTOrLDPOptions , LinkedDataProofOptions , ProofFormat } ;
1418
1519use crate :: error:: Error ;
1620use crate :: { arg, throws} ;
1721
22+ type GenericInvocation = Invocation < serde_json:: Value > ;
23+ type GenericDelegation = Delegation < serde_json:: Value > ;
24+
1825pub static VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
1926
2027pub fn get_version ( mut cx : FunctionContext ) -> JsResult < JsString > {
@@ -148,3 +155,133 @@ pub fn verify_presentation(mut cx: FunctionContext) -> JsResult<JsValue> {
148155 let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & result) ) ?;
149156 Ok ( result)
150157}
158+
159+ pub fn delegate_capability ( mut cx : FunctionContext ) -> JsResult < JsValue > {
160+ let del = arg ! ( cx, 0 , GenericDelegation ) ;
161+ let options = arg ! ( cx, 1 , LinkedDataProofOptions ) ;
162+ let parents = arg ! ( cx, 2 , Vec <String >) ;
163+ let key = arg ! ( cx, 3 , JWK ) ;
164+
165+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
166+ let proof = throws ! (
167+ cx,
168+ rt. block_on( del. generate_proof(
169+ & key,
170+ & options,
171+ DID_METHODS . to_resolver( ) ,
172+ & parents. iter( ) . map( |p| p. as_ref( ) ) . collect:: <Vec <& str >>( ) ,
173+ ) )
174+ ) ?;
175+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & proof) ) ?;
176+ Ok ( result)
177+ }
178+
179+ pub fn prepare_delegate_capability ( mut cx : FunctionContext ) -> JsResult < JsValue > {
180+ let del = arg ! ( cx, 0 , GenericDelegation ) ;
181+ let options = arg ! ( cx, 1 , LinkedDataProofOptions ) ;
182+ let parents = arg ! ( cx, 2 , Vec <String >) ;
183+ let key = arg ! ( cx, 3 , JWK ) ;
184+
185+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
186+ let prep = throws ! (
187+ cx,
188+ rt. block_on( del. prepare_proof(
189+ & key,
190+ & options,
191+ DID_METHODS . to_resolver( ) ,
192+ & parents. iter( ) . map( |p| p. as_ref( ) ) . collect:: <Vec <& str >>( ) ,
193+ ) )
194+ ) ?;
195+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & prep) ) ?;
196+ Ok ( result)
197+ }
198+
199+ pub fn complete_delegate_capability ( mut cx : FunctionContext ) -> JsResult < JsValue > {
200+ let del = arg ! ( cx, 0 , GenericDelegation ) ;
201+ let prep = arg ! ( cx, 1 , ProofPreparation ) ;
202+ let sig = arg ! ( cx, 2 , String ) ;
203+
204+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
205+ let proof = throws ! ( cx, rt. block_on( prep. complete( & sig) ) ) ?;
206+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & del. set_proof( proof) ) ) ?;
207+ Ok ( result)
208+ }
209+
210+ pub fn verify_delegation ( mut cx : FunctionContext ) -> JsResult < JsValue > {
211+ let del = arg ! ( cx, 0 , GenericDelegation ) ;
212+ let options = arg ! ( cx, 1 , LinkedDataProofOptions ) ;
213+
214+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
215+ let res = rt. block_on ( del. verify ( Some ( options) , DID_METHODS . to_resolver ( ) ) ) ;
216+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & res) ) ?;
217+ Ok ( result)
218+ }
219+
220+ pub fn invoke_capability ( mut cx : FunctionContext ) -> JsResult < JsValue > {
221+ let inv = arg ! ( cx, 0 , GenericInvocation ) ;
222+ let target = arg ! ( cx, 1 , URI ) ;
223+ let options = arg ! ( cx, 2 , LinkedDataProofOptions ) ;
224+ let key = arg ! ( cx, 3 , JWK ) ;
225+
226+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
227+ let proof = throws ! (
228+ cx,
229+ rt. block_on( inv. generate_proof( & key, & options, DID_METHODS . to_resolver( ) , & target) )
230+ ) ?;
231+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & inv. set_proof( proof) ) ) ?;
232+ Ok ( result)
233+ }
234+
235+ pub fn prepare_invoke_capability ( mut cx : FunctionContext ) -> JsResult < JsValue > {
236+ let inv = arg ! ( cx, 0 , GenericInvocation ) ;
237+ let target = arg ! ( cx, 1 , URI ) ;
238+ let options = arg ! ( cx, 2 , LinkedDataProofOptions ) ;
239+ let key = arg ! ( cx, 3 , JWK ) ;
240+
241+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
242+ let prep = throws ! (
243+ cx,
244+ rt. block_on( inv. prepare_proof( & key, & options, DID_METHODS . to_resolver( ) , & target) )
245+ ) ?;
246+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & prep) ) ?;
247+ Ok ( result)
248+ }
249+
250+ pub fn complete_invoke_capability ( mut cx : FunctionContext ) -> JsResult < JsValue > {
251+ let inv = arg ! ( cx, 0 , GenericInvocation ) ;
252+ let prep = arg ! ( cx, 1 , ProofPreparation ) ;
253+ let sig = arg ! ( cx, 2 , String ) ;
254+
255+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
256+ let res = throws ! ( cx, rt. block_on( prep. complete( & sig) ) ) ?;
257+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & inv. set_proof( res) ) ) ?;
258+ Ok ( result)
259+ }
260+
261+ pub fn verify_invocation ( mut cx : FunctionContext ) -> JsResult < JsValue > {
262+ let inv = arg ! ( cx, 0 , GenericInvocation ) ;
263+ let del = arg ! ( cx, 1 , GenericDelegation ) ;
264+ let options = arg ! ( cx, 2 , LinkedDataProofOptions ) ;
265+
266+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
267+ let res = rt. block_on ( inv. verify ( Some ( options) , DID_METHODS . to_resolver ( ) , & del) ) ;
268+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & res) ) ?;
269+ Ok ( result)
270+ }
271+
272+ pub fn verify_invocation_signature ( mut cx : FunctionContext ) -> JsResult < JsValue > {
273+ let inv = arg ! ( cx, 0 , GenericInvocation ) ;
274+ let options = arg ! ( cx, 1 , LinkedDataProofOptions ) ;
275+
276+ let rt = throws ! ( cx, runtime:: get( ) ) ?;
277+ let res = rt. block_on ( inv. verify_signature ( Some ( options) , DID_METHODS . to_resolver ( ) ) ) ;
278+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & res) ) ?;
279+ Ok ( result)
280+ }
281+
282+ pub fn jwk_from_tezos_key ( mut cx : FunctionContext ) -> JsResult < JsValue > {
283+ let tzk = arg ! ( cx, 0 , String ) ;
284+ let jwk = throws ! ( cx, tz_to_jwk( & tzk) ) ?;
285+ let result = throws ! ( cx, neon_serde:: to_value( & mut cx, & jwk) ) ?;
286+ Ok ( result)
287+ }
0 commit comments