mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
* feat: add docs and CI workflow for publishing to onsonr.dev * (refactor): Move hway,motr executables to their own repos * feat: simplify devnet and testnet configurations * refactor: update import path for didcrypto package * docs(networks): Add README with project overview, architecture, and community links * refactor: Move network configurations to deploy directory * build: update golang version to 1.23 * refactor: move logger interface to appropriate package * refactor: Move devnet configuration to networks/devnet * chore: improve release process with date variable * (chore): Move Crypto Library * refactor: improve code structure and readability in DID module * feat: integrate Trunk CI checks * ci: optimize CI workflow by removing redundant build jobs --------- Co-authored-by: Darp Alakun <i@prad.nu>
104 lines
2.4 KiB
Go
104 lines
2.4 KiB
Go
// go:build jwx_es256k
|
|
package spec
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/bech32"
|
|
"github.com/golang-jwt/jwt"
|
|
"github.com/onsonr/sonr/crypto/keys"
|
|
"github.com/onsonr/sonr/crypto/ucan"
|
|
)
|
|
|
|
type (
|
|
Token = ucan.Token
|
|
Claims = ucan.Claims
|
|
Proof = ucan.Proof
|
|
|
|
Attenuations = ucan.Attenuations
|
|
Fact = ucan.Fact
|
|
)
|
|
|
|
var (
|
|
UCANVersion = ucan.UCANVersion
|
|
UCANVersionKey = ucan.UCANVersionKey
|
|
PrfKey = ucan.PrfKey
|
|
FctKey = ucan.FctKey
|
|
AttKey = ucan.AttKey
|
|
CapKey = ucan.CapKey
|
|
)
|
|
|
|
type ucanKeyshare struct {
|
|
addr string
|
|
issuerDID string
|
|
}
|
|
|
|
func (k ucanKeyshare) NewOriginToken(audienceDID string, att Attenuations, fct []Fact, notBefore, expires time.Time) (*ucan.Token, error) {
|
|
return k.newToken(audienceDID, nil, att, fct, notBefore, expires)
|
|
}
|
|
|
|
func (k ucanKeyshare) NewAttenuatedToken(parent *Token, audienceDID string, att ucan.Attenuations, fct []ucan.Fact, nbf, exp time.Time) (*Token, error) {
|
|
if !parent.Attenuations.Contains(att) {
|
|
return nil, fmt.Errorf("scope of ucan attenuations must be less than it's parent")
|
|
}
|
|
return k.newToken(audienceDID, append(parent.Proofs, Proof(parent.Raw)), att, fct, nbf, exp)
|
|
}
|
|
|
|
func (k ucanKeyshare) newToken(audienceDID string, prf []Proof, att Attenuations, fct []Fact, nbf, exp time.Time) (*ucan.Token, error) {
|
|
t := jwt.New(NewJWTSigningMethod("MPC256", k))
|
|
|
|
// if _, err := did.Parse(audienceDID); err != nil {
|
|
// return nil, fmt.Errorf("invalid audience DID: %w", err)
|
|
// }
|
|
|
|
t.Header[UCANVersionKey] = UCANVersion
|
|
|
|
var (
|
|
nbfUnix int64
|
|
expUnix int64
|
|
)
|
|
|
|
if !nbf.IsZero() {
|
|
nbfUnix = nbf.Unix()
|
|
}
|
|
if !exp.IsZero() {
|
|
expUnix = exp.Unix()
|
|
}
|
|
|
|
// set our claims
|
|
t.Claims = &Claims{
|
|
StandardClaims: &jwt.StandardClaims{
|
|
Issuer: k.issuerDID,
|
|
Audience: audienceDID,
|
|
NotBefore: nbfUnix,
|
|
// set the expire time
|
|
// see http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-20#section-4.1.4
|
|
ExpiresAt: expUnix,
|
|
},
|
|
Attenuations: att,
|
|
Facts: fct,
|
|
Proofs: prf,
|
|
}
|
|
|
|
raw, err := t.SignedString(nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Token{
|
|
Raw: raw,
|
|
Attenuations: att,
|
|
Facts: fct,
|
|
Proofs: prf,
|
|
}, nil
|
|
}
|
|
|
|
func getIssuerDID(pk keys.PubKey) (string, string, error) {
|
|
addr, err := bech32.ConvertAndEncode("idx", pk.Bytes())
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return fmt.Sprintf("did:sonr:%s", addr), addr, nil
|
|
}
|