func TestSignature(t *testing.T) {
	pub, priv, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Error(err)
	}
	t.Logf("priv, pub - %x, %x", *priv, *pub)

	testData := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}

	sig := ed25519.Sign(priv, testData)

	ok := ed25519.Verify(pub, testData, sig)

	if ok == false {
		t.Error("Signature could not be verified")
	}

	pub2, err := primitives.PrivateKeyToPublicKey(priv[:])
	if err != nil {
		t.Error(err)
	}

	t.Logf("pub1 - %x", pub)
	t.Logf("pub2 - %x", pub2)

	if primitives.AreBytesEqual(pub[:], pub2[:]) == false {
		t.Error("Public keys are not equal")
	}
}
func newCommitChain() *CommitChainMsg {
	msg := new(CommitChainMsg)

	cc := entryCreditBlock.NewCommitChain()

	cc.Version = 0x11
	cc.MilliTime = (*primitives.ByteSlice6)(&[6]byte{1, 1, 1, 1, 1, 1})
	p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
	cc.ChainIDHash.SetBytes(p)
	p, _ = hex.DecodeString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
	cc.Weld.SetBytes(p)
	p, _ = hex.DecodeString("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
	cc.EntryHash.SetBytes(p)
	cc.Credits = 11

	// make a key and sign the msg
	if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
		panic(err)
	} else {
		cc.ECPubKey = (*primitives.ByteSlice32)(pub)
		cc.Sig = (*primitives.ByteSlice64)(ed.Sign(privkey, cc.CommitMsg()))
	}

	msg.CommitChain = cc
	//msg.Timestamp = primitives.NewTimestampNow()

	return msg
}
Exemple #3
0
// Sign signs msg with PrivateKey and return Signature
func (pk *PrivateKey) Sign(msg []byte) (sig interfaces.IFullSignature) {
	sig = new(Signature)
	sig.SetPub(pk.Pub[:])
	s := ed25519.Sign(pk.Key, msg)
	sig.SetSignature(s[:])
	return
}
Exemple #4
0
func (w *SCWallet) SignInputs(trans fct.ITransaction) (bool, error) {

	data, err := trans.MarshalBinarySig() // Get the part of the transaction we sign
	if err != nil {
		return false, err
	}

	var numSigs int = 0

	inputs := trans.GetInputs()
	rcds := trans.GetRCDs()
	for i, rcd := range rcds {
		rcd1, ok := rcd.(*fct.RCD_1)
		if ok {
			pub := rcd1.GetPublicKey()
			we := w.db.GetRaw([]byte(fct.W_ADDRESS_PUB_KEY), pub).(*WalletEntry)
			if we != nil {
				var pri [fct.SIGNATURE_LENGTH]byte
				copy(pri[:], we.private[0])
				bsig := ed25519.Sign(&pri, data)
				sig := new(fct.Signature)
				sig.SetSignature(bsig[:])
				sigblk := new(fct.SignatureBlock)
				sigblk.AddSignature(sig)
				trans.SetSignatureBlock(i, sigblk)
				numSigs += 1
			}
		}
	}

	return numSigs == len(inputs), nil
}
Exemple #5
0
func TestNewED25519Signature(t *testing.T) {
	testData := primitives.Sha([]byte("sig first half  one")).Bytes()
	priv := testHelper.NewPrivKey(0)

	sig := NewED25519Signature(priv, testData)

	pub := testHelper.PrivateKeyToEDPub(priv)
	pub2 := [32]byte{}
	copy(pub2[:], pub)

	s := sig.Signature
	valid := ed25519.VerifyCanonical(&pub2, testData, &s)
	if valid == false {
		t.Errorf("Signature is invalid - %v", valid)
	}

	priv2 := [64]byte{}
	copy(priv2[:], append(priv, pub...)[:])

	sig2 := ed25519.Sign(&priv2, testData)

	valid = ed25519.VerifyCanonical(&pub2, testData, sig2)
	if valid == false {
		t.Errorf("Test signature is invalid - %v", valid)
	}
}
func newCommitEntry() *CommitEntryMsg {
	cem := new(CommitEntryMsg)

	ce := entryCreditBlock.NewCommitEntry()

	// build a CommitEntry for testing
	ce.Version = 0
	ce.MilliTime = (*primitives.ByteSlice6)(&[6]byte{1, 1, 1, 1, 1, 1})
	p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
	ce.EntryHash.SetBytes(p)
	ce.Credits = 1

	// make a key and sign the msg
	if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
		panic(err)
	} else {
		ce.ECPubKey = (*primitives.ByteSlice32)(pub)
		ce.Sig = (*primitives.ByteSlice64)(ed.Sign(privkey, ce.CommitMsg()))
	}

	cem.CommitEntry = ce
	//cem.Timestamp = primitives.NewTimestampNow()

	return cem
}
Exemple #7
0
func createECBlock() *ECBlock {
	ecb1 := NewECBlock().(*ECBlock)

	// build a CommitChain for testing
	cc := NewCommitChain()
	cc.Version = 0
	cc.MilliTime = (*primitives.ByteSlice6)(&[6]byte{1, 1, 1, 1, 1, 1})
	cc.ChainIDHash.SetBytes(byteof(0xaa))
	cc.Weld.SetBytes(byteof(0xbb))
	cc.EntryHash.SetBytes(byteof(0xcc))
	cc.Credits = 11

	// make a key and sign the msg
	if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
		panic(err)
	} else {
		cc.ECPubKey = (*primitives.ByteSlice32)(pub)
		cc.Sig = (*primitives.ByteSlice64)(ed.Sign(privkey, cc.CommitMsg()))
	}

	// create a ECBlock for testing
	ecb1.Header.(*ECBlockHeader).ECChainID.SetBytes(byteof(0x11))
	ecb1.Header.(*ECBlockHeader).BodyHash.SetBytes(byteof(0x22))
	ecb1.Header.(*ECBlockHeader).PrevHeaderHash.SetBytes(byteof(0x33))
	ecb1.Header.(*ECBlockHeader).PrevLedgerKeyMR.SetBytes(byteof(0x44))
	ecb1.Header.(*ECBlockHeader).DBHeight = 10
	ecb1.Header.(*ECBlockHeader).HeaderExpansionArea = byteof(0x55)
	ecb1.Header.(*ECBlockHeader).ObjectCount = 0

	// add the CommitChain to the ECBlock
	ecb1.AddEntry(cc)

	m1 := NewMinuteNumber()
	m1.Number = 0x01
	ecb1.AddEntry(m1)

	// add a ServerIndexNumber
	si1 := NewServerIndexNumber()
	si1.Number = 3
	ecb1.AddEntry(si1)

	// create an IncreaseBalance for testing
	ib := NewIncreaseBalance()
	pub := new(primitives.ByteSlice32)
	copy(pub[:], byteof(0xaa))
	ib.ECPubKey = pub
	ib.TXID.SetBytes(byteof(0xbb))
	ib.NumEC = uint64(13)
	// add the IncreaseBalance
	ecb1.AddEntry(ib)

	m2 := NewMinuteNumber()
	m2.Number = 0x02
	ecb1.AddEntry(m2)

	return ecb1
}
Exemple #8
0
// SignCommit will sign the []byte with the Entry Credit Key and return the
// slice with the signature and pubkey appended.
func (w *SCWallet) SignCommit(we interfaces.IWalletEntry, data []byte) []byte {
	pub := new([constants.ADDRESS_LENGTH]byte)
	copy(pub[:], we.GetKey(0))
	pri := new([constants.PRIVATE_LENGTH]byte)
	copy(pri[:], we.GetPrivKey(0))
	sig := ed25519.Sign(pri, data)
	r := append(data, pub[:]...)
	r = append(r, sig[:]...)

	return r
}
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
}
Exemple #10
0
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 TestCommitChainMarshalUnmarshal(t *testing.T) {
	fmt.Printf("---\nTestCommitChainMarshalUnmarshal\n---\n")

	cc := NewCommitChain()

	// test MarshalBinary on a zeroed CommitChain
	if p, err := cc.MarshalBinary(); err != nil {
		t.Error(err)
	} else if z := make([]byte, CommitChainSize); string(p) != string(z) {
		t.Errorf("Marshal failed on zeroed CommitChain")
	}

	// build a CommitChain for testing
	cc.Version = 0
	cc.MilliTime = (*primitives.ByteSlice6)(&[6]byte{1, 1, 1, 1, 1, 1})
	p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
	cc.ChainIDHash.SetBytes(p)
	p, _ = hex.DecodeString("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
	cc.Weld.SetBytes(p)
	p, _ = hex.DecodeString("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc")
	cc.EntryHash.SetBytes(p)
	cc.Credits = 11

	// make a key and sign the msg
	if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
		t.Error(err)
	} else {
		cc.ECPubKey = (*primitives.ByteSlice32)(pub)
		cc.Sig = (*primitives.ByteSlice64)(ed.Sign(privkey, cc.CommitMsg()))
	}

	// marshal and unmarshal the commit and see if it matches
	cc2 := NewCommitChain()
	p, err := cc.MarshalBinary()
	if err != nil {
		t.Error(err)
	}
	t.Logf("%x\n", p)
	err = cc2.UnmarshalBinary(p)
	if err != nil {
		t.Error(err)
	}

	if !cc2.IsValid() {
		t.Errorf("signature did not match after unmarshalbinary")
	}
}
func TestCommitEntryMarshal(t *testing.T) {
	fmt.Printf("---\nTestCommitEntryMarshal\n---\n")

	ce := common.NewCommitEntry()

	// test MarshalBinary on a zeroed CommitEntry
	if p, err := ce.MarshalBinary(); err != nil {
		t.Error(err)
	} else if z := make([]byte, common.CommitEntrySize); string(p) != string(z) {
		t.Errorf("Marshal failed on zeroed CommitEntry")
	}

	// build a CommitEntry for testing
	ce.Version = 0
	ce.MilliTime = &[6]byte{1, 1, 1, 1, 1, 1}
	p, _ := hex.DecodeString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
	ce.EntryHash.SetBytes(p)
	ce.Credits = 1

	// make a key and sign the msg
	if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
		t.Error(err)
	} else {
		ce.ECPubKey = pub
		ce.Sig = ed.Sign(privkey, ce.CommitMsg())
	}

	// marshal and unmarshal the commit and see if it matches
	ce2 := common.NewCommitEntry()
	if p, err := ce.MarshalBinary(); err != nil {
		t.Error(err)
	} else {
		t.Logf("%x\n", p)
		ce2.UnmarshalBinary(p)
	}

	if !ce2.IsValid() {
		t.Errorf("signature did not match after unmarshalbinary")
	}
}
Exemple #13
0
func (w *SCWallet) SignInputs(trans fct.ITransaction) (bool, error) {

	data, err := trans.MarshalBinarySig() // Get the part of the transaction we sign
	if err != nil {
		return false, err
	}

	var errMsg []byte

	rcds := trans.GetRCDs()
	for i, rcd := range rcds {
		rcd1, ok := rcd.(*fct.RCD_1)
		if ok {
			pub := rcd1.GetPublicKey()
			we, ok := w.db.GetRaw([]byte(fct.W_ADDRESS_PUB_KEY), pub).(*WalletEntry)
			if ok {
				var pri [fct.SIGNATURE_LENGTH]byte
				copy(pri[:], we.private[0])
				bsig := ed25519.Sign(&pri, data)
				sig := new(fct.Signature)
				sig.SetSignature(bsig[:])
				sigblk := new(fct.SignatureBlock)
				sigblk.AddSignature(sig)
				trans.SetSignatureBlock(i, sigblk)
			} else {
				errMsg = append(errMsg,
					[]byte("Do not have the private key for: "+
						fct.ConvertFctAddressToUserStr(fct.NewAddress(pub))+"\n")...)
			}
		}
	}

	if errMsg != nil {
		return false, fmt.Errorf("%s", string(errMsg))
	}
	return true, nil
}
Exemple #14
0
// Sign signs msg with PrivateKey and return Signature
func (pk PrivateKey) Sign(msg []byte) (sig Signature) {
	sig.Pub = pk.Pub
	sig.Sig = ed25519.Sign(pk.Key, msg)
	return
}
func TestECBlockMarshal(t *testing.T) {
	ecb1 := common.NewECBlock()

	// build a CommitChain for testing
	cc := common.NewCommitChain()
	cc.Version = 0
	cc.MilliTime = &[6]byte{1, 1, 1, 1, 1, 1}
	cc.ChainIDHash.SetBytes(byteof(0xaa))
	cc.Weld.SetBytes(byteof(0xbb))
	cc.EntryHash.SetBytes(byteof(0xcc))
	cc.Credits = 11

	// make a key and sign the msg
	if pub, privkey, err := ed.GenerateKey(rand.Reader); err != nil {
		t.Error(err)
	} else {
		cc.ECPubKey = pub
		cc.Sig = ed.Sign(privkey, cc.CommitMsg())
	}

	// create a ECBlock for testing
	ecb1.Header.ECChainID.SetBytes(byteof(0x11))
	ecb1.Header.BodyHash.SetBytes(byteof(0x22))
	ecb1.Header.PrevHeaderHash.SetBytes(byteof(0x33))
	ecb1.Header.PrevLedgerKeyMR.SetBytes(byteof(0x44))
	ecb1.Header.EBHeight = 10
	ecb1.Header.HeaderExpansionArea = byteof(0x55)
	ecb1.Header.ObjectCount = 0

	// add the CommitChain to the ECBlock
	ecb1.AddEntry(cc)

	m1 := common.NewMinuteNumber()
	m1.Number = 0x01
	ecb1.AddEntry(m1)

	// add a ServerIndexNumber
	si1 := common.NewServerIndexNumber()
	si1.Number = 3
	ecb1.AddEntry(si1)

	// create an IncreaseBalance for testing
	ib := common.NewIncreaseBalance()
	pub := new([32]byte)
	copy(pub[:], byteof(0xaa))
	ib.ECPubKey = pub
	ib.TXID.SetBytes(byteof(0xbb))
	ib.NumEC = uint64(13)
	// add the IncreaseBalance
	ecb1.AddEntry(ib)

	m2 := common.NewMinuteNumber()
	m2.Number = 0x02
	ecb1.AddEntry(m2)

	ecb2 := common.NewECBlock()
	if p, err := ecb1.MarshalBinary(); err != nil {
		t.Error(err)
	} else {
		if err := ecb2.UnmarshalBinary(p); err != nil {
			t.Error(err)
		}
		t.Log(spew.Sdump(ecb1))
		t.Log(spew.Sdump(ecb2))
		if q, err := ecb2.MarshalBinary(); err != nil {
			t.Error(err)
		} else if string(p) != string(q) {
			t.Errorf("ecb1 = %x\n", p)
			t.Errorf("ecb2 = %x\n", q)
		}
	}
}