ssi

2025-12-10 0 993

SSI

The SSI library provides a simple and modular API to sign and verify claims
exchanged between applications using
Decentralized Identifiers (DIDs). SSI is embedded in the
cross-platform didkit library as a core dependency.

This library supports the two main families of verifiable claims:

  • JSON Web Tokens (JWT) where claims are encoded into JSON and
    secured using JSON Web Signatures; and
  • W3C\’s Verifiable Credentials (VCs), a
    Linked-Data-based model where claims (VCs) can be
    interpreted as RDF datasets. VC supports multiple signature formats
    provided by SSI:

    • VC over JWT (JWT-VC), a restricted form of JWT following the
      VC data model; or
    • Data Integrity, encoding the claims and their proof
      in the same JSON-LD document using a wide variety of
      cryptographic suites.

Basic Usage

SSI provides various functions to parse, verify, create and sign various
kind of claims. This section shows how to use these functions in combination
with JSON Web Signatures (or Tokens) and Verifiable Credentials.

Verification

The simplest type of claim to load and verify is probably JSON Web
Signatures (JWSs), often use to encode JSON Web Tokens (JWTs). To represent
such claims SSI provides the JwsBuf type representing a JWS
in compact textual form. One can load a JWS using new and verify
it using verify.

use ssi::prelude::*;

// Load a JWT from the file system.
let jwt = JwsBuf::new(
  std::fs::read_to_string(\"examples/files/claims.jwt\")
  .expect(\"unable to load JWT\")
).expect(\"invalid JWS\");

// Setup a verification method resolver, in charge of retrieving the
// public key used to sign the JWT.
// Here we use the example `ExampleDIDResolver` resolver, enabled with the
// `example` feature.
let vm_resolver = ExampleDIDResolver::default().into_vm_resolver::<AnyJwkMethod>();

// Setup the verification parameters.
let params = VerificationParameters::from_resolver(vm_resolver);

// Verify the JWT.
assert!(jwt.verify(&params).await.expect(\"verification failed\").is_ok())

Verifiable Credentials

Verifiable Credential are much more complex as they require interpreting
the input claims and proofs, such as Data-Integrity proofs as Linked-Data
using JSON-LD. This operation is highly configurable. SSI provide
functions exposing various levels of implementation details that you can
tweak as needed. The simplest of them is any_credential_from_json_str
that will simply load a VC from a string, assuming it is signed using
any Data-Integrity proof supported by SSI.

use ssi::prelude::*;

let vc = ssi::claims::vc::v1::data_integrity::any_credential_from_json_str(
  &std::fs::read_to_string(\"examples/files/vc.jsonld\")
  .expect(\"unable to load VC\")
).expect(\"invalid VC\");

// Setup a verification method resolver, in charge of retrieving the
// public key used to sign the JWT.
let vm_resolver = ExampleDIDResolver::default().into_vm_resolver();

// Setup the verification parameters.
let params = VerificationParameters::from_resolver(vm_resolver);

assert!(vc.verify(&params).await.expect(\"verification failed\").is_ok());

Signature & Custom Claims

In the previous section we have seen how to load and verify arbitrary
claims. This section shows how to create and sign custom claims.
With SSI, any Rust type can serve as claims as long as it complies to
certain conditions such as implementing serialization/deserialization
functions using serde.
Don\’t forget to enable the derive feature for serde.

In the following example, we create a custom type MyClaims and sign it
as a JWT.

use serde::{Serialize, Deserialize};
use ssi::prelude::*;

// Defines the shape of our custom claims.
#[derive(Serialize, Deserialize)]
pub struct MyClaims {
  name: String,
  email: String
}

// Create JWT claims from our custom (\"private\") claims.
let claims = JWTClaims::from_private_claims(MyClaims {
  name: \"John Smith\".to_owned(),
  email: \"john.smith@example.org\".to_owned()
});

// Create a random signing key, and turn its public part into a DID URL.
let mut key = JWK::generate_p256(); // requires the `p256` feature.
let did = DIDJWK::generate_url(&key.to_public());
key.key_id = Some(did.into());

// Sign the claims.
let jwt = claims.sign(&key).await.expect(\"signature failed\");

// Create a verification method resolver, which will be in charge of
// decoding the DID back into a public key.
let vm_resolver = DIDJWK.into_vm_resolver::<AnyJwkMethod>();

// Setup the verification parameters.
let params = VerificationParameters::from_resolver(vm_resolver);

// Verify the JWT.
assert!(jwt.verify(&params).await.expect(\"verification failed\").is_ok());

// Print the JWT.
println!(\"{jwt}\")

Verifiable Credential

We can use a similar technique to sign a VC with custom claims.
The SpecializedJsonCredential type provides a customizable
implementation of the VC data-model 1.1 where you can set the credential type
yourself.

use static_iref::uri;
use serde::{Serialize, Deserialize};
use ssi::claims::vc::syntax::NonEmptyVec;
use ssi::prelude::*;

// Defines the shape of our custom claims.
#[derive(Serialize, Deserialize)]
pub struct MyCredentialSubject {
  #[serde(rename = \"https://ex**amp*le.org/#name\")]
  name: String,

  #[serde(rename = \"https://e*xampl*e.*org/#email\")]
  email: String
}

let credential = ssi::claims::vc::v1::JsonCredential::<MyCredentialSubject>::new(
  Some(uri!(\"https://example.*o**rg/#CredentialId\").to_owned()), // id
  uri!(\"https://exampl***e.org/#Issuer\").to_owned().into(), // issuer
  DateTime::now().into(), // issuance date
  NonEmptyVec::new(MyCredentialSubject {
    name: \"John Smith\".to_owned(),
    email: \"john.smith@example.org\".to_owned()
  })
);

// Create a random signing key, and turn its public part into a DID URL.
let key = JWK::generate_p256(); // requires the `p256` feature.
let did = DIDJWK::generate_url(&key.to_public());

// Create a verification method resolver, which will be in charge of
// decoding the DID back into a public key.
let vm_resolver = DIDJWK.into_vm_resolver();

// Create a signer from the secret key.
// Here we use the simple `SingleSecretSigner` signer type which always uses
// the same provided secret key to sign messages.
let signer = SingleSecretSigner::new(key.clone()).into_local();

// Turn the DID URL into a verification method reference.
let verification_method = did.into_iri().into();

// Automatically pick a suitable Data-Integrity signature suite for our key.
let cryptosuite = AnySuite::pick(&key, Some(&verification_method))
  .expect(\"could not find appropriate cryptosuite\");

let vc = cryptosuite.sign(
  credential,
  &vm_resolver,
  &signer,
  ProofOptions::from_method(verification_method)
).await.expect(\"signature failed\");

It is critical that custom claims can be interpreted as Linked-Data. In
the above example this is done by specifying a serialization URL for each
field of MyCredentialSubject. This can also be done by creating a custom
JSON-LD context and embed it to credential using either
SpecializedJsonCredential\’s context field or leveraging its context type
parameter.

Data-Models

The examples above are using the VC data-model 1.1, but you ssi also has support for:

  • VC data-model 2.0
  • A wrapper type to accept both

Features

Security Audits

ssi has undergone the following security reviews:

  • March 14th, 2022 – Trail of Bits | Summary of Findings

Testing

Testing SSI requires the RDF canonicalization test suite, which is embedded as
a git submodule.

$ git submodule update --init
$ cargo test --workspace

下载源码

通过命令行克隆项目:

git clone https://github.com/spruceid/ssi.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 ssi https://www.zuozi.net/33499.html

SingulinkFX
上一篇: SingulinkFX
VisioPanelAddinVSTO
下一篇: VisioPanelAddinVSTO
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务