forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
/* | ||
Package identity provides defradb identity. | ||
*/ | ||
|
||
package identity | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/bech32" | ||
|
||
"github.com/cyware/ssi-sdk/crypto" | ||
"github.com/cyware/ssi-sdk/did/key" | ||
// "github.com/lestrrat-go/jwx/jwk" | ||
) | ||
|
||
const ( | ||
// Custom source account address prefix | ||
AccountAddressPrefix = "source" | ||
) | ||
|
||
// SourceIdentity holds the bits that make up an actor's unique identity. | ||
// TODO-ACP: This will be merged with `Identity`. | ||
type SourceIdentity struct { | ||
// An actor's private key. Uses secp256k1 algorithm. | ||
// PrivateJWK string | ||
|
||
// An actor's corresponding public key address. | ||
PublicKey string | ||
|
||
// An actor's DID. Generated from the public key address. | ||
DID string | ||
|
||
// SourceHub Account Address. Generated from the public key address. | ||
SourceAccountAddress string | ||
} | ||
|
||
// Generate generates a new source identity. | ||
func Generate() (SourceIdentity, error) { | ||
privateKey := secp256k1.GenPrivKey() | ||
|
||
publicKey := privateKey.PubKey().Address() | ||
|
||
accountAddress := types.AccAddress(publicKey) | ||
|
||
sourceAccountAddress, err := bech32.ConvertAndEncode(AccountAddressPrefix, accountAddress) | ||
if err != nil { | ||
return SourceIdentity{}, err | ||
} | ||
|
||
did, err := key.CreateDIDKey( | ||
crypto.SECP256k1, | ||
// []byte(publicKey), | ||
publicKey.Bytes(), | ||
) | ||
|
||
if err != nil { | ||
return SourceIdentity{}, err | ||
} | ||
|
||
fmt.Println("000000000000000000000000000000000000") | ||
fmt.Println("string(privateKeyAddress.Bytes()) : ", string(privateKey.Bytes())) | ||
fmt.Println("string(privateKeyAddress.GetKey()) : ", string(privateKey.GetKey())) | ||
fmt.Println("string(privateKeyAddress.Key) : ", string(privateKey.Key)) | ||
fmt.Println("privateKeyAddress.Key : ", privateKey.Key) | ||
fmt.Println("privateKeyAddress.GetKey() : ", privateKey.GetKey()) | ||
fmt.Println("privateKeyAddress.String() : ", privateKey.String()) | ||
fmt.Println("000000000000000000000000000000000000") | ||
|
||
// Generate a new secp256k1 private key | ||
// privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
// j, err := jwk.New(privateKey) | ||
if err != nil { | ||
return SourceIdentity{}, err | ||
} | ||
|
||
return SourceIdentity{ | ||
// PrivateJWK: string(privateKey.GetKey()), | ||
// PrivateJWK: j.Algorithm(), | ||
PublicKey: publicKey.String(), | ||
DID: did.String(), | ||
SourceAccountAddress: sourceAccountAddress, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func MakeACPIdentityCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "identity", | ||
Short: "Interact with the acp identity features of DefraDB instance", | ||
Long: `Interact with the acp identity features of DefraDB instance`, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2024 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"github.com/sourcenetwork/defradb/acp/identity" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func MakeACPIdentityNewCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "new", | ||
Short: "Generate a new identity", | ||
Long: `Generate a new identity | ||
Example: generate a new identity: | ||
defradb client acp identity new | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
newIdentity, err := identity.Generate() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return writeJSON(cmd, newIdentity) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters