2021-11-29 12:33:46 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-11-29 12:48:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-11-29 12:33:46 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/config"
|
2021-11-29 12:33:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2021-11-29 12:48:06 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-11-29 12:33:46 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2022-02-08 10:41:49 +00:00
|
|
|
const (
|
|
|
|
contractWalletFilename = "contract.json"
|
2022-04-08 14:58:21 +00:00
|
|
|
contractWalletPasswordKey = "contract"
|
2022-02-08 10:41:49 +00:00
|
|
|
)
|
2021-11-29 12:33:46 +00:00
|
|
|
|
2022-04-08 14:58:21 +00:00
|
|
|
func initializeContractWallet(v *viper.Viper, walletDir string) (*wallet.Wallet, error) {
|
|
|
|
password, err := config.GetPassword(v, contractWalletPasswordKey)
|
2022-02-08 10:41:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-29 12:48:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 14:40:51 +00:00
|
|
|
w, err := wallet.NewWallet(filepath.Join(walletDir, contractWalletFilename))
|
2021-11-29 12:48:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
acc, err := wallet.NewAccount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = acc.Encrypt(password, keys.NEP2ScryptParams())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.AddAccount(acc)
|
2022-06-09 08:53:31 +00:00
|
|
|
if err := w.SavePretty(); err != nil {
|
2021-11-29 12:48:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:58:21 +00:00
|
|
|
func openContractWallet(v *viper.Viper, cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) {
|
2022-04-08 14:40:51 +00:00
|
|
|
p := filepath.Join(walletDir, contractWalletFilename)
|
2021-11-29 12:33:46 +00:00
|
|
|
w, err := wallet.NewWalletFromFile(p)
|
|
|
|
if err != nil {
|
2021-11-29 12:48:06 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("can't open wallet: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:40:51 +00:00
|
|
|
cmd.Printf("Contract group wallet is missing, initialize at %s\n", p)
|
2022-04-08 14:58:21 +00:00
|
|
|
return initializeContractWallet(v, walletDir)
|
2021-11-29 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 14:58:21 +00:00
|
|
|
password, err := config.GetPassword(v, contractWalletPasswordKey)
|
2022-02-08 10:41:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-29 12:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range w.Accounts {
|
|
|
|
if err := w.Accounts[i].Decrypt(password, keys.NEP2ScryptParams()); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unlock wallet: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) addManifestGroup(h util.Uint160, cs *contractState) error {
|
|
|
|
priv := c.ContractWallet.Accounts[0].PrivateKey()
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
|
|
|
|
sig := priv.Sign(h.BytesBE())
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for i := range cs.Manifest.Groups {
|
|
|
|
if cs.Manifest.Groups[i].PublicKey.Equal(pub) {
|
|
|
|
cs.Manifest.Groups[i].Signature = sig
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
cs.Manifest.Groups = append(cs.Manifest.Groups, manifest.Group{
|
|
|
|
PublicKey: pub,
|
|
|
|
Signature: sig,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(cs.Manifest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.RawManifest = data
|
|
|
|
return nil
|
|
|
|
}
|