-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dot/rpc) implement author_hasSessionKeys
RPC call
#1704
Changes from all commits
fdac471
888de1f
157d3fd
904dbde
fc77c6d
feefdb2
6fad347
3b41f90
ff811b1
04eb2e8
22b1b80
8eb3b5e
6d20c6d
09b70bc
1486165
c3d6f55
6e32d71
d19b6ac
bab92a0
edd2f39
8aa968c
da2fc08
52647bd
6f28204
09408fd
542f853
fecd636
dc4b83d
83617bb
a7a9846
4f51ed8
eca7401
b1968fa
215f868
aee10f1
f6d4640
85c8a81
70439a5
e0d9f6c
444b03a
6dfdf15
fe70b02
8a65889
7fc851c
3d6fa1c
145e6d0
1f84625
b433e80
878abed
f167333
8c833d7
1079926
38f5229
d867db8
05cb9f4
32ab688
60690bf
d5a75ac
10e598b
33e51b0
812309e
d1e5621
fb91dbd
bd65835
338f0b8
9c659a5
d666381
d9943a7
74e520e
16ad5dd
550a091
d741597
a759f8c
2734ce7
38bb583
0b47095
55dd726
9890ee8
a33064f
716d6b3
2b20c8d
68ebfc4
204f1d3
a48b211
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,13 +17,14 @@ | |
package modules | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
"net/http" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/keystore" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
|
||
log "github.com/ChainSafe/log15" | ||
) | ||
|
@@ -35,8 +36,17 @@ type AuthorModule struct { | |
txStateAPI TransactionStateAPI | ||
} | ||
|
||
// HasSessionKeyRequest is used to receive the rpc data | ||
type HasSessionKeyRequest struct { | ||
PublicKeys string | ||
} | ||
|
||
// KeyInsertRequest is used as model for the JSON | ||
type KeyInsertRequest []string | ||
type KeyInsertRequest struct { | ||
Type string | ||
Seed string | ||
PublicKey string | ||
} | ||
|
||
// Extrinsic represents a hex-encoded extrinsic | ||
type Extrinsic struct { | ||
|
@@ -64,6 +74,18 @@ type RemoveExtrinsicsResponse []common.Hash | |
// KeyRotateResponse is a byte array used to rotate | ||
type KeyRotateResponse []byte | ||
|
||
// HasSessionKeyResponse is the response to the RPC call author_hasSessionKeys | ||
type HasSessionKeyResponse bool | ||
|
||
// KeyTypeID represents the key type of a session key | ||
type keyTypeID [4]uint8 | ||
|
||
// DecodedKey is the representation of a scaled decoded public key | ||
type decodedKey struct { | ||
Data []uint8 | ||
Type keyTypeID | ||
} | ||
|
||
// ExtrinsicStatus holds the actual valid statuses | ||
type ExtrinsicStatus struct { | ||
IsFuture bool | ||
|
@@ -94,27 +116,66 @@ func NewAuthorModule(logger log.Logger, coreAPI CoreAPI, txStateAPI TransactionS | |
} | ||
} | ||
|
||
// InsertKey inserts a key into the keystore | ||
func (am *AuthorModule) InsertKey(r *http.Request, req *KeyInsertRequest, res *KeyInsertResponse) error { | ||
keyReq := *req | ||
// HasSessionKeys checks if the keystore has private keys for the given session public keys. | ||
func (am *AuthorModule) HasSessionKeys(r *http.Request, req *HasSessionKeyRequest, res *HasSessionKeyResponse) error { | ||
pubKeysBytes, err := common.HexToBytes(req.PublicKeys) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pkeys, err := scale.Marshal(pubKeysBytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pkDec, err := common.HexToBytes(keyReq[1]) | ||
data, err := am.coreAPI.DecodeSessionKeys(pkeys) | ||
if err != nil { | ||
*res = false | ||
return err | ||
} | ||
|
||
privateKey, err := keystore.DecodePrivateKey(pkDec, keystore.DetermineKeyType(keyReq[0])) | ||
var decodedKeys *[]decodedKey | ||
err = scale.Unmarshal(data, &decodedKeys) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if decodedKeys == nil || len(*decodedKeys) < 1 { | ||
*res = false | ||
return nil | ||
} | ||
|
||
for _, key := range *decodedKeys { | ||
encType := keystore.Name(key.Type[:]) | ||
ok, err := am.coreAPI.HasKey(common.BytesToHex(key.Data), string(encType)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we also need to return if a key is not present? Can't we return nil for that specific key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the spec says: |
||
|
||
if err != nil || !ok { | ||
*res = false | ||
return err | ||
} | ||
} | ||
|
||
*res = true | ||
return nil | ||
} | ||
|
||
// InsertKey inserts a key into the keystore | ||
func (am *AuthorModule) InsertKey(r *http.Request, req *KeyInsertRequest, res *KeyInsertResponse) error { | ||
keyReq := *req | ||
|
||
keyBytes, err := common.HexToBytes(req.Seed) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
keyPair, err := keystore.PrivateKeyToKeypair(privateKey) | ||
keyPair, err := keystore.DecodeKeyPairFromHex(keyBytes, keystore.DetermineKeyType(keyReq.Type)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !reflect.DeepEqual(keyPair.Public().Hex(), keyReq[2]) { | ||
return fmt.Errorf("generated public key does not equal provide public key") | ||
//strings.EqualFold compare using case-insensitivity. | ||
if !strings.EqualFold(keyPair.Public().Hex(), keyReq.PublicKey) { | ||
return errors.New("generated public key does not equal provide public key") | ||
} | ||
|
||
am.coreAPI.InsertKey(keyPair) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this pointer to slice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's an optional vector.