Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Oct 10, 2024
1 parent f001109 commit e2febc3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
21 changes: 1 addition & 20 deletions acp/identity/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,9 @@
package identity

import (
"encoding/hex"

"github.com/sourcenetwork/defradb/crypto"
)

// 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
}

// Generate generates a new identity.
func Generate() (RawIdentity, error) {
privateKey, err := crypto.GenerateSecp256k1()
Expand All @@ -43,9 +28,5 @@ func Generate() (RawIdentity, error) {
return RawIdentity{}, err
}

return RawIdentity{
PrivateKey: hex.EncodeToString(privateKey.Serialize()),
PublicKey: hex.EncodeToString(publicKey.SerializeCompressed()),
DID: did,
}, nil
return newRawIdentity(privateKey, publicKey, did), nil
}
6 changes: 1 addition & 5 deletions acp/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,5 @@ func didFromPublicKey(publicKey *secp256k1.PublicKey, producer didProducer) (str

// IntoRawIdentity converts an `Identity` into a `RawIdentity`.
func (identity Identity) IntoRawIdentity() RawIdentity {
return RawIdentity{
PrivateKey: hex.EncodeToString(identity.PrivateKey.Serialize()),
PublicKey: hex.EncodeToString(identity.PublicKey.SerializeCompressed()),
DID: identity.DID,
}
return newRawIdentity(identity.PrivateKey, identity.PublicKey, identity.DID)
}
38 changes: 38 additions & 0 deletions acp/identity/raw_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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
}

func newRawIdentity(privateKey *secp256k1.PrivateKey, publicKey *secp256k1.PublicKey, did string) RawIdentity {
return RawIdentity{
PrivateKey: hex.EncodeToString(privateKey.Serialize()),
PublicKey: hex.EncodeToString(publicKey.SerializeCompressed()),
DID: did,
}
}

0 comments on commit e2febc3

Please sign in to comment.