Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 24c8a7e

Browse files
Remove returning private keys (#417)
* remove returning priv keys * update docs and integration tests * fix alice priv key * more int test fixes * Test fiox * Refactor credential application logic (#416) * tmp * remove side effects; use sdk lib * update issuance template * temp * temp * pr comments
1 parent e5f33f5 commit 24c8a7e

15 files changed

+128
-199
lines changed

integration/common.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package integration
22

33
import (
44
"bytes"
5+
gocrypto "crypto"
56
"embed"
67
"fmt"
78
"io"
@@ -10,10 +11,8 @@ import (
1011
"time"
1112

1213
manifestsdk "github.com/TBD54566975/ssi-sdk/credential/manifest"
13-
"github.com/TBD54566975/ssi-sdk/crypto"
1414
"github.com/cenkalti/backoff/v4"
1515
"github.com/goccy/go-json"
16-
"github.com/mr-tron/base58"
1716
"github.com/oliveagle/jsonpath"
1817
"github.com/pkg/errors"
1918
"github.com/sirupsen/logrus"
@@ -196,24 +195,14 @@ type credApplicationParams struct {
196195
ManifestID string
197196
}
198197

199-
func CreateCredentialApplicationJWT(credApplication credApplicationParams, credentialJWT, aliceDID, aliceKID, aliceDIDPrivateKey string) (string, error) {
198+
func CreateCredentialApplicationJWT(credApplication credApplicationParams, credentialJWT, aliceDID, aliceKID string, aliceDIDPrivateKey gocrypto.PrivateKey) (string, error) {
200199
logrus.Println("\n\nCreate an Application JWT:")
201200
applicationJSON, err := resolveTemplate(credApplication, "application-input.json")
202201
if err != nil {
203202
return "", err
204203
}
205204

206-
alicePrivKeyBytes, err := base58.Decode(aliceDIDPrivateKey)
207-
if err != nil {
208-
return "", errors.Wrap(err, "base58 decoding")
209-
}
210-
211-
alicePrivKey, err := crypto.BytesToPrivKey(alicePrivKeyBytes, crypto.Ed25519)
212-
if err != nil {
213-
return "", errors.Wrap(err, "bytes to priv key")
214-
}
215-
216-
signer, err := keyaccess.NewJWKKeyAccess(aliceDID, aliceKID, alicePrivKey)
205+
signer, err := keyaccess.NewJWKKeyAccess(aliceDID, aliceKID, aliceDIDPrivateKey)
217206
if err != nil {
218207
return "", errors.Wrap(err, "creating signer")
219208
}
@@ -270,30 +259,20 @@ type submissionJWTParams struct {
270259
SubmissionJWT string
271260
}
272261

273-
func CreateSubmission(params submissionParams, holderPrivateKey string) (string, error) {
262+
func CreateSubmission(params submissionParams, holderPrivateKey gocrypto.PrivateKey) (string, error) {
274263
logrus.Println("\n\nCreate our Submission:")
275264
submissionJSON, err := resolveTemplate(params, "presentation-submission-input.json")
276265
if err != nil {
277266
return "", err
278267
}
279268

280-
pkBytes, err := base58.Decode(holderPrivateKey)
281-
if err != nil {
282-
return "", errors.Wrap(err, "base58 decoding")
283-
}
284-
285-
pkCrypto, err := crypto.BytesToPrivKey(pkBytes, crypto.Ed25519)
286-
if err != nil {
287-
return "", errors.Wrap(err, "bytes to priv key")
288-
}
289-
290-
signer, err := keyaccess.NewJWKKeyAccess(params.HolderID, params.HolderKID, pkCrypto)
269+
signer, err := keyaccess.NewJWKKeyAccess(params.HolderID, params.HolderKID, holderPrivateKey)
291270
if err != nil {
292271
return "", errors.Wrap(err, "creating signer")
293272
}
294273

295274
var submission any
296-
if err := json.Unmarshal([]byte(submissionJSON), &submission); err != nil {
275+
if err = json.Unmarshal([]byte(submissionJSON), &submission); err != nil {
297276
return "", err
298277
}
299278

@@ -303,8 +282,7 @@ func CreateSubmission(params submissionParams, holderPrivateKey string) (string,
303282
return "", errors.Wrap(err, "signing json")
304283
}
305284

306-
submissionJSONWrapper, err := resolveTemplate(
307-
submissionJWTParams{SubmissionJWT: signed.String()},
285+
submissionJSONWrapper, err := resolveTemplate(submissionJWTParams{SubmissionJWT: signed.String()},
308286
"presentation-submission-input-jwt.json")
309287
if err != nil {
310288
return "", err

integration/didion_integration_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package integration
33
import (
44
"testing"
55

6+
"github.com/TBD54566975/ssi-sdk/crypto"
7+
didsdk "github.com/TBD54566975/ssi-sdk/did"
68
"github.com/stretchr/testify/assert"
79

810
"github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
@@ -29,29 +31,28 @@ func TestCreateIssuerDIDIONIntegration(t *testing.T) {
2931
SetValue(didIONContext, "issuerKID", issuerKID)
3032
}
3133

32-
func TestCreateAliceDIDIONIntegration(t *testing.T) {
34+
func TestCreateAliceDIDKeyForDIDIONIntegration(t *testing.T) {
3335
if testing.Short() {
3436
t.Skip("skipping integration test")
3537
}
3638

37-
didIONOutput, err := CreateDIDION()
39+
applicantPrivKey, applicantDIDKey, err := didsdk.GenerateDIDKey(crypto.Ed25519)
3840
assert.NoError(t, err)
39-
assert.NotEmpty(t, didIONOutput)
41+
assert.NotEmpty(t, applicantPrivKey)
42+
assert.NotEmpty(t, applicantDIDKey)
4043

41-
aliceDID, err := getJSONElement(didIONOutput, "$.did.id")
44+
applicantDID, err := applicantDIDKey.Expand()
4245
assert.NoError(t, err)
43-
assert.NotEmpty(t, aliceDID)
46+
assert.NotEmpty(t, applicantDID)
47+
48+
aliceDID := applicantDID.ID
49+
assert.Contains(t, aliceDID, "did:key")
4450
SetValue(didIONContext, "aliceDID", aliceDID)
4551

46-
aliceKID, err := getJSONElement(didIONOutput, "$.did.verificationMethod[0].id")
47-
assert.NoError(t, err)
52+
aliceKID := applicantDID.VerificationMethod[0].ID
4853
assert.NotEmpty(t, aliceKID)
4954
SetValue(didIONContext, "aliceKID", aliceKID)
50-
51-
aliceDIDPrivateKey, err := getJSONElement(didIONOutput, "$.privateKeyBase58")
52-
assert.NoError(t, err)
53-
assert.NotEmpty(t, aliceDIDPrivateKey)
54-
SetValue(didIONContext, "aliceDIDPrivateKey", aliceDIDPrivateKey)
55+
SetValue(didIONContext, "aliceDIDPrivateKey", applicantPrivKey)
5556
}
5657

5758
func TestDIDIONCreateSchemaIntegration(t *testing.T) {
@@ -168,7 +169,7 @@ func TestDIDIONSubmitAndReviewApplicationIntegration(t *testing.T) {
168169
credAppJWT, err := CreateCredentialApplicationJWT(credApplicationParams{
169170
DefinitionID: presentationDefinitionID.(string),
170171
ManifestID: manifestID.(string),
171-
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey.(string))
172+
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey)
172173
assert.NoError(t, err)
173174
assert.NotEmpty(t, credAppJWT)
174175

integration/didweb_integration_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package integration
33
import (
44
"testing"
55

6+
"github.com/TBD54566975/ssi-sdk/crypto"
7+
didsdk "github.com/TBD54566975/ssi-sdk/did"
68
"github.com/stretchr/testify/assert"
79

810
"github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
@@ -29,29 +31,28 @@ func TestCreateIssuerDIDWebIntegration(t *testing.T) {
2931
SetValue(didWebContext, "issuerKID", issuerKID)
3032
}
3133

32-
func TestCreateAliceDIDWebIntegration(t *testing.T) {
34+
func TestCreateAliceDIDKeyForDIDWebIntegration(t *testing.T) {
3335
if testing.Short() {
3436
t.Skip("skipping integration test")
3537
}
3638

37-
didWebOutput, err := CreateDIDWeb()
39+
applicantPrivKey, applicantDIDKey, err := didsdk.GenerateDIDKey(crypto.Ed25519)
3840
assert.NoError(t, err)
39-
assert.NotEmpty(t, didWebOutput)
41+
assert.NotEmpty(t, applicantPrivKey)
42+
assert.NotEmpty(t, applicantDIDKey)
4043

41-
aliceDID, err := getJSONElement(didWebOutput, "$.did.id")
44+
applicantDID, err := applicantDIDKey.Expand()
4245
assert.NoError(t, err)
43-
assert.Contains(t, aliceDID, "did:web")
46+
assert.NotEmpty(t, applicantDID)
47+
48+
aliceDID := applicantDID.ID
49+
assert.Contains(t, aliceDID, "did:key")
4450
SetValue(didWebContext, "aliceDID", aliceDID)
4551

46-
aliceKID, err := getJSONElement(didWebOutput, "$.did.verificationMethod[0].id")
47-
assert.NoError(t, err)
52+
aliceKID := applicantDID.VerificationMethod[0].ID
4853
assert.NotEmpty(t, aliceKID)
4954
SetValue(didWebContext, "aliceKID", aliceKID)
50-
51-
aliceDIDPrivateKey, err := getJSONElement(didWebOutput, "$.privateKeyBase58")
52-
assert.NoError(t, err)
53-
assert.NotEmpty(t, aliceDID)
54-
SetValue(didWebContext, "aliceDIDPrivateKey", aliceDIDPrivateKey)
55+
SetValue(didWebContext, "aliceDIDPrivateKey", applicantPrivKey)
5556
}
5657

5758
func TestDIDWebCreateSchemaIntegration(t *testing.T) {
@@ -167,7 +168,7 @@ func TestDIDWebSubmitAndReviewApplicationIntegration(t *testing.T) {
167168
credAppJWT, err := CreateCredentialApplicationJWT(credApplicationParams{
168169
DefinitionID: presentationDefinitionID.(string),
169170
ManifestID: manifestID.(string),
170-
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey.(string))
171+
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey)
171172
assert.NoError(t, err)
172173
assert.NotEmpty(t, credAppJWT)
173174

integration/presentation_exchange_integration_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package integration
33
import (
44
"testing"
55

6+
"github.com/TBD54566975/ssi-sdk/crypto"
7+
didsdk "github.com/TBD54566975/ssi-sdk/did"
68
"github.com/google/uuid"
79
"github.com/stretchr/testify/assert"
810

@@ -29,29 +31,27 @@ func TestCreateParticipants(t *testing.T) {
2931
assert.NotEmpty(t, issuerKID)
3032
SetValue(presentationExchangeContext, "issuerKID", issuerKID)
3133

32-
holderOutput, err := CreateDIDKey()
34+
holderPrivateKey, holderDIDKey, err := didsdk.GenerateDIDKey(crypto.Ed25519)
3335
assert.NoError(t, err)
36+
assert.NotEmpty(t, holderPrivateKey)
37+
assert.NotEmpty(t, holderDIDKey)
3438

35-
holderDID, err := getJSONElement(holderOutput, "$.did.id")
39+
holderDID, err := holderDIDKey.Expand()
3640
assert.NoError(t, err)
37-
assert.Contains(t, holderDID, "did:key")
38-
SetValue(presentationExchangeContext, "holderDID", holderDID)
41+
assert.NotEmpty(t, holderDID)
42+
SetValue(presentationExchangeContext, "holderDID", holderDID.ID)
3943

40-
holderKID, err := getJSONElement(holderOutput, "$.did.verificationMethod[0].id")
41-
assert.NoError(t, err)
44+
holderKID := holderDID.VerificationMethod[0].ID
4245
assert.NotEmpty(t, holderKID)
4346
SetValue(presentationExchangeContext, "holderKID", holderKID)
44-
45-
holderPrivateKey, err := getJSONElement(holderOutput, "$.privateKeyBase58")
46-
assert.NoError(t, err)
4747
SetValue(presentationExchangeContext, "holderPrivateKey", holderPrivateKey)
4848

4949
verifierOutput, err := CreateDIDKey()
5050
assert.NoError(t, err)
5151

5252
verifierDID, err := getJSONElement(verifierOutput, "$.did.id")
5353
assert.NoError(t, err)
54-
assert.Contains(t, holderDID, "did:key")
54+
assert.Contains(t, verifierDID, "did:key")
5555
SetValue(presentationExchangeContext, "verifierDID", verifierDID)
5656

5757
verifierKID, err := getJSONElement(verifierOutput, "$.did.verificationMethod[0].id")
@@ -121,7 +121,7 @@ func TestSubmissionFlow(t *testing.T) {
121121
DefinitionID: definitionID.(string),
122122
CredentialJWT: credentialJWT,
123123
SubmissionID: uuid.NewString(),
124-
}, holderPrivateKey.(string))
124+
}, holderPrivateKey)
125125
assert.NoError(t, err)
126126

127127
cancelOpID, err := getJSONElement(toBeCancelledOp, "$.id")
@@ -138,7 +138,7 @@ func TestSubmissionFlow(t *testing.T) {
138138
DefinitionID: definitionID.(string),
139139
CredentialJWT: credentialJWT,
140140
SubmissionID: uuid.NewString(),
141-
}, holderPrivateKey.(string))
141+
}, holderPrivateKey)
142142
assert.NoError(t, err)
143143

144144
opID, err := getJSONElement(submissionOpOutput, "$.id")

integration/steelthread_integration_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"testing"
55

66
credsdk "github.com/TBD54566975/ssi-sdk/credential"
7+
"github.com/TBD54566975/ssi-sdk/crypto"
8+
didsdk "github.com/TBD54566975/ssi-sdk/did"
79
"github.com/stretchr/testify/assert"
810

911
"github.com/tbd54566975/ssi-service/pkg/service/operation/storage"
@@ -49,24 +51,23 @@ func TestCreateAliceDIDKeyIntegration(t *testing.T) {
4951
t.Skip("skipping integration test")
5052
}
5153

52-
didKeyOutput, err := CreateDIDKey()
54+
applicantPrivKey, applicantDIDKey, err := didsdk.GenerateDIDKey(crypto.Ed25519)
5355
assert.NoError(t, err)
54-
assert.NotEmpty(t, didKeyOutput)
56+
assert.NotEmpty(t, applicantPrivKey)
57+
assert.NotEmpty(t, applicantDIDKey)
5558

56-
aliceDID, err := getJSONElement(didKeyOutput, "$.did.id")
59+
applicantDID, err := applicantDIDKey.Expand()
5760
assert.NoError(t, err)
61+
assert.NotEmpty(t, applicantDID)
62+
63+
aliceDID := applicantDID.ID
5864
assert.Contains(t, aliceDID, "did:key")
5965
SetValue(steelThreadContext, "aliceDID", aliceDID)
6066

61-
aliceKID, err := getJSONElement(didKeyOutput, "$.did.verificationMethod[0].id")
62-
assert.NoError(t, err)
67+
aliceKID := applicantDID.VerificationMethod[0].ID
6368
assert.NotEmpty(t, aliceKID)
6469
SetValue(steelThreadContext, "aliceKID", aliceKID)
65-
66-
aliceDIDPrivateKey, err := getJSONElement(didKeyOutput, "$.privateKeyBase58")
67-
assert.NoError(t, err)
68-
assert.NotEmpty(t, aliceDID)
69-
SetValue(steelThreadContext, "aliceDIDPrivateKey", aliceDIDPrivateKey)
70+
SetValue(steelThreadContext, "aliceDIDPrivateKey", applicantPrivKey)
7071
}
7172

7273
func TestCreateSchemaIntegration(t *testing.T) {
@@ -230,7 +231,7 @@ func TestSubmitApplicationWithIssuanceTemplateIntegration(t *testing.T) {
230231
credAppJWT, err := CreateCredentialApplicationJWT(credApplicationParams{
231232
DefinitionID: presentationDefinitionID.(string),
232233
ManifestID: manifestID.(string),
233-
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey.(string))
234+
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey)
234235
assert.NoError(t, err)
235236
assert.NotEmpty(t, credAppJWT)
236237

@@ -287,7 +288,7 @@ func TestSubmitAndReviewApplicationIntegration(t *testing.T) {
287288
credAppJWT, err := CreateCredentialApplicationJWT(credApplicationParams{
288289
DefinitionID: presentationDefinitionID.(string),
289290
ManifestID: manifestID.(string),
290-
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey.(string))
291+
}, credentialJWT.(string), aliceDID.(string), aliceKID.(string), aliceDIDPrivateKey)
291292
assert.NoError(t, err)
292293
assert.NotEmpty(t, credAppJWT)
293294

pkg/server/router/did.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,16 @@ type CreateDIDByMethodRequest struct {
6868
}
6969

7070
type CreateDIDByMethodResponse struct {
71-
DID didsdk.Document `json:"did,omitempty"`
72-
PrivateKeyBase58 string `json:"privateKeyBase58,omitempty"`
73-
KeyType crypto.KeyType `json:"keyType,omitempty"`
71+
DID didsdk.Document `json:"did,omitempty"`
7472
}
7573

7674
// CreateDIDByMethod godoc
7775
//
7876
// @Summary Create DID Document
79-
// @Description Creates a DID document with the given method. The document created is stored internally and can be
80-
// @Description retrieved using the GetOperation. Method dependent registration (for example, DID web registration)
81-
// @Description is left up to the clients of this API.
77+
// @Description Creates a fully custodial DID document with the given method. The document created is stored internally
78+
// @Description and can be retrieved using the GetOperation. Method dependent registration (for example, DID web
79+
// @Description registration) is left up to the clients of this API. The private key(s) created by the method are stored
80+
// @Description internally never leave the service boundary.
8281
// @Tags DecentralizedIdentityAPI
8382
// @Accept json
8483
// @Produce json
@@ -124,12 +123,7 @@ func (dr DIDRouter) CreateDIDByMethod(ctx context.Context, w http.ResponseWriter
124123
return framework.NewRequestError(errors.Wrap(err, errMsg), http.StatusInternalServerError)
125124
}
126125

127-
resp := CreateDIDByMethodResponse{
128-
DID: createDIDResponse.DID,
129-
PrivateKeyBase58: createDIDResponse.PrivateKeyBase58,
130-
KeyType: createDIDResponse.KeyType,
131-
}
132-
126+
resp := CreateDIDByMethodResponse{DID: createDIDResponse.DID}
133127
return framework.Respond(ctx, w, resp, http.StatusCreated)
134128
}
135129

0 commit comments

Comments
 (0)