Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion did/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/nuts-foundation/go-did"
"math/big"
"net/url"
"strings"

ssi "github.com/nuts-foundation/go-did"
)

var _ fmt.Stringer = DID{}
var _ encoding.TextMarshaler = DID{}

// DIDContextV1 contains the JSON-LD context for a DID Document
const DIDContextV1 = "https://www.w3.org/ns/did/v1"
const SECP256Recovery = "https://w3id.org/security/suites/secp256k1recovery-2020/v2"

// DIDContextV1URI returns DIDContextV1 as a URI
func DIDContextV1URI() ssi.URI {
return ssi.MustParseURI(DIDContextV1)
}

// SECP256RecoveryURI returns SECP256Recovery as a URI
func SECP256RecoveryURI() ssi.URI {
return ssi.MustParseURI(SECP256Recovery)
}

// DID represent a Decentralized Identifier as specified by the DID Core specification (https://www.w3.org/TR/did-core/#identifier).
type DID struct {
// Method is the DID method, e.g. "example".
Method string
// If the Method is blockchain and the chain id is neccessary use this.
// e.g. Method = "ethr", MethodID = "1"
MethodID *big.Int
// ID is the method-specific ID, in escaped form.
ID string
// DecodedID is the method-specific ID, in unescaped form.
Expand Down
18 changes: 15 additions & 3 deletions did/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"errors"
"fmt"

"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/multiformats/go-multibase"
"strings"

"github.com/nuts-foundation/go-did"
ssi "github.com/nuts-foundation/go-did"
"github.com/nuts-foundation/go-did/internal/marshal"
"github.com/shengdoushi/base58"
)
Expand Down Expand Up @@ -299,6 +301,8 @@ type VerificationMethod struct {
// PublicKeyBase58 is deprecated and should not be used anymore. Use PublicKeyMultibase or PublicKeyJwk instead.
PublicKeyBase58 string `json:"publicKeyBase58,omitempty"`
PublicKeyJwk map[string]interface{} `json:"publicKeyJwk,omitempty"`
// BlockchainAccountId can be used instead of public key when using smart contact based on DIDRegistry ERC1056.
BlockchainAccountId string `json:"blockchainAccountId,omitempty"`
}

// NewVerificationMethod is a convenience method to easily create verificationMethods based on a set of given params.
Expand Down Expand Up @@ -340,7 +344,7 @@ func NewVerificationMethod(id DIDURL, keyType ssi.KeyType, controller DID, key c
}
vm.PublicKeyJwk = jwkAsMap
}
if keyType == ssi.ED25519VerificationKey2018 || keyType == ssi.ED25519VerificationKey2020 {
if keyType == ssi.ED25519VerificationKey2018 || keyType == ssi.ED25519VerificationKey2020 {
ed25519Key, ok := key.(ed25519.PublicKey)
if !ok {
return nil, errors.New("wrong key type")
Expand All @@ -352,6 +356,14 @@ func NewVerificationMethod(id DIDURL, keyType ssi.KeyType, controller DID, key c
vm.PublicKeyMultibase = encodedKey
}

if keyType == ssi.ECDSASECP256K1RecoveryMethod2020 {
address := id.DID.ID
if common.HexToAddress(address) == (common.Address{}) || len(address) != 42 {
return nil, errors.New("invalid address")
}
vm.BlockchainAccountId = fmt.Sprintf("eip155:%d:%s", id.DID.MethodID, address)
}

return vm, nil
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ethereum/go-ethereum v1.12.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/ethereum/go-ethereum v1.12.2 h1:eGHJ4ij7oyVqUQn48LBz3B7pvQ8sV0wGJiIE6gDq/6Y=
github.com/ethereum/go-ethereum v1.12.2/go.mod h1:1cRAEV+rp/xX0zraSCBnu9Py3HQ+geRMj3HdR+k0wfI=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
Expand Down
5 changes: 4 additions & 1 deletion spec-registries.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ const ECDSASECP256K1VerificationKey2019 = KeyType("EcdsaSecp256k1VerificationKey
// https://w3c-ccg.github.io/lds-rsa2018/
const RSAVerificationKey2018 = KeyType("RsaVerificationKey2018")

// https://identity.foundation/EcdsaSecp256k1RecoverySignature2020/
// https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md
const ECDSASECP256K1RecoveryMethod2020 = KeyType("EcdsaSecp256k1RecoveryMethod2020")

type ProofType string

// JsonWebSignature2020 is a Proof type.
// https://w3c-ccg.github.io/lds-jws2020
const JsonWebSignature2020 = ProofType("JsonWebSignature2020")