本文整理汇总了Golang中github.com/conformal/btcutil.Address.ScriptAddress方法的典型用法代码示例。如果您正苦于以下问题:Golang Address.ScriptAddress方法的具体用法?Golang Address.ScriptAddress怎么用?Golang Address.ScriptAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/conformal/btcutil.Address
的用法示例。
在下文中一共展示了Address.ScriptAddress方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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
}
示例4: 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
}
示例5: TestAddresses
//.........这里部分代码省略.........
// Decode addr and compare error against valid.
decoded, err = btcutil.DecodeAddr(test.addr)
if (err == nil) != test.valid {
t.Errorf("%v: decoding test failed", test.name)
return
}
} else {
// The address can't be decoded directly, so instead
// call the creation function.
decoded, err = test.f()
if (err == nil) != test.valid {
t.Errorf("%v: creation test failed", test.name)
return
}
}
// If decoding succeeded, encode again and compare against the original.
if err == nil {
encoded := decoded.EncodeAddress()
// Compare encoded addr against the original encoding.
if test.addr != encoded {
t.Errorf("%v: decoding and encoding produced different addressess: %v != %v",
test.name, test.addr, encoded)
return
}
// Perform type-specific calculations.
var saddr []byte
var net btcwire.BitcoinNet
switch d := decoded.(type) {
case *btcutil.AddressPubKeyHash:
saddr = btcutil.TstAddressSAddr(encoded)
// Net is not part of the Address interface and
// must be calculated here.
net = d.Net()
case *btcutil.AddressScriptHash:
saddr = btcutil.TstAddressSAddr(encoded)
// Net is not part of the Address interface and
// must be calculated here.
net = d.Net()
case *btcutil.AddressPubKey:
// Ignore the error here since the script
// address is checked below.
saddr, _ = hex.DecodeString(d.String())
// Net is not part of the Address interface and
// must be calculated here.
net = d.Net()
}
// Check script address.
if !bytes.Equal(saddr, decoded.ScriptAddress()) {
t.Errorf("%v: script addresses do not match:\n%x != \n%x",
test.name, saddr, decoded.ScriptAddress())
return
}
// Check networks. This check always succeeds for non-P2PKH and
// non-P2SH addresses as both nets will be Go's default zero value.
if net != test.net {
t.Errorf("%v: calculated network does not match expected",
test.name)
return
}
}
if !test.valid {
// If address is invalid, but a creation function exists,
// verify that it returns a nil addr and non-nil error.
if test.f != nil {
_, err := test.f()
if err == nil {
t.Errorf("%v: address is invalid but creating new address succeeded",
test.name)
return
}
}
continue
}
// Valid test, compare address created with f against expected result.
addr, err := test.f()
if err != nil {
t.Errorf("%v: address is valid but creating new address failed with error %v",
test.name, err)
return
}
if !reflect.DeepEqual(addr, test.result) {
t.Errorf("%v: created address does not match expected result",
test.name)
return
}
}
}