func NewPrivateKeyFromHexBytes(privKeybytes []byte) *PrivateKey { pk := new(PrivateKey) pk.AllocateNew() copy(pk.Key[:], privKeybytes) pk.Pub.UnmarshalBinary(ed25519.GetPublicKey(pk.Key)[:]) return pk }
// New Transaction: key -- // We create a new transaction, and track it with the user supplied key. The // user can then use this key to make subsequent calls to add inputs, outputs, // and to sign. Then they can submit the transaction. // // When the transaction is submitted, we clear it from our working memory. // Multiple transactions can be under construction at one time, but they need // their own keys. Once a transaction is either submitted or deleted, the key // can be reused. func (AddressFromWords) Execute(state IState, args []string) error { if len(args) != 14 { return fmt.Errorf("Invalid Parameters") } name := args[1] na := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(name)) if na != nil { return fmt.Errorf("The name %s is already in use. Names must be unique.", name) } var mnstr string for i := 2; i < 14; i++ { if len(args[i]) == 0 { return fmt.Errorf("Invalid mnemonic; the %d word has issues", i+1) } mnstr = mnstr + args[i] + " " } privateKey, err := wallet.MnemonicStringToPrivateKey(mnstr) if err != nil { return err } var fixed [64]byte copy(fixed[:], privateKey) publicKey := ed25519.GetPublicKey(&fixed) state.GetFS().GetWallet().AddKeyPair("fct", []byte(name), publicKey[:], fixed[:], false) return nil }
// This function pulls the next private key from the deterministic // private key generator, gets the public key associated with it // then prepares the generator for the next time a private key is needed. // To prepare the next state, it sha512s the previous sha512 output. // It returns a 32 byte public key, a 64 byte private key, and an error condition. // The private key is the SUPERCOP style with the private key in the first 32 bytes // and the public key is the last 32 bytes. // The public key essentially returns twice because of this. func (w *SCWallet) generateKey() (public []byte, private []byte, err error) { keypair := new([64]byte) // the secret part of the keypair is the top 32 bytes of the sha512 hash copy(keypair[:32], w.GetSeed()[:32]) // the crypto library puts the pubkey in the lower 32 bytes and returns the same 32 bytes. pub := ed25519.GetPublicKey(keypair) return pub[:], keypair[:], err }
func MakeFEREntryWithHeightFromContent(passedResidentHeight uint32, passedTargetActivationHeight uint32, passedTargetPrice uint64, passedExpirationHeight uint32, passedPriority uint32) *FEREntryWithHeight { // Create and format the signing private key var signingPrivateKey [64]byte SigningPrivateKey := "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" signingBytes, err := hex.DecodeString(SigningPrivateKey) if err != nil { fmt.Println("Signing private key isn't parsable") return nil } copy(signingPrivateKey[:], signingBytes[:]) _ = ed.GetPublicKey(&signingPrivateKey) // Needed to format the public half of the key set anFEREntry := new(specialEntries.FEREntry) anFEREntry.SetExpirationHeight(passedExpirationHeight) anFEREntry.SetTargetActivationHeight(passedTargetActivationHeight) anFEREntry.SetPriority(passedPriority) anFEREntry.SetTargetPrice(passedTargetPrice) entryJson, err := json.Marshal(anFEREntry) if err != nil { fmt.Println("Bad marshal of anFEREntry") return nil } // Create the factom entry with the signing private key signingSignature := ed.Sign(&signingPrivateKey, entryJson) // Make a new factom entry and populate it anEntry := new(factom.Entry) anEntry.ChainID = "111111118d918a8be684e0dac725493a75862ef96d2d3f43f84b26969329bf03" anEntry.ExtIDs = append(anEntry.ExtIDs, signingSignature[:]) anEntry.Content = entryJson // ce := common.NewEntry() emb, _ := anEntry.MarshalBinary() // ce.UnmarshalBinary(emb) EBEntry := entryBlock.NewEntry() _, err = EBEntry.UnmarshalBinaryData(emb) if err != nil { fmt.Println("Error 3: couldn't unmarshal binary") return nil } ewh := new(FEREntryWithHeight) // Don't set the resident height in the actual FEREntry yet because the state validate loop will handle all that ewh.Height = passedResidentHeight ewh.AnFEREntry = EBEntry return ewh }
func Sign(priv, data []byte) []byte { priv2 := [64]byte{} if len(priv) == 64 { copy(priv2[:], priv[:]) } else if len(priv) == 32 { copy(priv2[:], priv[:]) pub := ed25519.GetPublicKey(&priv2) copy(priv2[:], append(priv, pub[:]...)[:]) } else { return nil } return ed25519.Sign(&priv2, data)[:constants.SIGNATURE_LENGTH] }
func GenerateKeyFromPrivateKey(privateKey []byte) (public []byte, private []byte, err error) { if len(privateKey) == 64 { privateKey = privateKey[:32] } if len(privateKey) != 32 { return nil, nil, fmt.Errorf("Wrong privateKey size") } keypair := new([64]byte) copy(keypair[:32], privateKey[:]) // the crypto library puts the pubkey in the lower 32 bytes and returns the same 32 bytes. pub := ed25519.GetPublicKey(keypair) return pub[:], keypair[:], err }
// ImportKey <name> <private key> func (ImportKey) Execute(state IState, args []string) error { if len(args) != 3 { return fmt.Errorf("Invalid Parameters") } name := args[1] adr := args[2] if err := ValidName(name); err != nil { return err } a := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(name)) if a != nil { return fmt.Errorf("That address name is in use. Specify a different name.") } fa := fct.ValidateFPrivateUserStr(adr) ec := fct.ValidateECPrivateUserStr(adr) b, err := hex.DecodeString(adr) if err == nil && len(b) != 32 { err = fmt.Errorf("wrong length") } if fa || ec || err == nil { var privateKey []byte if err != nil { privateKey = fct.ConvertUserStrToAddress(adr) } else { privateKey = b } var fixed [64]byte copy(fixed[:], privateKey) publicKey := ed25519.GetPublicKey(&fixed) adrtype := "ec" if fa { adrtype = "fct" } _, err := state.GetFS().GetWallet().AddKeyPair(adrtype, []byte(name), publicKey[:], privateKey, false) if err != nil { return err } return nil } return fmt.Errorf("Not a valid Private Key; Check that your key is typed correctly") }
func PrivateKeyToEDPub(priv []byte) []byte { priv2 := new([ed25519.PrivateKeySize]byte) copy(priv2[:], priv) pub := ed25519.GetPublicKey(priv2) return pub[:] }