Skip to content

Commit

Permalink
Fix blockIssuerKeys interface convertion panic
Browse files Browse the repository at this point in the history
  • Loading branch information
daria305 committed Jan 31, 2024
1 parent a9fb2c6 commit f87910f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
11 changes: 7 additions & 4 deletions pkg/accountmanager/accountcreation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/iotaledger/evil-tools/pkg/models"
"github.com/iotaledger/evil-tools/pkg/utils"
"github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
iotago "github.com/iotaledger/iota.go/v4"
Expand Down Expand Up @@ -62,10 +61,14 @@ func (m *Manager) transitionImplicitAccount(ctx context.Context, implicitAccount
// build account output with new Ed25519 address
wallet := m.getOrCreateWallet(params.Alias)
accEd25519Addr, accPrivateKey, accAddrIndex := wallet.getAddress(iotago.AddressEd25519)
accBlockIssuerKey := iotago.Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey(accPrivateKey.Public().(ed25519.PublicKey))

accBlockIssuerKey, err := utils.GetAccountIssuerKeys(accPrivateKey.Public())
if err != nil {
return iotago.EmptyAccountID, ierrors.Wrap(err, "failed to get account address and keys")
}
accountOutput := builder.NewAccountOutputBuilder(accEd25519Addr, tokenBalance).
AccountID(accountID).
BlockIssuer(iotago.NewBlockIssuerKeys(accBlockIssuerKey), iotago.MaxSlotIndex).MustBuild()
BlockIssuer(accBlockIssuerKey, iotago.MaxSlotIndex).MustBuild()

// transaction preparation, issue block with implicit account
implicitAccountIssuer := iotagowallet.NewEd25519Account(accountID, implicitAccountOutput.PrivateKey)
Expand Down Expand Up @@ -112,7 +115,7 @@ func (m *Manager) createAccountWithFaucet(ctx context.Context, params *CreateAcc
return iotago.EmptyAccountID, ierrors.Wrap(err, "failed to request enough funds for account creation")
}
accAddr, accPrivateKey, accAddrIndex := w.getAddress(iotago.AddressEd25519)
blockIssuerKeys, err := w.getAccountPublicKeys(accPrivateKey.Public())
blockIssuerKeys, err := utils.GetAccountIssuerKeys(accPrivateKey.Public())
if err != nil {
return iotago.EmptyAccountID, ierrors.Wrap(err, "failed to get account address and keys")
}
Expand Down
32 changes: 9 additions & 23 deletions pkg/accountmanager/wallet.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package accountmanager

import (
"crypto"
"crypto/ed25519"

"github.com/iotaledger/evil-tools/pkg/models"
hiveEd25519 "github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
iotago "github.com/iotaledger/iota.go/v4"
Expand Down Expand Up @@ -46,7 +44,7 @@ func (m *Manager) getOrCreateWallet(alias string) *Wallet {
return w
}

func (a *Wallet) GetAddrSignerForIndexes(outputs ...*models.OutputData) (iotago.AddressSigner, error) {
func (w *Wallet) GetAddrSignerForIndexes(outputs ...*models.OutputData) (iotago.AddressSigner, error) {
var addrKeys []iotago.AddressKeys
for _, out := range outputs {
switch out.Address.Type() {
Expand All @@ -68,37 +66,25 @@ func (a *Wallet) GetAddrSignerForIndexes(outputs ...*models.OutputData) (iotago.
return iotago.NewInMemoryAddressSigner(addrKeys...), nil
}

func (a *Wallet) getAccountPublicKeys(pubKey crypto.PublicKey) (iotago.BlockIssuerKeys, error) {
ed25519PubKey, isEd25519 := pubKey.(ed25519.PublicKey)
if !isEd25519 {
return nil, ierrors.New("Failed to create account: only Ed25519 keys are supported")
}

blockIssuerKeys := iotago.NewBlockIssuerKeys(iotago.Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey(hiveEd25519.PublicKey(ed25519PubKey)))

return blockIssuerKeys, nil

}

func (a *Wallet) getAddress(addressType iotago.AddressType) (iotago.DirectUnlockableAddress, ed25519.PrivateKey, uint32) {
a.LatestUsedIndex++
newIndex := a.LatestUsedIndex
keyManager := lo.PanicOnErr(wallet.NewKeyManager(a.Seed[:], BIP32PathForIndex(newIndex)))
func (w *Wallet) getAddress(addressType iotago.AddressType) (iotago.DirectUnlockableAddress, ed25519.PrivateKey, uint32) {
w.LatestUsedIndex++
newIndex := w.LatestUsedIndex
keyManager := lo.PanicOnErr(wallet.NewKeyManager(w.Seed[:], BIP32PathForIndex(newIndex)))
privateKey, _ := keyManager.KeyPair()
receiverAddr := keyManager.Address(addressType)

return receiverAddr, privateKey, newIndex
}

func (a *Wallet) getPrivateKeyForIndex(index uint32) ed25519.PrivateKey {
keyManager := lo.PanicOnErr(wallet.NewKeyManager(a.Seed[:], BIP32PathForIndex(index)))
func (w *Wallet) getPrivateKeyForIndex(index uint32) ed25519.PrivateKey {
keyManager := lo.PanicOnErr(wallet.NewKeyManager(w.Seed[:], BIP32PathForIndex(index)))
privateKey, _ := keyManager.KeyPair()

return privateKey
}

func (a *Wallet) createOutputDataForIndex(outputID iotago.OutputID, index uint32, outputStruct iotago.Output) *models.OutputData {
privateKey := a.getPrivateKeyForIndex(index)
func (w *Wallet) createOutputDataForIndex(outputID iotago.OutputID, index uint32, outputStruct iotago.Output) *models.OutputData {
privateKey := w.getPrivateKeyForIndex(index)

return &models.OutputData{
OutputID: outputID,
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package utils

import (
"crypto"
"crypto/ed25519"

"github.com/iotaledger/evil-tools/pkg/models"
hiveEd25519 "github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
iotago "github.com/iotaledger/iota.go/v4"
"github.com/iotaledger/iota.go/v4/builder"
"github.com/iotaledger/iota.go/v4/tpkg"
)

func GetAccountIssuerKeys(pubKey crypto.PublicKey) (iotago.BlockIssuerKeys, error) {
ed25519PubKey, isEd25519 := pubKey.(ed25519.PublicKey)
if !isEd25519 {
return nil, ierrors.New("Failed to create account: only Ed25519 keys are supported")
}

blockIssuerKeys := iotago.NewBlockIssuerKeys(iotago.Ed25519PublicKeyHashBlockIssuerKeyFromPublicKey(hiveEd25519.PublicKey(ed25519PubKey)))

return blockIssuerKeys, nil

}

// SplitBalanceEqually splits the balance equally between `splitNumber` outputs.
func SplitBalanceEqually[T iotago.BaseToken | iotago.Mana](splitNumber int, balance T) []T {
outputBalances := make([]T, 0)
Expand Down

0 comments on commit f87910f

Please sign in to comment.