Skip to content

Commit

Permalink
Merge pull request #105 from kaleido-io/eth-query
Browse files Browse the repository at this point in the history
Allow blank signing key for queries on Ethereum
  • Loading branch information
peterbroadhurst committed May 24, 2024
2 parents 226c765 + a98fe31 commit 1fab1d9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 53 deletions.
13 changes: 10 additions & 3 deletions internal/blockchain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,13 @@ func formatEthAddress(ctx context.Context, key string) (string, error) {
}

func (e *Ethereum) ResolveSigningKey(ctx context.Context, key string, intent blockchain.ResolveKeyIntent) (resolved string, err error) {
// Key may be unset for query intent only
if key == "" {
if intent == blockchain.ResolveKeyIntentQuery {
return "", nil
}
return "", i18n.NewError(ctx, coremsgs.MsgNodeMissingBlockchainKey)
}
if !e.addressResolveAlways {
// If there's no address resolver plugin, or addressResolveAlways is false,
// we check if it's already an ethereum address - in which case we can just return it.
Expand Down Expand Up @@ -649,7 +656,7 @@ func (e *Ethereum) queryContractMethod(ctx context.Context, address, signingKey
return res, nil
}

func (e *Ethereum) buildBatchPinInput(ctx context.Context, version int, namespace string, batch *blockchain.BatchPin) (*abi.Entry, []interface{}) {
func (e *Ethereum) buildBatchPinInput(version int, namespace string, batch *blockchain.BatchPin) (*abi.Entry, []interface{}) {
ethHashes := make([]string, len(batch.Contexts))
for i, v := range batch.Contexts {
ethHashes[i] = ethHexFormatB32(v)
Expand Down Expand Up @@ -694,7 +701,7 @@ func (e *Ethereum) SubmitBatchPin(ctx context.Context, nsOpID, networkNamespace,
return err
}

method, input := e.buildBatchPinInput(ctx, version, networkNamespace, batch)
method, input := e.buildBatchPinInput(version, networkNamespace, batch)

var emptyErrors []*abi.Entry
_, err = e.invokeContractMethod(ctx, ethLocation.Address, signingKey, method, nsOpID, input, emptyErrors, nil)
Expand Down Expand Up @@ -810,7 +817,7 @@ func (e *Ethereum) InvokeContract(ctx context.Context, nsOpID string, signingKey
if batch != nil {
err := e.checkDataSupport(ctx, methodInfo.methodABI)
if err == nil {
method, batchPin := e.buildBatchPinInput(ctx, 2, "", batch)
method, batchPin := e.buildBatchPinInput(2, "", batch)
encoded, err := method.Inputs.EncodeABIDataValuesCtx(ctx, batchPin)
if err == nil {
orderedInput[len(orderedInput)-1] = hex.EncodeToString(encoded)
Expand Down
49 changes: 28 additions & 21 deletions internal/blockchain/ethereum/ethereum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func newTestStreamManager(client *resty.Client) *streamManager {
return newStreamManager(client, cache.NewUmanagedCache(context.Background(), 100, 5*time.Minute), defaultBatchSize, defaultBatchTimeout)
}

func mockNetworkVersion(t *testing.T, version int) func(req *http.Request) (*http.Response, error) {
func mockNetworkVersion(version int) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
readBody, _ := req.GetBody()
var body map[string]interface{}
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestEthCacheInitFail(t *testing.T) {
"registeredAs": "firefly",
}),
)
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 1))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(1))

e, cancel := newTestEthereum()
resetConf(e)
Expand Down Expand Up @@ -617,7 +617,7 @@ func TestInitOldInstancePathContracts(t *testing.T) {
"registeredAs": "firefly",
}),
)
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 1))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(1))

resetConf(e)
utEthconnectConf.Set(ffresty.HTTPConfigURL, "http://localhost:12345")
Expand Down Expand Up @@ -654,7 +654,7 @@ func TestInitOldInstancePathInstances(t *testing.T) {
assert.Equal(t, "es12345", body["stream"])
return httpmock.NewJsonResponderOrPanic(200, subscription{ID: "sub12345"})(req)
})
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 1))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(1))

resetConf(e)
utEthconnectConf.Set(ffresty.HTTPConfigURL, "http://localhost:12345")
Expand Down Expand Up @@ -848,7 +848,7 @@ func TestInitAllExistingStreams(t *testing.T) {
}))
httpmock.RegisterResponder("PATCH", "http://localhost:12345/eventstreams/es12345",
httpmock.NewJsonResponderOrPanic(200, &eventStream{ID: "es12345", Name: "topic1"}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 2))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(2))
httpmock.RegisterResponder("POST", "http://localhost:12345/subscriptions",
httpmock.NewJsonResponderOrPanic(200, subscription{}))

Expand Down Expand Up @@ -894,7 +894,7 @@ func TestInitAllExistingStreamsV1(t *testing.T) {
}))
httpmock.RegisterResponder("PATCH", "http://localhost:12345/eventstreams/es12345",
httpmock.NewJsonResponderOrPanic(200, &eventStream{ID: "es12345", Name: "topic1"}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 1))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(1))
httpmock.RegisterResponder("POST", "http://localhost:12345/subscriptions",
httpmock.NewJsonResponderOrPanic(200, subscription{}))

Expand Down Expand Up @@ -940,7 +940,7 @@ func TestInitAllExistingStreamsOld(t *testing.T) {
}))
httpmock.RegisterResponder("PATCH", "http://localhost:12345/eventstreams/es12345",
httpmock.NewJsonResponderOrPanic(200, &eventStream{ID: "es12345", Name: "topic1"}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 1))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(1))
httpmock.RegisterResponder("POST", "http://localhost:12345/subscriptions",
httpmock.NewJsonResponderOrPanic(200, subscription{}))

Expand Down Expand Up @@ -986,7 +986,7 @@ func TestInitAllExistingStreamsInvalidName(t *testing.T) {
}))
httpmock.RegisterResponder("PATCH", "http://localhost:12345/eventstreams/es12345",
httpmock.NewJsonResponderOrPanic(200, &eventStream{ID: "es12345", Name: "topic1"}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 2))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(2))
httpmock.RegisterResponder("POST", "http://localhost:12345/subscriptions",
httpmock.NewJsonResponderOrPanic(200, subscription{}))

Expand Down Expand Up @@ -1093,7 +1093,7 @@ func TestSubmitBatchPinOK(t *testing.T) {

httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 2)(req)
res, err := mockNetworkVersion(2)(req)
if res != nil || err != nil {
return res, err
}
Expand Down Expand Up @@ -1137,7 +1137,7 @@ func TestSubmitBatchPinV1(t *testing.T) {

httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 1)(req)
res, err := mockNetworkVersion(1)(req)
if res != nil || err != nil {
return res, err
}
Expand Down Expand Up @@ -1207,7 +1207,7 @@ func TestSubmitBatchEmptyPayloadRef(t *testing.T) {

httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 2)(req)
res, err := mockNetworkVersion(2)(req)
if res != nil || err != nil {
return res, err
}
Expand Down Expand Up @@ -1288,7 +1288,7 @@ func TestSubmitBatchPinFail(t *testing.T) {

httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 2)(req)
res, err := mockNetworkVersion(2)(req)
if res != nil || err != nil {
return res, err
}
Expand Down Expand Up @@ -1326,7 +1326,7 @@ func TestSubmitBatchPinError(t *testing.T) {

httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 2)(req)
res, err := mockNetworkVersion(2)(req)
if res != nil || err != nil {
return res, err
}
Expand All @@ -1344,10 +1344,17 @@ func TestVerifyEthAddress(t *testing.T) {
e, cancel := newTestEthereum()
defer cancel()

_, err := e.ResolveSigningKey(context.Background(), "0x12345", blockchain.ResolveKeyIntentSign)
_, err := e.ResolveSigningKey(context.Background(), "", blockchain.ResolveKeyIntentSign)
assert.Regexp(t, "FF10354", err)

key, err := e.ResolveSigningKey(context.Background(), "", blockchain.ResolveKeyIntentQuery)
assert.NoError(t, err)
assert.Empty(t, key)

_, err = e.ResolveSigningKey(context.Background(), "0x12345", blockchain.ResolveKeyIntentSign)
assert.Regexp(t, "FF10141", err)

key, err := e.ResolveSigningKey(context.Background(), "0x2a7c9D5248681CE6c393117E641aD037F5C079F6", blockchain.ResolveKeyIntentSign)
key, err = e.ResolveSigningKey(context.Background(), "0x2a7c9D5248681CE6c393117E641aD037F5C079F6", blockchain.ResolveKeyIntentSign)
assert.NoError(t, err)
assert.Equal(t, "0x2a7c9d5248681ce6c393117e641ad037f5c079f6", key)

Expand Down Expand Up @@ -3472,7 +3479,7 @@ func TestSubmitNetworkAction(t *testing.T) {
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 2)(req)
res, err := mockNetworkVersion(2)(req)
if res != nil || err != nil {
return res, err
}
Expand Down Expand Up @@ -3501,7 +3508,7 @@ func TestSubmitNetworkActionV1(t *testing.T) {
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", `http://localhost:12345/`,
func(req *http.Request) (*http.Response, error) {
res, err := mockNetworkVersion(t, 1)(req)
res, err := mockNetworkVersion(1)(req)
if res != nil || err != nil {
return res, err
}
Expand Down Expand Up @@ -3946,7 +3953,7 @@ func TestAddAndRemoveFireflySubscription(t *testing.T) {
httpmock.NewJsonResponderOrPanic(200, subscription{
ID: "sub1",
}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 2))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(2))

utEthconnectConf.Set(ffresty.HTTPConfigURL, "http://localhost:12345")
utEthconnectConf.Set(ffresty.HTTPCustomClient, mockedClient)
Expand Down Expand Up @@ -3999,7 +4006,7 @@ func TestAddFireflySubscriptionV1(t *testing.T) {
httpmock.NewJsonResponderOrPanic(200, subscription{
ID: "sub1",
}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 1))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(1))

utEthconnectConf.Set(ffresty.HTTPConfigURL, "http://localhost:12345")
utEthconnectConf.Set(ffresty.HTTPCustomClient, mockedClient)
Expand Down Expand Up @@ -4041,7 +4048,7 @@ func TestAddFireflySubscriptionQuerySubsFail(t *testing.T) {
httpmock.NewStringResponder(500, `pop`))
httpmock.RegisterResponder("POST", "http://localhost:12345/subscriptions",
httpmock.NewJsonResponderOrPanic(200, subscription{}))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 2))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(2))

utEthconnectConf.Set(ffresty.HTTPConfigURL, "http://localhost:12345")
utEthconnectConf.Set(ffresty.HTTPCustomClient, mockedClient)
Expand Down Expand Up @@ -4124,7 +4131,7 @@ func TestAddFireflySubscriptionGetVersionError(t *testing.T) {
httpmock.NewJsonResponderOrPanic(200, []subscription{}))
httpmock.RegisterResponder("POST", "http://localhost:12345/subscriptions",
httpmock.NewJsonResponderOrPanic(500, `pop`))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(t, 2))
httpmock.RegisterResponder("POST", "http://localhost:12345/", mockNetworkVersion(2))

utEthconnectConf.Set(ffresty.HTTPConfigURL, "http://localhost:12345")
utEthconnectConf.Set(ffresty.HTTPCustomClient, mockedClient)
Expand Down
11 changes: 8 additions & 3 deletions internal/blockchain/fabric/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,11 @@ func (f *Fabric) ResolveSigningKey(ctx context.Context, signingKeyInput string,
// currently pluggable to external identity resolution systems (as is the case for
// ethereum blockchain connectors).

// Key is always required
if signingKeyInput == "" {
return "", i18n.NewError(ctx, coremsgs.MsgNodeMissingBlockchainKey)
}

// we expand the short user name into the fully qualified onchain identity:
// mspid::x509::{ecert DN}::{CA DN} return signingKeyInput, nil
if !fullIdentityPattern.MatchString(signingKeyInput) {
Expand Down Expand Up @@ -652,7 +657,7 @@ func hexFormatB32(b *fftypes.Bytes32) string {
return "0x" + hex.EncodeToString(b[0:32])
}

func (f *Fabric) buildBatchPinInput(ctx context.Context, version int, namespace string, batch *blockchain.BatchPin) (prefixItems []*PrefixItem, pinInput map[string]interface{}) {
func (f *Fabric) buildBatchPinInput(version int, namespace string, batch *blockchain.BatchPin) (prefixItems []*PrefixItem, pinInput map[string]interface{}) {
hashes := make([]string, len(batch.Contexts))
for i, v := range batch.Contexts {
hashes[i] = hexFormatB32(v)
Expand Down Expand Up @@ -693,7 +698,7 @@ func (f *Fabric) SubmitBatchPin(ctx context.Context, nsOpID, networkNamespace, s
return err
}

prefixItems, pinInput := f.buildBatchPinInput(ctx, version, networkNamespace, batch)
prefixItems, pinInput := f.buildBatchPinInput(version, networkNamespace, batch)

input, _ := jsonEncodeInput(pinInput)
_, err = f.invokeContractMethod(ctx, fabricOnChainLocation.Channel, fabricOnChainLocation.Chaincode, batchPinMethodName, signingKey, nsOpID, prefixItems, input, nil)
Expand Down Expand Up @@ -807,7 +812,7 @@ func (f *Fabric) InvokeContract(ctx context.Context, nsOpID string, signingKey s
}

if batch != nil {
_, batchPin := f.buildBatchPinInput(ctx, 2, "", batch)
_, batchPin := f.buildBatchPinInput(2, "", batch)
if input == nil {
input = make(map[string]interface{})
}
Expand Down
Loading

0 comments on commit 1fab1d9

Please sign in to comment.