mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-04 18:31:41 +00:00
feature/1121 implement ucan validation (#1176)
- **refactor: remove unused auth components** - **refactor: improve devbox configuration and deployment process** - **refactor: improve devnet and testnet setup** - **fix: update templ version to v0.2.778** - **refactor: rename pkl/net.matrix to pkl/matrix.net** - **refactor: migrate webapp components to nebula** - **refactor: protobuf types** - **chore: update dependencies for improved security and stability** - **feat: implement landing page and vault gateway servers** - **refactor: Migrate data models to new module structure and update related files** - **feature/1121-implement-ucan-validation** - **refactor: Replace hardcoded constants with model types in attns.go** - **feature/1121-implement-ucan-validation** - **chore: add origin Host struct and update main function to handle multiple hosts** - **build: remove unused static files from dwn module** - **build: remove unused static files from dwn module** - **refactor: Move DWN models to common package** - **refactor: move models to pkg/common** - **refactor: move vault web app assets to embed module** - **refactor: update session middleware import path** - **chore: configure port labels and auto-forwarding behavior** - **feat: enhance devcontainer configuration** - **feat: Add UCAN middleware for Echo with flexible token validation** - **feat: add JWT middleware for UCAN authentication** - **refactor: update package URI and versioning in PklProject files** - **fix: correct sonr.pkl import path** - **refactor: move JWT related code to auth package** - **feat: introduce vault configuration retrieval and management** - **refactor: Move vault components to gateway module and update file paths** - **refactor: remove Dexie and SQLite database implementations** - **feat: enhance frontend with PWA features and WASM integration** - **feat: add Devbox features and streamline Dockerfile** - **chore: update dependencies to include TigerBeetle** - **chore(deps): update go version to 1.23** - **feat: enhance devnet setup with PATH environment variable and updated PWA manifest** - **fix: upgrade tigerbeetle-go dependency and remove indirect dependency** - **feat: add PostgreSQL support to devnet and testnet deployments** - **refactor: rename keyshare cookie to token cookie** - **feat: upgrade Go version to 1.23.3 and update dependencies** - **refactor: update devnet and testnet configurations** - **feat: add IPFS configuration for devnet** - **I'll help you update the ipfs.config.pkl to include all the peers from the shell script. Here's the updated configuration:** - **refactor: move mpc package to crypto directory** - **feat: add BIP32 support for various cryptocurrencies** - **feat: enhance ATN.pkl with additional capabilities** - **refactor: simplify smart account and vault attenuation creation** - **feat: add new capabilities to the Attenuation type** - **refactor: Rename MPC files for clarity and consistency** - **feat: add DIDKey support for cryptographic operations** - **feat: add devnet and testnet deployment configurations** - **fix: correct key derivation in bip32 package** - **refactor: rename crypto/bip32 package to crypto/accaddr** - **fix: remove duplicate indirect dependency** - **refactor: move vault package to root directory** - **refactor: update routes for gateway and vault** - **refactor: remove obsolete web configuration file** - **refactor: remove unused TigerBeetle imports and update host configuration** - **refactor: adjust styles directory path** - **feat: add broadcastTx and simulateTx functions to gateway** - **feat: add PinVault handler**
This commit is contained in:
Executable
+237
@@ -0,0 +1,237 @@
|
||||
//
|
||||
// Copyright Coinbase, Inc. All Rights Reserved.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
bls "github.com/onsonr/sonr/crypto/signatures/bls/bls_sig"
|
||||
)
|
||||
|
||||
type signOp struct {
|
||||
Data string `json:"data"`
|
||||
PublicKey string `json:"public_key"`
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
|
||||
type cmdFlags struct {
|
||||
Generate bool
|
||||
PublicKeys bool
|
||||
Sign bool
|
||||
Verify bool
|
||||
Number int
|
||||
}
|
||||
|
||||
func parseCliArgs() cmdFlags {
|
||||
var generate, publickeys, sign, verify bool
|
||||
var number int
|
||||
flag.BoolVar(&generate, "g", false, "Generate public keys")
|
||||
flag.BoolVar(&publickeys, "p", false, "Verify public keys")
|
||||
flag.BoolVar(&sign, "s", false, "Generate signatures")
|
||||
flag.BoolVar(&verify, "v", false, "Verify signatures")
|
||||
flag.IntVar(&number, "n", 25, "The number of items to generate")
|
||||
flag.Parse()
|
||||
return cmdFlags{
|
||||
generate, publickeys, sign, verify, number,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
flags := parseCliArgs()
|
||||
|
||||
if flags.Generate {
|
||||
generate(flags.Number)
|
||||
} else if flags.PublicKeys {
|
||||
publicKeys()
|
||||
} else if flags.Sign {
|
||||
sign(flags.Number)
|
||||
} else if flags.Verify {
|
||||
verify()
|
||||
}
|
||||
}
|
||||
|
||||
func generate(number int) {
|
||||
scheme := bls.NewSigPop()
|
||||
publicKeys := make([]string, number)
|
||||
for i := 0; i < number; i++ {
|
||||
publicKey, _, err := scheme.Keygen()
|
||||
if err != nil {
|
||||
fmt.Printf("Keygen error occurred: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
b, _ := publicKey.MarshalBinary()
|
||||
publicKeys[i] = hex.EncodeToString(b)
|
||||
}
|
||||
out, _ := json.Marshal(publicKeys)
|
||||
fmt.Println(string(out))
|
||||
}
|
||||
|
||||
func publicKeys() {
|
||||
input, err := getInput()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to read input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var pubkeys []string
|
||||
err = json.Unmarshal(input, &pubkeys)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to parse json input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Checking public keys")
|
||||
for _, pk := range pubkeys {
|
||||
data, err := hex.DecodeString(pk)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to parse hex input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
pubkey := new(bls.PublicKey)
|
||||
fmt.Printf("Checking %s - ", pk)
|
||||
err = pubkey.UnmarshalBinary(data)
|
||||
if err != nil {
|
||||
fmt.Printf("fail\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("pass\n")
|
||||
}
|
||||
}
|
||||
|
||||
func sign(number int) {
|
||||
tests := []string{
|
||||
"", "aaa", "aaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
}
|
||||
|
||||
var err error
|
||||
scheme := bls.NewSigPop()
|
||||
secretKeys := make([]*bls.SecretKey, number)
|
||||
publicKeys := make([]*bls.PublicKey, number)
|
||||
for i := 0; i < number; i++ {
|
||||
publicKeys[i], secretKeys[i], err = scheme.Keygen()
|
||||
if err != nil {
|
||||
fmt.Printf("Keygen error occurred: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
sigs := make([]*signOp, number*len(tests))
|
||||
|
||||
k := 0
|
||||
for _, t := range tests {
|
||||
for j := 0; j < len(secretKeys); j++ {
|
||||
signature, err := scheme.Sign(secretKeys[j], []byte(t))
|
||||
if err != nil {
|
||||
fmt.Printf("Signing failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
pk, _ := publicKeys[j].MarshalBinary()
|
||||
sig, _ := signature.MarshalBinary()
|
||||
sigs[k] = &signOp{
|
||||
Data: t,
|
||||
PublicKey: hex.EncodeToString(pk),
|
||||
Signature: hex.EncodeToString(sig),
|
||||
}
|
||||
k++
|
||||
}
|
||||
}
|
||||
|
||||
data, err := json.Marshal(sigs)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to convert signatures to json")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Print(string(data))
|
||||
}
|
||||
|
||||
func verify() {
|
||||
input, err := getInput()
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to read input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var operations []signOp
|
||||
err = json.Unmarshal(input, &operations)
|
||||
if err != nil {
|
||||
fmt.Printf("Unable to parse json input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
scheme := bls.NewSigPop()
|
||||
for _, op := range operations {
|
||||
fmt.Printf("Checking %s - ", op.PublicKey)
|
||||
data, err := hex.DecodeString(op.PublicKey)
|
||||
if err != nil {
|
||||
fmt.Printf("fail. Unable to parse hex input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
pubkey := new(bls.PublicKey)
|
||||
err = pubkey.UnmarshalBinary(data)
|
||||
if err != nil {
|
||||
fmt.Printf("fail. Invalid public key: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
data, err = hex.DecodeString(op.Signature)
|
||||
if err != nil {
|
||||
fmt.Printf("fail. Unable to parse hex input: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
sig := new(bls.Signature)
|
||||
err = sig.UnmarshalBinary(data)
|
||||
if err != nil {
|
||||
fmt.Printf("fail. Invalid signature format: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
ok, err := scheme.Verify(pubkey, []byte(op.Data), sig)
|
||||
if !ok || err != nil {
|
||||
fmt.Printf("fail. Invalid signature")
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("pass.")
|
||||
}
|
||||
}
|
||||
|
||||
func getInput() ([]byte, error) {
|
||||
fi, _ := os.Stdin.Stat()
|
||||
|
||||
var data []byte
|
||||
|
||||
if (fi.Mode() & os.ModeCharDevice) == 0 {
|
||||
// Read from pipe
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
data = append(data, scanner.Bytes()...)
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// Read from file
|
||||
arguments := flag.Args()
|
||||
if len(arguments) < 1 {
|
||||
return nil, fmt.Errorf("expected data argument")
|
||||
}
|
||||
|
||||
_, err := os.Stat(arguments[0])
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
data = []byte(arguments[0])
|
||||
} else {
|
||||
contents, err := ioutil.ReadFile(arguments[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data = contents
|
||||
}
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user