コード例 #1
0
ファイル: balance.go プロジェクト: liudch/gocoin
func do_scan_stealth(p string, ignore_prefix bool) {
	ad, _ := btc.NewAddrFromString(p)
	if ad == nil {
		fmt.Println("Specify base58 encoded bitcoin address")
		return
	}

	sa := ad.StealthAddr
	if sa == nil {
		fmt.Println("Specify base58 encoded stealth address")
		return
	}
	if sa.Version != btc.StealthAddressVersion(common.Testnet) {
		fmt.Println("Incorrect version of the stealth address")
		return
	}
	if len(sa.SpendKeys) != 1 {
		fmt.Println("Currently only single spend keys are supported. This address has", len(sa.SpendKeys))
		return
	}

	//fmt.Println("scankey", hex.EncodeToString(sa.ScanKey[:]))
	if ignore_prefix {
		sa.Prefix = []byte{0}
		fmt.Println("Ignoring Prefix inside the address")
	} else if len(sa.Prefix) == 0 {
		fmt.Println("Prefix not present in the address")
	} else {
		fmt.Println("Prefix", sa.Prefix[0], hex.EncodeToString(sa.Prefix[1:]))
	}

	wallet.FetchStealthKeys()
	d := wallet.FindStealthSecret(sa)
	if d == nil {
		fmt.Println("No matching secret found in your wallet/stealth folder")
		return
	}

	var unsp chain.AllUnspentTx
	var c, spen_exp []byte
	var rec, out *chain.QdbTxOut
	var h160 [20]byte

	common.BlockChain.Unspent.BrowseUTXO(true, func(tx *chain.QdbRec) {
		for i := 0; i < len(tx.Outs)-1; i++ {
			if rec = tx.Outs[i]; rec == nil {
				continue
			}
			if out = tx.Outs[i+1]; out == nil {
				continue
			}
			if !rec.IsStealthIdx() || !out.IsP2KH() || !ad.StealthAddr.CheckNonce(rec.PKScr[3:40]) {
				continue
			}
			c = btc.StealthDH(rec.PKScr[7:40], d)
			spen_exp = btc.DeriveNextPublic(sa.SpendKeys[0][:], c)
			btc.RimpHash(spen_exp, h160[:])
			if bytes.Equal(out.PKScr[3:23], h160[:]) {
				uo := new(chain.OneUnspentTx)
				uo.TxPrevOut.Hash = tx.TxID
				uo.TxPrevOut.Vout = uint32(i + 1)
				uo.Value = out.Value
				uo.MinedAt = tx.InBlock
				uo.BtcAddr = btc.NewAddrFromHash160(h160[:], btc.AddrVerPubkey(common.CFG.Testnet))
				uo.FixDestString()
				uo.BtcAddr.StealthAddr = sa
				uo.BtcAddr.Extra = ad.Extra
				uo.StealthC = c
				unsp = append(unsp, uo)
			}
		}
	})

	sort.Sort(unsp)
	os.RemoveAll("balance")
	os.MkdirAll("balance/", 0770)
	utxt, _ := os.Create("balance/unspent.txt")
	fmt.Print(wallet.DumpBalance(unsp, utxt, true, false))
}
コード例 #2
0
ファイル: balance.go プロジェクト: liudch/gocoin
func list_unspent(addr string) {
	fmt.Println("Checking unspent coins for addr", addr)

	defer func() { // in case if ad.OutScript() would panic
		if r := recover(); r != nil {
			err := r.(error)
			fmt.Println("main panic recovered:", err.Error())
		}
	}()

	var ad *btc.BtcAddr
	var e error
	ad, e = btc.NewAddrFromString(addr)
	if e != nil {
		println(e.Error())
		return
	}
	sa := ad.StealthAddr
	var walk chain.FunctionWalkUnspent
	var unsp chain.AllUnspentTx

	if sa == nil {
		exp_scr := ad.OutScript()
		walk = func(tx *chain.QdbRec) {
			for idx, rec := range tx.Outs {
				if rec != nil && bytes.Equal(rec.PKScr, exp_scr) {
					unsp = append(unsp, tx.ToUnspent(uint32(idx), ad))
				}
			}
		}
	} else {
		var c, spen_exp []byte
		var rec, out *chain.QdbTxOut
		var h160 [20]byte

		wallet.FetchStealthKeys()
		d := wallet.FindStealthSecret(sa)
		if d == nil {
			fmt.Println("No matching secret found in your wallet/stealth folder")
			return
		}
		walk = func(tx *chain.QdbRec) {
			for i := 0; i < len(tx.Outs)-1; i++ {
				if rec = tx.Outs[i]; rec == nil {
					continue
				}
				if out = tx.Outs[i+1]; out == nil {
					continue
				}
				if !rec.IsStealthIdx() || !out.IsP2KH() || !ad.StealthAddr.CheckNonce(rec.PKScr[3:40]) {
					continue
				}
				c = btc.StealthDH(rec.PKScr[7:40], d)
				spen_exp = btc.DeriveNextPublic(sa.SpendKeys[0][:], c)
				btc.RimpHash(spen_exp, h160[:])
				if bytes.Equal(out.PKScr[3:23], h160[:]) {
					uo := new(chain.OneUnspentTx)
					uo.TxPrevOut.Hash = tx.TxID
					uo.TxPrevOut.Vout = uint32(i + 1)
					uo.Value = out.Value
					uo.MinedAt = tx.InBlock
					uo.BtcAddr = btc.NewAddrFromHash160(h160[:], btc.AddrVerPubkey(common.CFG.Testnet))
					uo.FixDestString()
					uo.BtcAddr.StealthAddr = sa
					uo.BtcAddr.Extra = ad.Extra
					uo.StealthC = c
					unsp = append(unsp, uo)
				}
			}
		}
	}
	common.BlockChain.Unspent.BrowseUTXO(false, walk)

	sort.Sort(unsp)
	var sum uint64
	for i := range unsp {
		if len(unsp) < 200 {
			fmt.Println(unsp[i].String())
		}
		sum += unsp[i].Value
	}
	fmt.Printf("Total %.8f unspent BTC in %d outputs at address %s\n",
		float64(sum)/1e8, len(unsp), ad.String())
}
コード例 #3
0
ファイル: balance.go プロジェクト: liudch/gocoin
// This function is only used when loading UTXO database
func newUTXO(tx *chain.QdbRec) (update_wallet bool) {
	var c, spen_exp []byte
	var rec *chain.QdbTxOut
	var h160 [20]byte

check_next_address:
	for idx, out := range tx.Outs {
		if out == nil {
			continue
		}

		// check for stealth
		if len(StealthAdCache) > 0 && idx > 0 {
			if rec = tx.Outs[idx-1]; rec == nil {
				goto not_stealth
			}
			if !rec.IsStealthIdx() || !out.IsP2KH() {
				goto not_stealth
			}

			for _, ad := range StealthAdCache {
				if sa := ad.addr.StealthAddr; sa.CheckNonce(rec.PKScr[3:40]) {
					c = btc.StealthDH(rec.PKScr[7:40], ad.d[:])
					spen_exp = btc.DeriveNextPublic(sa.SpendKeys[0][:], c)
					btc.RimpHash(spen_exp, h160[:])
					if bytes.Equal(out.PKScr[3:23], h160[:]) {
						uo := new(chain.OneUnspentTx)
						uo.TxPrevOut.Hash = tx.TxID
						uo.TxPrevOut.Vout = uint32(idx)
						uo.Value = out.Value
						uo.MinedAt = tx.InBlock
						uo.BtcAddr = btc.NewAddrFromHash160(h160[:], btc.AddrVerPubkey(common.CFG.Testnet))
						uo.FixDestString()
						uo.BtcAddr.StealthAddr = sa
						uo.BtcAddr.Extra = ad.addr.Extra
						uo.StealthC = c

						carec := CachedAddrs[ad.h160]
						carec.Value += uo.Value
						CacheUnspent[carec.CacheIndex].AllUnspentTx = append(CacheUnspent[carec.CacheIndex].AllUnspentTx, uo)
						CacheUnspentIdx[uo.TxPrevOut.UIdx()] = &OneCachedUnspentIdx{Index: carec.CacheIndex, Record: uo}
						if carec.InWallet {
							update_wallet = true
						}
						continue check_next_address // it it was setalth, cannot be enythign else
					}
				}
			}
		}

	not_stealth:
		// Extract hash160 from pkscript
		adr := btc.NewAddrFromPkScript(out.PKScr, common.Testnet)
		if adr != nil {
			if carec, ok := CachedAddrs[adr.Hash160]; ok {
				carec.Value += out.Value
				utxo := new(chain.OneUnspentTx)
				utxo.TxPrevOut.Hash = tx.TxID
				utxo.TxPrevOut.Vout = uint32(idx)
				utxo.Value = out.Value
				utxo.MinedAt = tx.InBlock
				utxo.BtcAddr = CacheUnspent[carec.CacheIndex].BtcAddr
				CacheUnspent[carec.CacheIndex].AllUnspentTx = append(CacheUnspent[carec.CacheIndex].AllUnspentTx, utxo)
				CacheUnspentIdx[utxo.TxPrevOut.UIdx()] = &OneCachedUnspentIdx{Index: carec.CacheIndex, Record: utxo}
				if carec.InWallet {
					update_wallet = true
				}
			}
		}
	}
	return
}