Skip to content

Commit fea440e

Browse files
authored
zcap ld in didkit-wasm (spruceid#190)
* expose zcap delegation and invocation functions for web and nodejs * update to latest `ssi`
1 parent ed7f789 commit fea440e

8 files changed

Lines changed: 449 additions & 4 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
with:
3030
repository: spruceid/ssi
3131
path: ssi
32-
ref: 3aa4d1a2660e6b963eb2e963782a5602d203e6d1
32+
ref: 21c13cc96a28c36fa6d1cb8407fc235035fb9f6d
3333
submodules: true
3434

3535
- name: Cache Cargo registry and build artifacts
@@ -180,7 +180,7 @@ jobs:
180180
with:
181181
repository: spruceid/ssi
182182
path: ssi
183-
ref: 3aa4d1a2660e6b963eb2e963782a5602d203e6d1
183+
ref: 21c13cc96a28c36fa6d1cb8407fc235035fb9f6d
184184
submodules: true
185185

186186
- name: Cache Cargo registry and build artifacts

‎lib/node/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ neon-build = "0.8.1"
1313

1414
[dependencies]
1515
serde = "1.0"
16+
serde_json = "1.0"
1617

1718
[dependencies.ssi]
1819
version = "0.2.1"

‎lib/node/index.d.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ declare module "didkit" {
4545
function verifyPresentation(vp: any, options: Options): VerifyResult;
4646

4747
function DIDAuth(did: string, options: Options, key: Key): string;
48+
49+
function delegateCapability(del: any, options: Options, parents: string[], key: Key): any;
50+
function prepareDelegateCapability(del: any, options: Options, parents: string[], key: Key): any;
51+
function completeDelegateCapability(del: any, prep: any, sig: string): any;
52+
function verifyDelegation(del: any, options: Options): VerifyResult;
53+
54+
function invokeCapability(inv: any, target: string, options: Options, key: Key): any;
55+
function prepareInvokeCapability(inv: any, target: string, options: Options, key: Key): any;
56+
function completeInvokeCapability(inv: any, prep: any, sig: string): any;
57+
function verifyInvocation(inv: any, del: any, options: Options): VerifyResult;
58+
function verifyInvocationSignature(inv: any, options: Options): VerifyResult;
59+
60+
function jwkFromTezosKey(key: string): Ed25519Key;
4861
}

‎lib/node/src/didkit.rs‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ use didkit::error::Error as DIDKitError;
44
#[cfg(doc)]
55
use didkit::error::{didkit_error_code, didkit_error_message};
66
use didkit::get_verification_method;
7+
use didkit::jwk_from_tezos_key as tz_to_jwk;
78
use didkit::runtime;
9+
use didkit::ProofPreparation;
810
use didkit::Source;
911
use didkit::VerifiableCredential;
1012
use didkit::VerifiablePresentation;
1113
use didkit::DID_METHODS;
1214
use didkit::JWK;
15+
use didkit::URI;
16+
use didkit::{Delegation, Invocation};
1317
use didkit::{JWTOrLDPOptions, LinkedDataProofOptions, ProofFormat};
1418

1519
use crate::error::Error;
1620
use crate::{arg, throws};
1721

22+
type GenericInvocation = Invocation<serde_json::Value>;
23+
type GenericDelegation = Delegation<serde_json::Value>;
24+
1825
pub static VERSION: &str = env!("CARGO_PKG_VERSION");
1926

2027
pub 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+
}

‎lib/node/src/lib.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,32 @@ register_module!(mut m, {
2121
m.export_function("verifyPresentation", didkit::verify_presentation)?;
2222
m.export_function("DIDAuth", didkit::did_auth)?;
2323

24+
m.export_function("delegateCapability", didkit::delegate_capability)?;
25+
m.export_function(
26+
"prepareDelegateCapability",
27+
didkit::prepare_delegate_capability,
28+
)?;
29+
m.export_function(
30+
"completeDelegateCapability",
31+
didkit::complete_delegate_capability,
32+
)?;
33+
34+
m.export_function("verifyDelegation", didkit::verify_delegation)?;
35+
36+
m.export_function("invokeCapability", didkit::invoke_capability)?;
37+
m.export_function("prepareInvokeCapability", didkit::prepare_invoke_capability)?;
38+
m.export_function(
39+
"completeInvokeCapability",
40+
didkit::complete_invoke_capability,
41+
)?;
42+
43+
m.export_function("verifyInvocation", didkit::verify_invocation)?;
44+
m.export_function(
45+
"verifyInvocationSignature",
46+
didkit::verify_invocation_signature,
47+
)?;
48+
49+
m.export_function("jwkFromTezosKey", didkit::jwk_from_tezos_key)?;
50+
2451
Ok(())
2552
});

‎lib/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub use ssi::vc::Presentation as VerifiablePresentation;
3333
pub use ssi::vc::ProofPurpose;
3434
pub use ssi::vc::VerificationResult;
3535
pub use ssi::vc::URI;
36+
pub use ssi::zcap::{Delegation, Invocation};
37+
pub use ssi::tzkey::jwk_from_tezos_key;
3638

3739
use core::str::FromStr;
3840
use serde::{Deserialize, Serialize};

‎lib/web/Cargo.toml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ crate-type = ["cdylib"]
4242
wasm-opt = false
4343

4444
[features]
45-
default = ["generate", "issue", "verify"]
45+
default = ["generate", "issue", "verify", "invoke", "delegate"]
4646

4747
generate = []
4848
issue = []
4949
verify = []
5050
credential = []
5151
presentation = []
52+
delegate = []
53+
invoke = []
54+
zcap = []

0 commit comments

Comments
 (0)