本文整理汇总了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;未经允许,请勿转载。 |
请发表评论