diff --git a/did/did.go b/did/did.go index ed75953..2d02afa 100644 --- a/did/did.go +++ b/did/did.go @@ -5,9 +5,11 @@ 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{} @@ -15,16 +17,25 @@ 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. diff --git a/did/document.go b/did/document.go index f78dd3d..0bd320f 100644 --- a/did/document.go +++ b/did/document.go @@ -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" ) @@ -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. @@ -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") @@ -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 } diff --git a/go.mod b/go.mod index f5125b9..cd74437 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e936095..7b3fd02 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/spec-registries.go b/spec-registries.go index 546b102..298fedb 100644 --- a/spec-registries.go +++ b/spec-registries.go @@ -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") -