Skip to content

Commit

Permalink
Merge pull request #184 from kaleido-io/fabric-plugin
Browse files Browse the repository at this point in the history
Fabric plugin
  • Loading branch information
jimthematrix authored Oct 14, 2021
2 parents 83507dc + e2c747d commit 3d9f3ad
Show file tree
Hide file tree
Showing 10 changed files with 1,535 additions and 10 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ RUN mkdir /firefly/frontend \
ADD . .
RUN make build

FROM golang:1.16-alpine3.13 AS fabric-builder
RUN apk add libc6-compat
WORKDIR /firefly/smart_contracts/fabric/firefly-go
ADD smart_contracts/fabric/firefly-go .
RUN GO111MODULE=on go mod vendor
WORKDIR /tmp/fabric
RUN wget https://github.com/hyperledger/fabric/releases/download/v2.3.2/hyperledger-fabric-linux-amd64-2.3.2.tar.gz
RUN tar -zxf hyperledger-fabric-linux-amd64-2.3.2.tar.gz
RUN touch core.yaml
RUN ./bin/peer lifecycle chaincode package /firefly/smart_contracts/fabric/firefly-go/firefly_fabric.tar.gz --path /firefly/smart_contracts/fabric/firefly-go --lang golang --label firefly_1.0

FROM node:14-alpine3.11 AS solidity-builder
WORKDIR /firefly/solidity_firefly
ADD smart_contracts/ethereum/solidity_firefly/package*.json .
Expand All @@ -23,5 +34,6 @@ COPY --from=firefly-builder /firefly/firefly ./firefly
COPY --from=firefly-builder /firefly/frontend/ /firefly/frontend/
COPY --from=firefly-builder /firefly/db ./db
COPY --from=solidity-builder /firefly/solidity_firefly/build/contracts ./contracts
COPY --from=fabric-builder /firefly/smart_contracts/fabric/firefly-go/firefly_fabric.tar.gz ./contracts/firefly_fabric.tar.gz
RUN ln -s /firefly/firefly /usr/bin/firefly
ENTRYPOINT [ "firefly" ]
2 changes: 2 additions & 0 deletions internal/blockchain/bifactory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"context"

"github.com/hyperledger/firefly/internal/blockchain/ethereum"
"github.com/hyperledger/firefly/internal/blockchain/fabric"
"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/i18n"
"github.com/hyperledger/firefly/pkg/blockchain"
)

var plugins = []blockchain.Plugin{
&ethereum.Ethereum{},
&fabric.Fabric{},
}

var pluginsByName = make(map[string]blockchain.Plugin)
Expand Down
110 changes: 110 additions & 0 deletions internal/blockchain/fabric/certs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright © 2021 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fabric

import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
)

var attributeTypeNames = map[string]string{
"2.5.4.6": "C",
"2.5.4.10": "O",
"2.5.4.11": "OU",
"2.5.4.3": "CN",
"2.5.4.5": "SERIALNUMBER",
"2.5.4.7": "L",
"2.5.4.8": "ST",
"2.5.4.9": "STREET",
"2.5.4.17": "POSTALCODE",
}

func getDNFromCertString(certString string) (string, error) {
cert, err := getCertificateFromBytes(certString)
if err != nil {
return "", err
}
return getDN(&cert.Subject), nil
}

// borrowed from fabric-chaincode-go to guarantee the same
// resolution of "DN" string from x509 certs
// https://github.com/hyperledger/fabric-chaincode-go/blob/main/pkg/cid/cid.go
func getDN(name *pkix.Name) string {
r := name.ToRDNSequence()
s := ""
for i := 0; i < len(r); i++ {
rdn := r[len(r)-1-i]
if i > 0 {
s += ","
}
for j, tv := range rdn {
if j > 0 {
s += "+"
}
typeString := tv.Type.String()
typeName, ok := attributeTypeNames[typeString]
if !ok {
derBytes, err := asn1.Marshal(tv.Value)
if err == nil {
s += typeString + "=#" + hex.EncodeToString(derBytes)
continue // No value escaping necessary.
}
typeName = typeString
}
valueString := fmt.Sprint(tv.Value)
escaped := ""
begin := 0
for idx, c := range valueString {
if (idx == 0 && (c == ' ' || c == '#')) ||
(idx == len(valueString)-1 && c == ' ') {
escaped += valueString[begin:idx]
escaped += "\\" + string(c)
begin = idx + 1
continue
}
switch c {
case ',', '+', '"', '\\', '<', '>', ';':
escaped += valueString[begin:idx]
escaped += "\\" + string(c)
begin = idx + 1
}
}
escaped += valueString[begin:]
s += typeName + "=" + escaped
}
}
return s
}

func getCertificateFromBytes(certString string) (*x509.Certificate, error) {
idbytes, err := base64.StdEncoding.DecodeString(certString)
if err != nil {
return nil, err
}
block, _ := pem.Decode(idbytes)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return cert, nil
}
68 changes: 68 additions & 0 deletions internal/blockchain/fabric/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright © 2021 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fabric

import (
"github.com/hyperledger/firefly/internal/config"
"github.com/hyperledger/firefly/internal/config/wsconfig"
)

const (
defaultBatchSize = 50
defaultBatchTimeout = 500
defaultPrefixShort = "fly"
defaultPrefixLong = "firefly"
)

const (
// FabconnectConfigKey is a sub-key in the config to contain all the ethconnect specific config,
FabconnectConfigKey = "fabconnect"

// FabconnectConfigDefaultChannel is the default Fabric channel to use if no "ledger" is specified in requests
FabconnectConfigDefaultChannel = "channel"
// FabconnectConfigChaincode is the Fabric Firefly chaincode deployed to the Firefly channels
FabconnectConfigChaincode = "chaincode"
// FabconnectConfigSigner is the signer identity used to subscribe to FireFly chaincode events
FabconnectConfigSigner = "signer"
// FabconnectConfigTopic is the websocket listen topic that the node should register on, which is important if there are multiple
// nodes using a single fabconnect
FabconnectConfigTopic = "topic"
// FabconnectConfigBatchSize is the batch size to configure on event streams, when auto-defining them
FabconnectConfigBatchSize = "batchSize"
// FabconnectConfigBatchTimeout is the batch timeout to configure on event streams, when auto-defining them
FabconnectConfigBatchTimeout = "batchTimeout"
// FabconnectConfigSkipEventstreamInit disables auto-configuration of event streams
FabconnectConfigSkipEventstreamInit = "skipEventstreamInit"
// FabconnectPrefixShort is used in the query string in requests to ethconnect
FabconnectPrefixShort = "prefixShort"
// FabconnectPrefixLong is used in HTTP headers in requests to ethconnect
FabconnectPrefixLong = "prefixLong"
)

func (f *Fabric) InitPrefix(prefix config.Prefix) {
fabconnectConf := prefix.SubPrefix(FabconnectConfigKey)
wsconfig.InitPrefix(fabconnectConf)
fabconnectConf.AddKnownKey(FabconnectConfigDefaultChannel)
fabconnectConf.AddKnownKey(FabconnectConfigChaincode)
fabconnectConf.AddKnownKey(FabconnectConfigSigner)
fabconnectConf.AddKnownKey(FabconnectConfigTopic)
fabconnectConf.AddKnownKey(FabconnectConfigSkipEventstreamInit)
fabconnectConf.AddKnownKey(FabconnectConfigBatchSize, defaultBatchSize)
fabconnectConf.AddKnownKey(FabconnectConfigBatchTimeout, defaultBatchTimeout)
fabconnectConf.AddKnownKey(FabconnectPrefixShort, defaultPrefixShort)
fabconnectConf.AddKnownKey(FabconnectPrefixLong, defaultPrefixLong)
}
Loading

0 comments on commit 3d9f3ad

Please sign in to comment.