-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Relevant issue(s) Resolves #2908 ## Description Assign an identity to the node upon startup.
- Loading branch information
1 parent
bb0917e
commit d95c51f
Showing
124 changed files
with
1,607 additions
and
1,166 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
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,41 @@ | ||
// 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 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sourcenetwork/immutable" | ||
) | ||
|
||
// identityContextKey is the key type for ACP identity context values. | ||
type identityContextKey struct{} | ||
|
||
// FromContext returns the identity from the given context. | ||
// | ||
// If an identity does not exist `NoIdentity` is returned. | ||
func FromContext(ctx context.Context) immutable.Option[Identity] { | ||
identity, ok := ctx.Value(identityContextKey{}).(Identity) | ||
if ok { | ||
return immutable.Some(identity) | ||
} | ||
return None | ||
} | ||
|
||
// WithContext returns a new context with the identity value set. | ||
// | ||
// This will overwrite any previously set identity value. | ||
func WithContext(ctx context.Context, identity immutable.Option[Identity]) context.Context { | ||
if identity.HasValue() { | ||
return context.WithValue(ctx, identityContextKey{}, identity.Value()) | ||
} | ||
return context.WithValue(ctx, identityContextKey{}, 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
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
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,73 @@ | ||
// 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 | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
"github.com/decred/dcrd/dcrec/secp256k1/v4" | ||
) | ||
|
||
// RawIdentity holds the raw bytes that make up an actor's identity. | ||
type RawIdentity struct { | ||
// PrivateKey is a secp256k1 private key that is a 256-bit big-endian | ||
// binary-encoded number, padded to a length of 32 bytes in HEX format. | ||
PrivateKey string | ||
|
||
// PublicKey is a compressed 33-byte secp256k1 public key in HEX format. | ||
PublicKey string | ||
|
||
// DID is `did:key` key generated from the public key address. | ||
DID string | ||
} | ||
|
||
// PublicRawIdentity holds the raw bytes that make up an actor's identity that can be shared publicly. | ||
type PublicRawIdentity struct { | ||
// PublicKey is a compressed 33-byte secp256k1 public key in HEX format. | ||
PublicKey string | ||
|
||
// DID is `did:key` key generated from the public key address. | ||
DID string | ||
} | ||
|
||
func newRawIdentity(privateKey *secp256k1.PrivateKey, publicKey *secp256k1.PublicKey, did string) RawIdentity { | ||
res := RawIdentity{ | ||
PublicKey: hex.EncodeToString(publicKey.SerializeCompressed()), | ||
DID: did, | ||
} | ||
if privateKey != nil { | ||
res.PrivateKey = hex.EncodeToString(privateKey.Serialize()) | ||
} | ||
return res | ||
} | ||
|
||
func (r RawIdentity) Public() PublicRawIdentity { | ||
return PublicRawIdentity{ | ||
PublicKey: r.PublicKey, | ||
DID: r.DID, | ||
} | ||
} | ||
|
||
// IntoIdentity converts a RawIdentity into an Identity. | ||
func (r RawIdentity) IntoIdentity() (Identity, error) { | ||
privateKeyBytes, err := hex.DecodeString(r.PrivateKey) | ||
if err != nil { | ||
return Identity{}, err | ||
} | ||
|
||
privateKey := secp256k1.PrivKeyFromBytes(privateKeyBytes) | ||
|
||
return Identity{ | ||
PublicKey: privateKey.PubKey(), | ||
PrivateKey: privateKey, | ||
DID: r.DID, | ||
}, 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
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
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
Oops, something went wrong.