-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* derive key sets * derive key sets
- Loading branch information
Showing
10 changed files
with
242 additions
and
89 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
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
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,83 @@ | ||
package crypto | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"github.com/decred/dcrd/dcrec/secp256k1/v4" | ||
"github.com/samber/lo" | ||
"math" | ||
"sort" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const MaxOrder = 64 | ||
|
||
type KeySet struct { | ||
Id string | ||
DerivationPath string | ||
PublicKeys PublicKeyList `gorm:"-"` | ||
PrivateKeys PrivateKeyList `gorm:"-"` | ||
MintUrl string | ||
ValidFrom time.Time | ||
ValidTo time.Time | ||
FirstSeen time.Time | ||
Active time.Time | ||
} | ||
|
||
func NewKeySet(masterKey, derivationPath string) *KeySet { | ||
ks := &KeySet{DerivationPath: derivationPath} | ||
ks.DeriveKeys(masterKey) | ||
ks.DerivePublicKeys() | ||
ks.DeriveKeySetId() | ||
return ks | ||
} | ||
|
||
func (k *KeySet) DeriveKeys(masterKey string) { | ||
k.PrivateKeys = deriveKeys(masterKey, k.DerivationPath) | ||
} | ||
|
||
func (k *KeySet) DerivePublicKeys() { | ||
k.PublicKeys = derivePublicKeys(k.PrivateKeys) | ||
} | ||
|
||
func (k *KeySet) DeriveKeySetId() { | ||
k.Id = deriveKeySetId(k.PublicKeys) | ||
} | ||
|
||
// deriveKeys will generate private keys for the mint server | ||
func deriveKeys(masterKey string, derivationPath string) PrivateKeyList { | ||
pk := make(PrivateKeyList, 0) | ||
for i := 0; i < MaxOrder; i++ { | ||
hasher := sha256.New() | ||
hasher.Write([]byte(masterKey + derivationPath + strconv.Itoa(i))) | ||
pk = append(pk, PrivateKey{Amount: int64(math.Pow(2, float64(i))), Key: secp256k1.PrivKeyFromBytes(hasher.Sum(nil)[:32])}) | ||
} | ||
sort.Sort(pk) | ||
return pk | ||
} | ||
|
||
// derivePublicKeys will generate public keys for the mint server | ||
func derivePublicKeys(pk PrivateKeyList) PublicKeyList { | ||
PublicKeys := make(PublicKeyList, 0) | ||
for _, key := range pk { | ||
PublicKeys = append(PublicKeys, PublicKey{Amount: key.Amount, Key: key.Key.PubKey()}) | ||
} | ||
sort.Sort(pk) | ||
return PublicKeys | ||
} | ||
|
||
// deriveKeySetId will derive the keySetId from all public key of a keySet | ||
func deriveKeySetId(publicKeys PublicKeyList) string { | ||
var publicKeysConcatenated []byte | ||
// all public keys into concatenated and compressed hex string | ||
lo.ForEach[PublicKey](publicKeys, | ||
func(key PublicKey, _ int) { | ||
publicKeysConcatenated = append(publicKeysConcatenated, []byte(hex.EncodeToString(key.Key.SerializeCompressed()))...) | ||
}) | ||
// hash and encode | ||
hasher := sha256.New() | ||
hasher.Write(publicKeysConcatenated) | ||
return base64.StdEncoding.EncodeToString(hasher.Sum(nil))[:12] | ||
} |
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,43 @@ | ||
package crypto | ||
|
||
import "github.com/decred/dcrd/dcrec/secp256k1/v4" | ||
|
||
type PublicKey struct { | ||
Amount int64 | ||
Key *secp256k1.PublicKey | ||
} | ||
|
||
type PublicKeyList []PublicKey | ||
|
||
func (s PublicKeyList) ByAmount(amount int64) *PublicKey { | ||
for _, key := range s { | ||
if key.Amount == amount { | ||
return &key | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s PrivateKeyList) ByAmount(amount int64) *PrivateKey { | ||
for _, key := range s { | ||
if key.Amount == amount { | ||
return &key | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s PublicKeyList) Len() int { return len(s) } | ||
func (s PublicKeyList) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
func (s PublicKeyList) Less(i, j int) bool { return s[i].Amount < s[j].Amount } | ||
|
||
type PrivateKey struct { | ||
Amount int64 | ||
Key *secp256k1.PrivateKey | ||
} | ||
|
||
type PrivateKeyList []PrivateKey | ||
|
||
func (p PrivateKeyList) Len() int { return len(p) } | ||
func (p PrivateKeyList) Swap(i, j int) { p[i], p[j] = p[j], p[i] } | ||
func (p PrivateKeyList) Less(i, j int) bool { return p[i].Amount < p[j].Amount } |
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.