// Returns the LedgerMR for this block. func (b *FBlock) GetLedgerMR() fct.IHash { b.EndOfPeriod(0) // Clean up end of minute markers, if needed. hashes := make([]fct.IHash, 0, len(b.Transactions)) marker := 0 for i, trans := range b.Transactions { for marker < len(b.endOfPeriod) && i != 0 && i == b.endOfPeriod[marker] { marker++ hashes = append(hashes, fct.Sha(fct.ZERO)) } data, err := trans.MarshalBinarySig() hash := fct.Sha(data) if err != nil { panic("Failed to get LedgerMR: " + err.Error()) } hashes = append(hashes, hash) } // Add any lagging markers for marker < len(b.endOfPeriod) { marker++ hashes = append(hashes, fct.Sha(fct.ZERO)) } lmr := fct.ComputeMerkleRoot(hashes) return lmr }
// Calculates the Key Merkle Root for this block and returns it. func (b *FBlock) GetKeyMR() fct.IHash { bodyMR := b.GetBodyMR() data, err := b.MarshalHeader() if err != nil { panic("Failed to create KeyMR: " + err.Error()) } headerHash := fct.Sha(data) cat := append(headerHash.Bytes(), bodyMR.Bytes()...) kmr := fct.Sha(cat) return kmr }
// Calculates the Key Merkle Root for this block and returns it. func (b *FBlock) GetLedgerKeyMR() fct.IHash { ledgerMR := b.GetLedgerMR() data, err := b.MarshalHeader() if err != nil { panic("Failed to create LedgerKeyMR: " + err.Error()) } headerHash := fct.Sha(data) cat := append(ledgerMR.Bytes(), headerHash.Bytes()...) lkmr := fct.Sha(cat) return lkmr }
func Test_bolt_init(t *testing.T) { db := new(BoltDB) bucketList := make([][]byte, 5, 5) bucketList[0] = []byte("one") bucketList[1] = []byte("two") bucketList[2] = []byte("three") bucketList[3] = []byte("four") bucketList[4] = []byte("five") instances := make(map[[fct.ADDRESS_LENGTH]byte]fct.IBlock) { var a fct.IBlock a = new(fct.Address) instances[cp(a.GetDBHash())] = a a = new(fct.Hash) instances[cp(a.GetDBHash())] = a a = new(fct.InAddress) instances[cp(a.GetDBHash())] = a a = new(fct.OutAddress) instances[cp(a.GetDBHash())] = a a = new(fct.OutECAddress) instances[cp(a.GetDBHash())] = a a = new(fct.RCD_1) instances[cp(a.GetDBHash())] = a a = new(fct.RCD_2) instances[cp(a.GetDBHash())] = a a = new(fct.Signature) instances[cp(a.GetDBHash())] = a a = new(fct.Transaction) instances[cp(a.GetDBHash())] = a } db.Init(bucketList, instances) a := new(fct.Address) a.SetBytes(fct.Sha([]byte("I came, I saw")).Bytes()) db.Put("one", fct.Sha([]byte("one")), a) r := db.Get("one", fct.Sha([]byte("one"))) if a.IsEqual(r) != nil { t.Fail() } db.DeleteKey([]byte("one"), fct.Sha([]byte("one")).Bytes()) r = db.Get("one", fct.Sha([]byte("one"))) if r != nil { t.Fail() } }
func (r Setup) Execute(state IState, args []string) error { // Make sure we have a seed. if len(args) != 2 { msg := "You must supply some random seed. For example (don't use this!)\n" + "factom-cli setup 'woe!#in31!%234ng)%^&$%oeg%^&*^jp45694a;gmr@#t4 q34y'\n" + "would make a nice seed. The more random the better.\n\n" + "Note that if you create an address before you call Setup, you must\n" + "use those address(s) as you access the fountians." return fmt.Errorf(msg) } setFountian := false keys, _ := state.GetFS().GetWallet().GetDB().GetKeysValues([]byte(fct.W_NAME)) if len(keys) == 0 { setFountian = true for i := 1; i <= 10; i++ { name := fmt.Sprintf("%02d-Fountain", i) err := GenAddress(state, "fct", name) if err != nil { fmt.Println(err) return nil } } } seedprime := fct.Sha([]byte(fmt.Sprintf("%s%v", args[1], time.Now().UnixNano()))).Bytes() NewSeed(state, seedprime) if setFountian { fmt.Println("New seed set, fountain addresses defined") } else { fmt.Println("New seed set, no fountain addresses defined") } return nil }
func Test_Replay(test *testing.T) { type mh struct { hash []byte time int64 } var h [5000]*mh for i := 0; i < 5000; i++ { h[i] = new(mh) h[i].hash = fct.Sha([]byte(fmt.Sprintf("h%d", i))).Bytes() h[i].time = now + (rand.Int63() % 24 * hour) - 12*hour if !IsTSValid_(h[i].hash, h[i].time, now) { fmt.Println("Failed Test ", i, "first") test.Fail() return } if IsTSValid_(h[i].hash, h[i].time, now) { fmt.Println("Failed Test ", i, "second") test.Fail() return } now += rand.Int63() % hour for j := 0; j < i; j++ { if IsTSValid_(h[i].hash, h[i].time, hour) { fmt.Println("Failed Test ", i, j, "repeat") test.Fail() return } } } }
func HandleFactoidSetup(ctx *web.Context, seed string) { // Make sure we have a seed. if len(seed) == 0 { msg := "You must supply some random seed. For example (don't use this!)\n" + "factom-cli setup 'woe!#in31!%234ng)%^&$%oeg%^&*^jp45694a;gmr@#t4 q34y'\n" + "would make a nice seed. The more random the better.\n\n" + "Note that if you create an address before you call Setup, you must\n" + "use those address(s) as you access the fountians." reportResults(ctx, msg, false) } setFountian := false keys, _ := Wallet.GetWalletNames() if len(keys) == 0 { setFountian = true for i := 1; i <= 10; i++ { name := fmt.Sprintf("%02d-Fountain", i) _, err := Wallet.GenerateFctAddress([]byte(name), 1, 1) if err != nil { reportResults(ctx, err.Error(), false) return } } } seedprime := fct.Sha([]byte(fmt.Sprintf("%s%v", seed, time.Now().UnixNano()))).Bytes() Wallet.NewSeed(seedprime) if setFountian { reportResults(ctx, "New seed set, fountain addresses defined", true) } else { reportResults(ctx, "New seed set, no fountain addresses defined", true) } }
func Test_Replay(test *testing.T) { type mh struct { hash []byte time int64 } XTrans := 15000 h := make([]*mh, XTrans) start := now for i := 0; i < XTrans; i++ { // We are going to remember some large set of transactions. h[i] = new(mh) h[i].hash = fct.Sha([]byte(fmt.Sprintf("h%d", i))).Bytes() // Build a valid transaction somewhere +/- 12 hours of now h[i].time = now + (rand.Int63() % 24 * hour) - 12*hour // The first time we test, it should be valid. if !IsTSValid_(h[i].hash, h[i].time, now) { fmt.Println("Failed Test ", i, "first") test.Fail() return } // An immediate replay! Should fail! if IsTSValid_(h[i].hash, h[i].time, now) { fmt.Println("Failed Test ", i, "second") test.Fail() return } // Move time forward somewhere between 0 to 15 minutes now += rand.Int63() % hour / 4 // Now replay all the transactions we have collected. NONE of them // should work. for j := 0; j < i; j++ { if IsTSValid_(h[i].hash, h[i].time, hour) { fmt.Println("Failed Test ", i, j, "repeat") test.Fail() return } } } fmt.Println("Simulation ran from", time.Unix(start, 0), "to", time.Unix(now, 0)) }
func (b *FBlock) GetBodyMR() fct.IHash { b.EndOfPeriod(0) // Clean up end of minute markers, if needed. hashes := make([]fct.IHash, 0, len(b.Transactions)) marker := 0 for i, trans := range b.Transactions { for marker < len(b.endOfPeriod) && i != 0 && i == b.endOfPeriod[marker] { marker++ hashes = append(hashes, fct.Sha(fct.ZERO)) } hashes = append(hashes, trans.GetHash()) } // Add any lagging markers for marker < len(b.endOfPeriod) { marker++ hashes = append(hashes, fct.Sha(fct.ZERO)) } b.BodyMR = fct.ComputeMerkleRoot(hashes) return b.BodyMR }
func Test_Auth1_Equals(test *testing.T) { scd := new(MapDB) // Get me a database scd.Init() ecAdr := factoid.Sha([]byte("ec one")) // Get me an address b := new(t_balance) // Get a balance IBlock b.balance = 1000 // Set the balance scd.Put("ec", ecAdr, b) // Write balance to db b2 := scd.Get("ec", ecAdr) // Get it back. if b.balance != b2.(*t_balance).balance { // Make sure we got it back. test.Fail() } }
func main() { // Get a wallet wallet := new(wallet.SCWallet) // make me a wallet wallet.Init() // Generate a Random Seed seed := fct.Sha([]byte(fmt.Sprintf("asdfjkoergipupdiofbd;;aerled: %v", time.Now().UnixNano()))).Bytes() // Randomize the address generation. This should be very random, and destroyed for security wallet.NewSeed(seed) addr, err := wallet.GenerateECAddress([]byte("dan")) if err != nil { fmt.Println(err) return } we := wallet.GetAddressDetailsAddr(addr.Bytes()) pub := we.GetKey(0) pri := we.GetPrivKey(0) fmt.Printf("Public Key: %x\n", pub) fmt.Printf("Private Key: %x\n %x\n", pri[:]) }
func newFakeAddr() sc.IAddress { fakeAddr = sc.Sha(fakeAddr.Bytes()) return fakeAddr }
func (FSbalance) GetDBHash() fct.IHash { return fct.Sha([]byte("FSbalance")) }
func (FBlock) GetDBHash() fct.IHash { return fct.Sha([]byte("FBlock")) }
func (SCWallet) GetDBHash() fct.IHash { return fct.Sha([]byte("SCWallet")) }
func (b ByteStore) GetHash() fct.IHash { return fct.Sha(b.byteData) }
func (w1 WalletEntry) GetDBHash() fct.IHash { return fct.Sha([]byte("WalletEntry")) }
func (ByteStore) GetDBHash() fct.IHash { return fct.Sha([]byte("ByteStore")) }