Skip to content

Commit

Permalink
Add crypto-providers ADR implementation POC
Browse files Browse the repository at this point in the history
  • Loading branch information
raynaudoe committed Aug 30, 2024
1 parent bb8c5de commit 2ea27e8
Show file tree
Hide file tree
Showing 79 changed files with 1,695 additions and 3,369 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
.idea
.vscode
build/*
51 changes: 13 additions & 38 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
# Variables
PKG := ./...
GOFILES := $(shell find . -name '*.go' | grep -v _test.go)
TESTFILES := $(shell find . -name '*_test.go')
GOLANGCI_VERSION := v1.59.0
WALLET_BIN := build/wallet

all: build
.PHONY: demo run clean

build:
@if [ -z "$(TAGS)" ]; then \
echo "Building..."; \
else \
echo "Building with tags: $(TAGS)"; \
fi
@go build -tags "$(TAGS)" $(PKG)
.DEFAULT_GOAL := build

# Run tests
test:
@if [ -z "$(TAGS)" ]; then \
echo "Running tests..."; \
else \
echo "Running tests with tags: $(TAGS)"; \
fi
@go test -tags "$(TAGS)" -v $(PKG)
# Build the wallet command
build-wallet:
go build -o $(WALLET_BIN) ./cmd

# Install golangci-lint
lint-install:
@echo "--> Installing golangci-lint $(GOLANGCI_VERSION)"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION)
# Run the demo
demo:
cd demo; \
go run main.go

# Run golangci-lint
lint:
@echo "--> Running linter"
$(MAKE) lint-install
@golangci-lint run --timeout=15m

# Run golangci-lint and fix
lint-fix:
@echo "--> Running linter with fix"
$(MAKE) lint-install
@golangci-lint run --fix

.PHONY: build test lint-install lint lint-fix
# Clean built binaries
clean:
rm -f $(WALLET_BIN)
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
# crypto
# crypto-provider

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

## Importing it
## Overview

To get the interfaces,
`import "github.com/cosmos/crypto/types"`
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).

## Main Components

The main components of this project are located in the `components` package. They include:

- **CryptoProvider**: Aggregates functionalities of signing, verifying, and hashing, and provides metadata.
- **CryptoProviderFactory**: A factory interface for creating CryptoProviders.
- **BuildSource**: Various implementations for building CryptoProviders from different sources.
- **ProviderMetadata**: Metadata structure for the crypto provider.
- **Signer, Verifier, Hasher**: Interfaces for signing, verifying, and hashing data.
- **AddressFormatter**: Interface for formatting addresses from public key bytes.

## Running the Demo App

To run the demo app, just type the following command:

```bash
make demo
```

### What the Demo Does

The demo application performs the following steps:

1. Creates a new wallet using an in-memory keyring.
2. Loads a JSON file and creates a new crypto provider from it.
3. Retrieves a signer from the selected provider.
4. Generates random data and signs it using the signer.
5. Retrieves a verifier from the provider and verifies the generated signature.

### Demo Architecture

```mermaid
graph TD
A[Demo Application] --> B[Wallet]
B --> C[Keyring]
B --> D[CryptoProviderFactory]
B --> Q[SimpleAddressFormatter]
D --> E[FileProviderFactory]
E --> F[FileProvider]
F --> G[Signer]
F --> H[Verifier]
F --> I[Hasher]
F --> O[ProviderMetadata]
F --> P[FileProviderConfig]
```
52 changes: 0 additions & 52 deletions armor/armor.go

This file was deleted.

21 changes: 0 additions & 21 deletions armor/armor_test.go

This file was deleted.

34 changes: 34 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List all providers",
Long: `This command lists all the crypto providers currently available in the wallet.`,
RunE: func(cmd *cobra.Command, args []string) error {
w, err := setup()
if err != nil {
return err
}

providers, err := w.ListProviders()
if err != nil {
return fmt.Errorf("failed to list providers: %v", err)
}

if len(providers) == 0 {
fmt.Println("No providers found.")
} else {
fmt.Println("Available providers:")
for _, provider := range providers {
fmt.Printf("- %s\n", provider)
}
}

return nil
},
}
15 changes: 15 additions & 0 deletions cmd/register/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package register

import (
// Import all provider packages here
"crypto-provider/pkg/factory"
_ "crypto-provider/pkg/provider/file"
_ "crypto-provider/pkg/provider/file/cmd"
// Add other providers as needed
// _ "crypto-provider/pkg/provider/someprovider"
)

// Init is a dummy function to ensure this package is imported
func Init() {
_ = factory.GetFactory()
}
49 changes: 49 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"crypto-provider/pkg/cli"
"crypto-provider/pkg/keyring"
"crypto-provider/pkg/wallet"
"fmt"
"os"

"github.com/spf13/cobra"
)

var (
flags struct {
providersDir string
}
)

// SimpleAddressFormatter implementation
type SimpleAddressFormatter struct{}

func (f SimpleAddressFormatter) FormatAddress(pubKey []byte) (string, error) {
return fmt.Sprintf("addr_%x", pubKey[:8]), nil
}

func setup() (wallet.Wallet, error) {
addressFormatter := SimpleAddressFormatter{}
w, err := wallet.NewKeyringWallet("wallet-app", keyring.BackendMemory, flags.providersDir, addressFormatter)
if err != nil {
return nil, fmt.Errorf("failed to create wallet: %v", err)
}
return w, nil
}

func initFlags(rootCmd *cobra.Command) {
rootCmd.PersistentFlags().StringVar(&flags.providersDir, "providers-dir", "", "Directory containing provider configurations")
_ = rootCmd.MarkPersistentFlagRequired("providers-dir")
}

func main() {
rootCmd := cli.GetRootCmd()
initFlags(rootCmd)
rootCmd.AddCommand(listCmd)

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
7 changes: 0 additions & 7 deletions curves/bls12381/alias.go

This file was deleted.

6 changes: 0 additions & 6 deletions curves/bls12381/doc.go

This file was deleted.

13 changes: 0 additions & 13 deletions curves/bls12381/helper_test.go

This file was deleted.

25 changes: 0 additions & 25 deletions curves/bls12381/init.go

This file was deleted.

21 changes: 0 additions & 21 deletions curves/bls12381/interface.go

This file was deleted.

Loading

0 comments on commit 2ea27e8

Please sign in to comment.