Skip to content

Commit 2ea27e8

Browse files
committed
Add crypto-providers ADR implementation POC
1 parent bb8c5de commit 2ea27e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1695
-3369
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea
1+
.idea
2+
.vscode
3+
build/*

Makefile

+13-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
1-
# Variables
2-
PKG := ./...
3-
GOFILES := $(shell find . -name '*.go' | grep -v _test.go)
4-
TESTFILES := $(shell find . -name '*_test.go')
5-
GOLANGCI_VERSION := v1.59.0
1+
WALLET_BIN := build/wallet
62

7-
all: build
3+
.PHONY: demo run clean
84

9-
build:
10-
@if [ -z "$(TAGS)" ]; then \
11-
echo "Building..."; \
12-
else \
13-
echo "Building with tags: $(TAGS)"; \
14-
fi
15-
@go build -tags "$(TAGS)" $(PKG)
5+
.DEFAULT_GOAL := build
166

17-
# Run tests
18-
test:
19-
@if [ -z "$(TAGS)" ]; then \
20-
echo "Running tests..."; \
21-
else \
22-
echo "Running tests with tags: $(TAGS)"; \
23-
fi
24-
@go test -tags "$(TAGS)" -v $(PKG)
7+
# Build the wallet command
8+
build-wallet:
9+
go build -o $(WALLET_BIN) ./cmd
2510

26-
# Install golangci-lint
27-
lint-install:
28-
@echo "--> Installing golangci-lint $(GOLANGCI_VERSION)"
29-
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION)
11+
# Run the demo
12+
demo:
13+
cd demo; \
14+
go run main.go
3015

31-
# Run golangci-lint
32-
lint:
33-
@echo "--> Running linter"
34-
$(MAKE) lint-install
35-
@golangci-lint run --timeout=15m
36-
37-
# Run golangci-lint and fix
38-
lint-fix:
39-
@echo "--> Running linter with fix"
40-
$(MAKE) lint-install
41-
@golangci-lint run --fix
42-
43-
.PHONY: build test lint-install lint lint-fix
16+
# Clean built binaries
17+
clean:
18+
rm -f $(WALLET_BIN)

README.md

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1-
# crypto
1+
# crypto-provider
22

3-
cosmos-crypto is the cryptographic package adapted for the interchain stack
3+
<p align="center">
4+
<img src="logo.png" alt="Crypto Provider Logo" width="200" />
5+
</p>
46

5-
## Importing it
7+
## Overview
68

7-
To get the interfaces,
8-
`import "github.com/cosmos/crypto/types"`
9+
This is a Proof of Concept of the crypto-providers ADR as described in the [ADR-001 Crypto Provider](https://github.com/cosmos/crypto/blob/main/docs/architecture/adr-001-crypto-provider.md).
910

11+
## Main Components
12+
13+
The main components of this project are located in the `components` package. They include:
14+
15+
- **CryptoProvider**: Aggregates functionalities of signing, verifying, and hashing, and provides metadata.
16+
- **CryptoProviderFactory**: A factory interface for creating CryptoProviders.
17+
- **BuildSource**: Various implementations for building CryptoProviders from different sources.
18+
- **ProviderMetadata**: Metadata structure for the crypto provider.
19+
- **Signer, Verifier, Hasher**: Interfaces for signing, verifying, and hashing data.
20+
- **AddressFormatter**: Interface for formatting addresses from public key bytes.
21+
22+
## Running the Demo App
23+
24+
To run the demo app, just type the following command:
25+
26+
```bash
27+
make demo
28+
```
29+
30+
### What the Demo Does
31+
32+
The demo application performs the following steps:
33+
34+
1. Creates a new wallet using an in-memory keyring.
35+
2. Loads a JSON file and creates a new crypto provider from it.
36+
3. Retrieves a signer from the selected provider.
37+
4. Generates random data and signs it using the signer.
38+
5. Retrieves a verifier from the provider and verifies the generated signature.
39+
40+
### Demo Architecture
41+
42+
```mermaid
43+
graph TD
44+
A[Demo Application] --> B[Wallet]
45+
B --> C[Keyring]
46+
B --> D[CryptoProviderFactory]
47+
B --> Q[SimpleAddressFormatter]
48+
D --> E[FileProviderFactory]
49+
E --> F[FileProvider]
50+
F --> G[Signer]
51+
F --> H[Verifier]
52+
F --> I[Hasher]
53+
F --> O[ProviderMetadata]
54+
F --> P[FileProviderConfig]
55+
```

armor/armor.go

-52
This file was deleted.

armor/armor_test.go

-21
This file was deleted.

cmd/list.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var listCmd = &cobra.Command{
9+
Use: "list",
10+
Short: "List all providers",
11+
Long: `This command lists all the crypto providers currently available in the wallet.`,
12+
RunE: func(cmd *cobra.Command, args []string) error {
13+
w, err := setup()
14+
if err != nil {
15+
return err
16+
}
17+
18+
providers, err := w.ListProviders()
19+
if err != nil {
20+
return fmt.Errorf("failed to list providers: %v", err)
21+
}
22+
23+
if len(providers) == 0 {
24+
fmt.Println("No providers found.")
25+
} else {
26+
fmt.Println("Available providers:")
27+
for _, provider := range providers {
28+
fmt.Printf("- %s\n", provider)
29+
}
30+
}
31+
32+
return nil
33+
},
34+
}

cmd/register/register.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package register
2+
3+
import (
4+
// Import all provider packages here
5+
"crypto-provider/pkg/factory"
6+
_ "crypto-provider/pkg/provider/file"
7+
_ "crypto-provider/pkg/provider/file/cmd"
8+
// Add other providers as needed
9+
// _ "crypto-provider/pkg/provider/someprovider"
10+
)
11+
12+
// Init is a dummy function to ensure this package is imported
13+
func Init() {
14+
_ = factory.GetFactory()
15+
}

cmd/root.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"crypto-provider/pkg/cli"
5+
"crypto-provider/pkg/keyring"
6+
"crypto-provider/pkg/wallet"
7+
"fmt"
8+
"os"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
flags struct {
15+
providersDir string
16+
}
17+
)
18+
19+
// SimpleAddressFormatter implementation
20+
type SimpleAddressFormatter struct{}
21+
22+
func (f SimpleAddressFormatter) FormatAddress(pubKey []byte) (string, error) {
23+
return fmt.Sprintf("addr_%x", pubKey[:8]), nil
24+
}
25+
26+
func setup() (wallet.Wallet, error) {
27+
addressFormatter := SimpleAddressFormatter{}
28+
w, err := wallet.NewKeyringWallet("wallet-app", keyring.BackendMemory, flags.providersDir, addressFormatter)
29+
if err != nil {
30+
return nil, fmt.Errorf("failed to create wallet: %v", err)
31+
}
32+
return w, nil
33+
}
34+
35+
func initFlags(rootCmd *cobra.Command) {
36+
rootCmd.PersistentFlags().StringVar(&flags.providersDir, "providers-dir", "", "Directory containing provider configurations")
37+
_ = rootCmd.MarkPersistentFlagRequired("providers-dir")
38+
}
39+
40+
func main() {
41+
rootCmd := cli.GetRootCmd()
42+
initFlags(rootCmd)
43+
rootCmd.AddCommand(listCmd)
44+
45+
if err := rootCmd.Execute(); err != nil {
46+
fmt.Println(err)
47+
os.Exit(1)
48+
}
49+
}

curves/bls12381/alias.go

-7
This file was deleted.

curves/bls12381/doc.go

-6
This file was deleted.

curves/bls12381/helper_test.go

-13
This file was deleted.

curves/bls12381/init.go

-25
This file was deleted.

curves/bls12381/interface.go

-21
This file was deleted.

0 commit comments

Comments
 (0)