Skip to content

Commit d01db98

Browse files
committed
refactor(keys): use interface for keys used in proxy
1 parent cfa146b commit d01db98

File tree

5 files changed

+98
-13
lines changed

5 files changed

+98
-13
lines changed

dataverse/client.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dataverse
33
import (
44
"context"
55
"fmt"
6+
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
67

78
cgschema "github.com/axone-protocol/axone-contract-schema/go/cognitarium-schema/v5"
89
dvschema "github.com/axone-protocol/axone-contract-schema/go/dataverse-schema/v5"
@@ -30,6 +31,8 @@ type Client interface {
3031
// ```
3132
// The function returns true if Result is 'permitted', false otherwise.
3233
AskGovTellAction(context.Context, string, string, string) (bool, error)
34+
35+
SubmitClaims(ctx context.Context, credential *verifiable.Credential)
3336
}
3437

3538
type LawStoneFactory func(string) (lsschema.QueryClient, error)
@@ -40,6 +43,10 @@ type client struct {
4043
lawStoneFactory LawStoneFactory
4144
}
4245

46+
func (c *client) SubmitClaims(_ context.Context, _ *verifiable.Credential) {
47+
panic("implement me")
48+
}
49+
4350
func NewClient(ctx context.Context,
4451
grpcAddr, contractAddr string,
4552
opts ...grpc.DialOption,

keys/keyring.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package keys
2+
3+
import (
4+
"github.com/cosmos/cosmos-sdk/crypto/types"
5+
)
6+
7+
type Keyring interface {
8+
Sign(msg []byte) ([]byte, error)
9+
PubKey() types.PubKey
10+
Alg() string
11+
DID() string
12+
}

keys/key.go keys/mnemonic.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package keys
33
import (
44
"github.com/axone-protocol/axoned/v9/x/logic/util"
55
"github.com/cosmos/cosmos-sdk/crypto/hd"
6-
"github.com/cosmos/cosmos-sdk/crypto/keyring"
6+
k "github.com/cosmos/cosmos-sdk/crypto/keyring"
77
"github.com/cosmos/cosmos-sdk/crypto/types"
88
sdk "github.com/cosmos/cosmos-sdk/types"
99
)
1010

11+
var _ Keyring = &Key{}
12+
1113
type Key struct {
1214
privKey types.PrivKey
13-
DID string
15+
did string
1416
DIDKeyID string
1517
Addr string
1618
}
@@ -20,7 +22,6 @@ func NewKeyFromMnemonic(mnemonic string) (*Key, error) {
2022
if err != nil {
2123
return nil, err
2224
}
23-
2425
return NewKeyFromPrivKey(pkey)
2526
}
2627

@@ -35,9 +36,12 @@ func NewKeyFromPrivKey(pkey types.PrivKey) (*Key, error) {
3536
return nil, err
3637
}
3738

39+
// TODO: Maybe make this configurable...
40+
sdk.GetConfig().SetBech32PrefixForAccount("axone", "axonepub")
41+
3842
return &Key{
3943
privKey: pkey,
40-
DID: did,
44+
did: did,
4145
DIDKeyID: didKeyID,
4246
Addr: sdk.AccAddress(pkey.PubKey().Address()).String(),
4347
}, nil
@@ -52,11 +56,15 @@ func (k *Key) Sign(msg []byte) ([]byte, error) {
5256
}
5357

5458
func (k *Key) Alg() string {
55-
return "unknown"
59+
return "secp256k1"
60+
}
61+
62+
func (k *Key) DID() string {
63+
return k.did
5664
}
5765

5866
func parseMnemonic(mnemonic string) (types.PrivKey, error) {
59-
algo, err := keyring.NewSigningAlgoFromString("secp256k1", keyring.SigningAlgoList{hd.Secp256k1})
67+
algo, err := k.NewSigningAlgoFromString("secp256k1", k.SigningAlgoList{hd.Secp256k1})
6068
if err != nil {
6169
return nil, err
6270
}

keys/mnemonic_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package keys
2+
3+
import (
4+
"fmt"
5+
. "github.com/smartystreets/goconvey/convey"
6+
"testing"
7+
)
8+
9+
func TestNewKeyFromMnemonic(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
mnemonic string
13+
wantDID string
14+
wantDIDKey string
15+
wantAddr string
16+
wantErr error
17+
}{
18+
{
19+
name: "valid mnemonic",
20+
mnemonic: "code ceiling reduce repeat unfold intact cloud marriage nut remove illegal eternal pool frame mask rate buzz vintage pulp suggest loan faint snake spoon",
21+
wantDID: "did:key:zQ3shVMdXcC6eGDC1UGHDzzvtrZVwmqHtYaAW6BDKqPNR569S",
22+
wantDIDKey: "did:key:zQ3shVMdXcC6eGDC1UGHDzzvtrZVwmqHtYaAW6BDKqPNR569S#zQ3shVMdXcC6eGDC1UGHDzzvtrZVwmqHtYaAW6BDKqPNR569S",
23+
wantAddr: "axone14u8n76zahep9xkfr9gc3zxv5c7rf3x8wx3fdjl",
24+
wantErr: nil,
25+
},
26+
{
27+
name: "invalid mnemonic",
28+
mnemonic: "invalid",
29+
wantErr: fmt.Errorf("Invalid mnemonic"),
30+
},
31+
}
32+
33+
for _, test := range tests {
34+
t.Run(test.name, func(t *testing.T) {
35+
Convey("Given a mnemonic", t, func() {
36+
Convey("When NewKeyFromMnemonic is called", func() {
37+
key, err := NewKeyFromMnemonic(test.mnemonic)
38+
Convey("Then the key should be created", func() {
39+
if test.wantErr != nil {
40+
So(err, ShouldNotBeNil)
41+
So(err.Error(), ShouldEqual, test.wantErr.Error())
42+
So(key, ShouldBeNil)
43+
} else {
44+
So(err, ShouldBeNil)
45+
So(key, ShouldNotBeNil)
46+
So(key.privKey, ShouldNotBeNil)
47+
So(key.DID(), ShouldEqual, test.wantDID)
48+
So(key.DIDKeyID, ShouldEqual, test.wantDIDKey)
49+
So(key.Addr, ShouldEqual, test.wantAddr)
50+
So(key.PubKey(), ShouldNotBeNil)
51+
}
52+
})
53+
})
54+
})
55+
})
56+
}
57+
}

provider/storage/proxy.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
)
2121

2222
type Proxy struct {
23-
key *keys.Key
23+
key keys.Keyring
2424
baseURL string
2525
dvClient dataverse.Client
2626
authProxy auth.Proxy
@@ -34,14 +34,14 @@ type Proxy struct {
3434

3535
func NewProxy(
3636
ctx context.Context,
37-
key *keys.Key,
37+
key keys.Keyring,
3838
baseURL string,
3939
dvClient dataverse.Client,
4040
documentLoader ld.DocumentLoader,
4141
readFn func(context.Context, string) (io.Reader, error),
4242
storeFn func(context.Context, string, io.Reader) error,
4343
) (*Proxy, error) {
44-
gov, err := dvClient.GetResourceGovAddr(ctx, key.DID)
44+
gov, err := dvClient.GetResourceGovAddr(ctx, key.DID())
4545
if err != nil {
4646
return nil, err
4747
}
@@ -54,7 +54,7 @@ func NewProxy(
5454
key: key,
5555
baseURL: baseURL,
5656
dvClient: dvClient,
57-
authProxy: auth.NewProxy(gov, key.DID, dvClient, credential.NewAuthParser(documentLoader)),
57+
authProxy: auth.NewProxy(gov, key.DID(), dvClient, credential.NewAuthParser(documentLoader)),
5858
vcParser: credential.NewDefaultParser(documentLoader),
5959
readFn: readFn,
6060
storeFn: storeFn,
@@ -75,7 +75,8 @@ func (p *Proxy) Read(ctx context.Context, id *auth.Identity, resourceID string)
7575
return nil, err
7676
}
7777

78-
ok, err := p.dvClient.AskGovTellAction(ctx, govAddr, p.key.DID, readAction)
78+
ok, err := p.dvClient.AskGovTellAction(ctx, govAddr, p.key.DID(), readAction)
79+
7980
if err != nil {
8081
return nil, err
8182
}
@@ -97,9 +98,9 @@ func (p *Proxy) Store(ctx context.Context, id *auth.Identity, resourceID string,
9798
}
9899

99100
vc, err := credential.New(
100-
template.NewPublication(resourceID, p.baseURL+resourceID, p.key.DID),
101+
template.NewPublication(resourceID, p.baseURL+resourceID, p.key.DID()),
101102
credential.WithParser(p.vcParser),
102-
credential.WithSigner(p.key, p.key.DID),
103+
credential.WithSigner(p.key, p.key.DID()),
103104
).Generate()
104105
if err != nil {
105106
return nil, err

0 commit comments

Comments
 (0)