package main import ( "bufio" "fmt" "os" "github.com/HoosatNetwork/HTND/v2/cmd/htnwallet/libhtnwallet" "github.com/HoosatNetwork/HTND/v2/cmd/htnwallet/libhtnwallet/bip32" "github.com/HoosatNetwork/HTND/v2/cmd/htnwallet/utils" "github.com/pkg/errors" "github.com/HoosatNetwork/HTND/v2/cmd/htnwallet/keys" ) func create(conf *createConfig) error { var encryptedMnemonics []*keys.EncryptedMnemonic var signerExtendedPublicKeys []string var mldsa44KeyPools map[string]*keys.MLDSA44KeyPool var err error isMultisig := conf.NumPublicKeys > 1 if !conf.Import { encryptedMnemonics, signerExtendedPublicKeys, mldsa44KeyPools, err = keys.CreateMnemonics(conf.NetParams(), conf.NumPrivateKeys, conf.Password, isMultisig) } else { encryptedMnemonics, signerExtendedPublicKeys, mldsa44KeyPools, err = keys.ImportMnemonics(conf.NetParams(), conf.NumPrivateKeys, conf.Password, isMultisig) } if err != nil { return err } for i, extendedPublicKey := range signerExtendedPublicKeys { fmt.Printf("Extended public key of mnemonic #%d:\n%s\n\n", i+1, extendedPublicKey) } fmt.Printf("Notice the above is neither a secret key to your wallet " + "(use \"htnwallet dump-unencrypted-data\" to see a secret seed phrase) " + "nor a wallet public address (use \"htnwallet new-address\" to create and see one)\n\n") extendedPublicKeys := make([]string, conf.NumPrivateKeys, conf.NumPublicKeys) copy(extendedPublicKeys, signerExtendedPublicKeys) reader := bufio.NewReader(os.Stdin) for i := conf.NumPrivateKeys; i < conf.NumPublicKeys; i++ { fmt.Printf("Enter public key #%d here:\n", i+1) extendedPublicKey, err := utils.ReadLine(reader) if err != nil { return err } _, err = bip32.DeserializeExtendedKey(string(extendedPublicKey)) if err != nil { return errors.Wrapf(err, "%s is invalid extended public key", string(extendedPublicKey)) } fmt.Println() extendedPublicKeys = append(extendedPublicKeys, string(extendedPublicKey)) } // For a read only wallet the cosigner index is 0 cosignerIndex := uint32(0) if len(signerExtendedPublicKeys) > 0 { cosignerIndex, err = libhtnwallet.MinimumCosignerIndex(signerExtendedPublicKeys, extendedPublicKeys) if err != nil { return err } } file := keys.File{ Version: keys.LastVersion, EncryptedMnemonics: encryptedMnemonics, ExtendedPublicKeys: extendedPublicKeys, MinimumSignatures: conf.MinimumSignatures, CosignerIndex: cosignerIndex, ECDSA: conf.ECDSA, } supportsMLDSA44Multisig := libhtnwallet.SupportsMLDSA44Multisig(len(extendedPublicKeys), conf.MinimumSignatures) if isMultisig { file.MLDSA44Cosigners = mldsa44KeyPools } else if len(signerExtendedPublicKeys) == 1 { file.MLDSA44 = mldsa44KeyPools[signerExtendedPublicKeys[0]] } if supportsMLDSA44Multisig { fmt.Printf("For ML-DSA-44 multisig addresses, every cosigner has to import the others' ML-DSA-44 keys: "+ "run \"htnwallet %s\" here and \"htnwallet %s\" with the other cosigners' files\n\n", exportMLDSA44KeysSubCmd, importMLDSA44KeysSubCmd) } err = file.SetPath(conf.NetParams(), conf.KeysFile, conf.Yes) if err != nil { return err } err = file.TryLock() if err != nil { return err } err = file.Save() if err != nil { return err } fmt.Printf("Wrote the keys into %s\n", file.Path()) if conf.Import && supportsMLDSA44Multisig { fmt.Printf("Newly generated cosigner pools initially cover address indexes below %d. ML-DSA-44-only history "+ "beyond the imported pools cannot reveal the missing indexes. Use longer pools from backup, or have "+ "each cosigner run \"htnwallet %s --count \" with n above the highest index used and exchange the "+ "longer pools with \"htnwallet %s\" and \"htnwallet %s\".\n", libhtnwallet.DefaultMLDSA44KeyPoolSize, generateMLDSA44KeysSubCmd, exportMLDSA44KeysSubCmd, importMLDSA44KeysSubCmd) } else if conf.Import && file.MLDSA44 != nil { fmt.Printf("The wallet holds ML-DSA-44 keys for address indexes below %d. If it used ML-DSA-44 addresses at "+ "higher indexes before, run \"htnwallet %s --count \" with n above the highest index it used: coins "+ "on those addresses are not shown until then.\n", file.MLDSA44.Size(), generateMLDSA44KeysSubCmd) } return nil }