• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang btcutil.Address类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/conformal/btcutil.Address的典型用法代码示例。如果您正苦于以下问题:Golang Address类的具体用法?Golang Address怎么用?Golang Address使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Address类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: CalculateAddressBalance

// CalculateAddressBalance sums the amounts of all unspent transaction
// outputs to a single address's pubkey hash and returns the balance
// as a float64.
//
// If confirmations is 0, all UTXOs, even those not present in a
// block (height -1), will be used to get the balance.  Otherwise,
// a UTXO must be in a block.  If confirmations is 1 or greater,
// the balance will be calculated based on how many how many blocks
// include a UTXO.
func (a *Account) CalculateAddressBalance(addr btcutil.Address, confirms int) float64 {
	bs, err := GetCurBlock()
	if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
		return 0.
	}

	var bal btcutil.Amount
	unspent, err := a.TxStore.UnspentOutputs()
	if err != nil {
		return 0.
	}
	for _, credit := range unspent {
		if credit.Confirmed(confirms, bs.Height) {
			// We only care about the case where len(addrs) == 1, and err
			// will never be non-nil in that case
			_, addrs, _, _ := credit.Addresses(activeNet.Params)
			if len(addrs) != 1 {
				continue
			}
			if addrs[0].EncodeAddress() == addr.EncodeAddress() {
				bal += credit.Amount()
			}
		}
	}
	return bal.ToUnit(btcutil.AmountBTC)
}
开发者ID:kingpro,项目名称:btcwallet,代码行数:35,代码来源:account.go


示例2: AddressUsed

// AddressUsed returns whether there are any recorded transactions spending to
// a given address.  Assumming correct TxStore usage, this will return true iff
// there are any transactions with outputs to this address in the blockchain or
// the btcd mempool.
func (a *Account) AddressUsed(addr btcutil.Address) bool {
	// This not only can be optimized by recording this data as it is
	// read when opening an account, and keeping it up to date each time a
	// new received tx arrives, but it probably should in case an address is
	// used in a tx (made public) but the tx is eventually removed from the
	// store (consider a chain reorg).

	pkHash := addr.ScriptAddress()

	for _, r := range a.TxStore.Records() {
		credits := r.Credits()
		for _, c := range credits {
			// Extract addresses from this output's pkScript.
			_, addrs, _, err := c.Addresses(cfg.Net())
			if err != nil {
				continue
			}

			for _, a := range addrs {
				if bytes.Equal(a.ScriptAddress(), pkHash) {
					return true
				}
			}
		}
	}
	return false
}
开发者ID:GeertJohan,项目名称:btcwallet,代码行数:31,代码来源:account.go


示例3: MarkAddressForAccount

// MarkAddressForAccount labels the given account as containing the provided
// address.
func (am *AccountManager) MarkAddressForAccount(address btcutil.Address,
	account *Account) {
	// TODO(oga) really this entire dance should be carried out implicitly
	// instead of requiring explicit messaging from the account to the
	// manager.
	am.cmdChan <- &markAddressForAccountCmd{
		address: address.EncodeAddress(),
		account: account,
	}
}
开发者ID:GeertJohan,项目名称:btcwallet,代码行数:12,代码来源:acctmgr.go


示例4: AccountByAddress

// AccountByAddress returns the account specified by address, or
// ErrNotFound as an error if the account is not found.
func (am *AccountManager) AccountByAddress(addr btcutil.Address) (*Account,
	error) {
	respChan := make(chan *Account)
	am.cmdChan <- &accessAccountByAddressRequest{
		address: addr.EncodeAddress(),
		resp:    respChan,
	}
	resp := <-respChan
	if resp == nil {
		return nil, ErrNotFound
	}
	return resp, nil
}
开发者ID:GeertJohan,项目名称:btcwallet,代码行数:15,代码来源:acctmgr.go


示例5: AccountByAddress

// AccountByAddress returns the account specified by address, or
// ErrNotFound as an error if the account is not found.
func (am *AccountManager) AccountByAddress(addr btcutil.Address) (*Account, error) {
	respChan := make(chan *Account)
	req := accessAccountByAddressRequest{
		address: addr.EncodeAddress(),
		resp:    respChan,
	}
	select {
	case am.cmdChan <- &req:
		resp := <-respChan
		if resp == nil {
			return nil, ErrNotFound
		}
		return resp, nil
	case <-am.quit:
		return nil, ErrNoAccounts
	}
}
开发者ID:kingpro,项目名称:btcwallet,代码行数:19,代码来源:acctmgr.go


示例6: PayToAddrScript

// PayToAddrScript creates a new script to pay a transaction output to a the
// specified address.  Currently the only supported address types are
// btcutil.AddressPubKeyHash and btcutil.AddressScriptHash.
func PayToAddrScript(addr btcutil.Address) ([]byte, error) {
	switch addr := addr.(type) {
	case *btcutil.AddressPubKeyHash:
		if addr == nil {
			return nil, ErrUnsupportedAddress
		}
		return PayToPubKeyHashScript(addr.ScriptAddress())

	case *btcutil.AddressScriptHash:
		if addr == nil {
			return nil, ErrUnsupportedAddress
		}
		return PayToScriptHashScript(addr.ScriptAddress())
	}

	return nil, ErrUnsupportedAddress
}
开发者ID:hsk81,项目名称:btcscript,代码行数:20,代码来源:script.go


示例7: AddressUsed

// AddressUsed returns whether there are any recorded transactions spending to
// a given address.  Assumming correct TxStore usage, this will return true iff
// there are any transactions with outputs to this address in the blockchain or
// the btcd mempool.
func (a *Account) AddressUsed(addr btcutil.Address) bool {
	// This can be optimized by recording this data as it is read when
	// opening an account, and keeping it up to date each time a new
	// received tx arrives.

	pkHash := addr.ScriptAddress()

	for i := range a.TxStore {
		rtx, ok := a.TxStore[i].(*tx.RecvTx)
		if !ok {
			continue
		}

		if bytes.Equal(rtx.ReceiverHash, pkHash) {
			return true
		}
	}
	return false
}
开发者ID:kawalgrover,项目名称:btcwallet,代码行数:23,代码来源:account.go


示例8: AddressUsed

// AddressUsed returns whether there are any recorded transactions spending to
// a given address.  Assumming correct TxStore usage, this will return true iff
// there are any transactions with outputs to this address in the blockchain or
// the btcd mempool.
func (a *Account) AddressUsed(addr btcutil.Address) bool {
	// This not only can be optimized by recording this data as it is
	// read when opening an account, and keeping it up to date each time a
	// new received tx arrives, but it probably should in case an address is
	// used in a tx (made public) but the tx is eventually removed from the
	// store (consider a chain reorg).

	pkHash := addr.ScriptAddress()

	for _, r := range a.TxStore.Records() {
		credits := r.Credits()
		for _, c := range credits {
			// Errors don't matter here.  If addrs is nil, the
			// range below does nothing.
			_, addrs, _, _ := c.Addresses(activeNet.Params)
			for _, a := range addrs {
				if bytes.Equal(a.ScriptAddress(), pkHash) {
					return true
				}
			}
		}
	}
	return false
}
开发者ID:kingpro,项目名称:btcwallet,代码行数:28,代码来源:account.go


示例9: TestAddresses

func TestAddresses(t *testing.T) {
	tests := []struct {
		name      string
		addr      string
		valid     bool
		canDecode bool
		result    btcutil.Address
		f         func() (btcutil.Address, error)
		net       btcwire.BitcoinNet
	}{
		// Positive P2PKH tests.
		{
			name:      "mainnet p2pkh",
			addr:      "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gX",
			valid:     true,
			canDecode: true,
			result: btcutil.TstAddressPubKeyHash(
				[ripemd160.Size]byte{
					0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
					0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84},
				btcwire.MainNet),
			f: func() (btcutil.Address, error) {
				pkHash := []byte{
					0xe3, 0x4c, 0xce, 0x70, 0xc8, 0x63, 0x73, 0x27, 0x3e, 0xfc,
					0xc5, 0x4c, 0xe7, 0xd2, 0xa4, 0x91, 0xbb, 0x4a, 0x0e, 0x84}
				return btcutil.NewAddressPubKeyHash(pkHash, btcwire.MainNet)
			},
			net: btcwire.MainNet,
		},
		{
			name:      "mainnet p2pkh 2",
			addr:      "12MzCDwodF9G1e7jfwLXfR164RNtx4BRVG",
			valid:     true,
			canDecode: true,
			result: btcutil.TstAddressPubKeyHash(
				[ripemd160.Size]byte{
					0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
					0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa},
				btcwire.MainNet),
			f: func() (btcutil.Address, error) {
				pkHash := []byte{
					0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b, 0xf4,
					0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad, 0xaa}
				return btcutil.NewAddressPubKeyHash(pkHash, btcwire.MainNet)
			},
			net: btcwire.MainNet,
		},
		{
			name:      "testnet p2pkh",
			addr:      "mrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
			valid:     true,
			canDecode: true,
			result: btcutil.TstAddressPubKeyHash(
				[ripemd160.Size]byte{
					0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
					0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f},
				btcwire.TestNet3),
			f: func() (btcutil.Address, error) {
				pkHash := []byte{
					0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
					0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f}
				return btcutil.NewAddressPubKeyHash(pkHash, btcwire.TestNet3)
			},
			net: btcwire.TestNet3,
		},

		// Negative P2PKH tests.
		{
			name:      "p2pkh wrong byte identifier/net",
			addr:      "MrX9vMRYLfVy1BnZbc5gZjuyaqH3ZW2ZHz",
			valid:     false,
			canDecode: true,
			f: func() (btcutil.Address, error) {
				pkHash := []byte{
					0x78, 0xb3, 0x16, 0xa0, 0x86, 0x47, 0xd5, 0xb7, 0x72, 0x83,
					0xe5, 0x12, 0xd3, 0x60, 0x3f, 0x1f, 0x1c, 0x8d, 0xe6, 0x8f}
				return btcutil.NewAddressPubKeyHash(pkHash, btcwire.TestNet)
			},
		},
		{
			name:      "p2pkh wrong hash length",
			addr:      "",
			valid:     false,
			canDecode: true,
			f: func() (btcutil.Address, error) {
				pkHash := []byte{
					0x00, 0x0e, 0xf0, 0x30, 0x10, 0x7f, 0xd2, 0x6e, 0x0b, 0x6b,
					0xf4, 0x05, 0x12, 0xbc, 0xa2, 0xce, 0xb1, 0xdd, 0x80, 0xad,
					0xaa}
				return btcutil.NewAddressPubKeyHash(pkHash, btcwire.MainNet)
			},
		},
		{
			name:      "p2pkh bad checksum",
			addr:      "1MirQ9bwyQcGVJPwKUgapu5ouK2E2Ey4gY",
			valid:     false,
			canDecode: true,
		},

		// Positive P2SH tests.
//.........这里部分代码省略.........
开发者ID:rivercheng,项目名称:btcutil,代码行数:101,代码来源:address_test.go



注:本文中的github.com/conformal/btcutil.Address类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang btcutil.Block类代码示例发布时间:2022-05-23
下一篇:
Golang btcutil.NewTx函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap