diff --git a/go.mod b/go.mod index 5ac5b84..202144a 100644 --- a/go.mod +++ b/go.mod @@ -3,34 +3,36 @@ module github.com/13x-tech/go-did-web go 1.19 require ( - github.com/TBD54566975/ssi-sdk v0.0.4-alpha + github.com/TBD54566975/ssi-sdk v0.0.4-alpha.0.20230629211408-d0031cf86600 github.com/gorilla/mux v1.8.0 go.etcd.io/bbolt v1.3.7 ) require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect + golang.org/x/net v0.10.0 // indirect ) require ( github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect github.com/cloudflare/circl v1.3.3 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.13.0 // indirect + github.com/go-playground/validator/v10 v10.14.1 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/hyperledger/aries-framework-go v0.3.1 // indirect + github.com/hyperledger/aries-framework-go v0.3.2 // indirect github.com/hyperledger/aries-framework-go/component/kmscrypto v0.0.0-20230427134832-0c9969493bd3 // indirect github.com/hyperledger/aries-framework-go/component/log v0.0.0-20230427134832-0c9969493bd3 // indirect github.com/hyperledger/aries-framework-go/component/models v0.0.0-20230501135648-a9a7ad029347 // indirect github.com/hyperledger/aries-framework-go/spi v0.0.0-20230427134832-0c9969493bd3 // indirect github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69 // indirect - github.com/leodido/go-urn v1.2.3 // indirect + github.com/leodido/go-urn v1.2.4 // indirect github.com/lestrrat-go/blackmagic v1.0.1 // indirect github.com/lestrrat-go/httpcc v1.0.1 // indirect github.com/lestrrat-go/httprc v1.0.4 // indirect @@ -48,11 +50,11 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pquerna/cachecontrol v0.1.0 // indirect github.com/segmentio/asm v1.2.0 // indirect - github.com/sirupsen/logrus v1.9.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/stretchr/testify v1.8.4 github.com/urfave/cli/v2 v2.25.7 golang.org/x/crypto v0.9.0 // indirect - golang.org/x/sys v0.8.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/sys v0.10.0 // indirect + golang.org/x/text v0.11.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9964985..1c2e726 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/TBD54566975/ssi-sdk v0.0.4-alpha h1:GbZG0S3xeaWQi2suWw2VjGRhM/S2RrIsfiubxSHlViE= -github.com/TBD54566975/ssi-sdk v0.0.4-alpha/go.mod h1:O4iANflxGCX0NbjHOhthq0X0il2ZYNMYlUnjEa0rsC0= +github.com/TBD54566975/ssi-sdk v0.0.4-alpha.0.20230629211408-d0031cf86600 h1:POzVbjLxtQYQGVGaU95uQtUvhf3djJsAAWrADV6Ou4Y= +github.com/TBD54566975/ssi-sdk v0.0.4-alpha.0.20230629211408-d0031cf86600/go.mod h1:yPkVO9MCC/kRu+lut3jllhnCV0gEqSubaaSVT7xLSOs= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= @@ -13,21 +13,24 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.13.0 h1:cFRQdfaSMCOSfGCCLB20MHvuoHb/s5G8L5pu2ppK5AQ= -github.com/go-playground/validator/v10 v10.13.0/go.mod h1:dwu7+CG8/CtBiJFZDz4e+5Upb6OLw04gtBYw0mcG/z4= +github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k= +github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= 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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/hyperledger/aries-framework-go v0.3.1 h1:44hOqFdVtXPRmfxK1dHds1g1mouJFNeP1D/PBjDxRv8= -github.com/hyperledger/aries-framework-go v0.3.1/go.mod h1:SorUysWEBw+uyXhY5RAtg2iyNkWTIIPM8+Slkt1Spno= +github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= +github.com/hyperledger/aries-framework-go v0.3.2 h1:GsSUaSEW82cr5X8b3Qf90GAi37kmTKHqpPJLhar13X8= +github.com/hyperledger/aries-framework-go v0.3.2/go.mod h1:SorUysWEBw+uyXhY5RAtg2iyNkWTIIPM8+Slkt1Spno= github.com/hyperledger/aries-framework-go/component/kmscrypto v0.0.0-20230427134832-0c9969493bd3 h1:PCbDSujjQ6oTEnAHgtThNmbS7SPAYEDBlKOnZFE+Ujw= github.com/hyperledger/aries-framework-go/component/kmscrypto v0.0.0-20230427134832-0c9969493bd3/go.mod h1:aEk0vHBmZsAdDfXaI12Kg5ipZGiB3qNqgbPt/e/Hm2s= github.com/hyperledger/aries-framework-go/component/log v0.0.0-20230427134832-0c9969493bd3 h1:x5qFQraTX86z9GCwF28IxfnPm6QH5YgHaX+4x97Jwvw= @@ -41,8 +44,8 @@ github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69 h1:kMJlf8z8wUcpy github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69/go.mod h1:tlkavyke+Ac7h8R3gZIjI5LKBcvMlSWnXNMgT3vZXo8= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/leodido/go-urn v1.2.3 h1:6BE2vPT0lqoz3fmOesHZiaiFh7889ssCo2GMvLCfiuA= -github.com/leodido/go-urn v1.2.3/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80= github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU= github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE= @@ -80,8 +83,8 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys= github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs= -github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= -github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -109,6 +112,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -122,8 +126,9 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -132,8 +137,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= @@ -141,6 +147,7 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/didweb/didweb.go b/pkg/didweb/didweb.go index 88ea94b..acd2d91 100644 --- a/pkg/didweb/didweb.go +++ b/pkg/didweb/didweb.go @@ -9,17 +9,14 @@ import ( "strconv" "strings" + "github.com/TBD54566975/ssi-sdk/crypto" "github.com/TBD54566975/ssi-sdk/did" + "github.com/TBD54566975/ssi-sdk/did/web" ) -func New(id string) (*did.Document, error) { - - doc := did.NewDIDDocumentBuilder() - if err := doc.SetID(fmt.Sprintf("did:web:%s", id)); err != nil { - return nil, fmt.Errorf("invalid id %s: %w", id, err) - } - - return doc.Build() +func New(id string, publicKey []byte) (*did.Document, error) { + dweb := web.DIDWeb(fmt.Sprintf("did:web:%s", id)) + return dweb.CreateDoc(crypto.P256, publicKey) } type DIDWebURL struct { @@ -41,9 +38,11 @@ func (u *DIDWebURL) URL() string { } return rawURL.String() } + func (u DIDWebURL) RawHost() string { return u.host } + func (u DIDWebURL) Host() string { host := u.host port := 0 @@ -72,14 +71,7 @@ func (u DIDWebURL) Host() string { } func (u *DIDWebURL) DID() string { - parts := u.parts - if len(parts) == 0 { - return fmt.Sprintf("did:web:%s", u.host) - } - for i, part := range parts { - parts[i] = url.QueryEscape(part) - } - return fmt.Sprintf("did:web:%s:%s", u.host, strings.Join(parts, ":")) + return fmt.Sprintf("did:web:%s", u.ID()) } func (u *DIDWebURL) ID() string { @@ -178,6 +170,9 @@ func Resolve(id string, client *http.Client) (*did.Document, error) { return &doc, nil } +func Test() { +} + var ( ErrorDIDNotFound = fmt.Errorf("not found") ) diff --git a/pkg/didweb/didweb_test.go b/pkg/didweb/didweb_test.go index 129a038..93e983b 100644 --- a/pkg/didweb/didweb_test.go +++ b/pkg/didweb/didweb_test.go @@ -1,20 +1,56 @@ package didweb import ( + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" "fmt" "testing" + "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/stretchr/testify/assert" ) +func GenSchnorrPubKey() *secp256k1.PublicKey { + private, _ := secp256k1.GeneratePrivateKey() + return private.PubKey() +} + +// compressPublicKey compresses an ECDSA public key. +func compressPublicKey(pubKey *ecdsa.PublicKey) []byte { + byteLen := (pubKey.Params().BitSize + 7) >> 3 + compressed := make([]byte, 1+byteLen) + compressed[0] = 2 // 02/03 prefix depending on y's least significant bit + if pubKey.Y.Bit(0) == 1 { + compressed[0] = 3 + } + + xBytes := pubKey.X.Bytes() + copy(compressed[1+byteLen-len(xBytes):], xBytes) + return compressed +} + +func GenP256Key() ([]byte, error) { + pKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return nil, fmt.Errorf("could not generate key: %w", err) + } + return compressPublicKey(&pKey.PublicKey), nil +} + // Testing New() func TestNew(t *testing.T) { id := "example.com:alice" - doc, err := New(id) + pubKey, err := GenP256Key() + if err != nil { + t.Fatal(err) + } + doc, err := New(id, pubKey) assert.NoError(t, err) assert.NotNil(t, doc) assert.Equal(t, "did:web:example.com:alice", doc.ID) + //TODO assert Key } func TestParsePath(t *testing.T) { diff --git a/pkg/server/server.go b/pkg/server/server.go index 8049d27..a2af307 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -1,6 +1,7 @@ package server import ( + "encoding/hex" "encoding/json" "fmt" "io" @@ -92,7 +93,7 @@ func (b *PaymentBroker) WaitForPayment(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] - fmt.Printf("Connected and waiting: %s", id) + log.Printf("Connected and waiting: %s\n", id) b.mu.Lock() clients, ok := b.clients[id] if !ok { @@ -334,6 +335,7 @@ func (s *Server) handlePaid(w http.ResponseWriter, r *http.Request) { go s.payBroker.BroadcastPayment(doc.ID) s.jsonSuccess(w, "ok") } + func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("ok")) @@ -364,11 +366,10 @@ func (s *Server) jsonSuccess(w http.ResponseWriter, response any) { } func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { + if r.Method != http.MethodPost { w.WriteHeader(http.StatusNotFound) return } - body, err := io.ReadAll(r.Body) if err != nil { s.errorResponse(w, 500, "could not get boxy") @@ -380,6 +381,12 @@ func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) { return } + pubKey, err := hex.DecodeString(input.OwnerPubKey) + if err != nil { + s.errorResponse(w, 400, "invalid request") + return + } + parts := strings.Split(input.ID, ":") if len(parts) < 2 { s.errorResponse(w, 400, fmt.Sprintf("id must be in the format of %s:sally, where sally is the name you're registering", s.domain)) @@ -395,11 +402,14 @@ func (s *Server) handleRegister(w http.ResponseWriter, r *http.Request) { return } - doc, err := didstorage.DIDFromProps(input.ID, input.Keys, input.Services) + doc, err := didstorage.DIDFromProps(input.ID, pubKey, input.AdditionalKeys, input.Services) if err != nil { s.errorResponse(w, 500, fmt.Sprintf("could not register: %s", err.Error())) return } + docJSON, _ := json.Marshal(doc) + + fmt.Printf("\n\nDoc: %s\n\n", docJSON) if payReq, ok := s.regStore.Get(doc); ok { s.jsonSuccess(w, payReq) @@ -447,18 +457,34 @@ func (s *Server) handleResolve(w http.ResponseWriter, r *http.Request) { func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) {} -func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) {} +func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete { + w.WriteHeader(http.StatusNotFound) + return + } + pathParts := strings.Split(r.RequestURI, "/") + if len(pathParts) < 3 { + s.errorResponse(w, 400, "invalid") + return + } + id := pathParts[2] + if len(id) == 0 { + s.errorResponse(w, 400, "invalid id") + return + } + url, err := didweb.Parse(id) + if err != nil { + s.errorResponse(w, 400, "invalid id") + return + } -func (s *Server) keyAuthMiddleware(next http.HandlerFunc) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - keys, ok := r.Header["X-Api-Key"] - if !ok || len(keys) == 0 { - s.errorResponse(w, 401, "unauthorized") + //TODO: Validate Auth - challenge middleware + if strings.EqualFold(url.RawHost(), s.domain) { + if err := s.store.Delete(url.ID()); err == nil { + s.jsonSuccess(w, "ok") return } - - next.ServeHTTP(w, r) - }) + } } func (s *Server) addCORS(limited bool, next http.HandlerFunc) http.HandlerFunc { @@ -479,7 +505,8 @@ func (s *Server) addCORS(limited bool, next http.HandlerFunc) http.HandlerFunc { } type RegisterRequest struct { - ID string `json:"id"` - Keys []didstorage.KeyInput `json:"keys"` - Services []did.Service `json:"services"` + ID string `json:"id"` + OwnerPubKey string `json:"ownerPubKey,omitempty"` + AdditionalKeys []didstorage.KeyInput `json:"additionalKeys"` + Services []did.Service `json:"services"` } diff --git a/pkg/storage/didstorage/didstorage.go b/pkg/storage/didstorage/didstorage.go index 7ebacd4..ac0fb98 100644 --- a/pkg/storage/didstorage/didstorage.go +++ b/pkg/storage/didstorage/didstorage.go @@ -18,15 +18,19 @@ type Storage interface { Delete(id string) error } -func DIDFromProps(id string, keys []KeyInput, services []did.Service) (*did.Document, error) { - newDID, err := didweb.New(id) +func DIDFromProps(id string, ownerKey []byte, additionalKeys []KeyInput, services []did.Service) (*did.Document, error) { + if ownerKey == nil { + return nil, fmt.Errorf("must supply a valid owner key") + } + + newDID, err := didweb.New(id, ownerKey) if err != nil { - return nil, err + return nil, fmt.Errorf("could not create did: %w", err) } doc := did.NewDIDDocumentBuilder() doc.Document = newDID - for _, key := range keys { + for _, key := range additionalKeys { key.VerificationMethod.Controller = doc.ID if err := doc.AddVerificationMethod(key.VerificationMethod); err != nil { return nil, fmt.Errorf("verification method error: %w", err) @@ -269,7 +273,6 @@ func (s *RegisterStore) validatePaymentRequest(payReq string) bool { } if len(responseData) > 0 { - fmt.Printf("Response Data: %s\n", responseData) return true }