本文整理汇总了Golang中github.com/decred/dcrd/chaincfg/chainhash.NewHashFromStr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHashFromStr函数的具体用法?Golang NewHashFromStr怎么用?Golang NewHashFromStr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewHashFromStr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseVoteCreatedNtfnParams
// parseVoteCreatedNtfnParams parses out the hash, block hash, block height,
// ticket input hash, and votebits from a newly created SSGen in wallet.
// from a recent ticket purchase in the wallet.
func parseVoteCreatedNtfnParams(params []json.RawMessage) (txHash *chainhash.Hash,
blockHash *chainhash.Hash,
height int32,
sstxIn *chainhash.Hash,
voteBits uint16,
err error) {
if len(params) != 5 {
return nil, nil, 0, nil, 0, wrongNumParams(len(params))
}
// Unmarshal first parameter as a string and convert to hash.
var th string
err = json.Unmarshal(params[0], &th)
if err != nil {
return nil, nil, 0, nil, 0, err
}
thHash, err := chainhash.NewHashFromStr(th)
if err != nil {
return nil, nil, 0, nil, 0, err
}
// Unmarshal second parameter as a string and convert to hash.
var bh string
err = json.Unmarshal(params[1], &bh)
if err != nil {
return nil, nil, 0, nil, 0, err
}
bhHash, err := chainhash.NewHashFromStr(bh)
if err != nil {
return nil, nil, 0, nil, 0, err
}
// Unmarshal third parameter as an int32.
var h int32
err = json.Unmarshal(params[2], &h)
if err != nil {
return nil, nil, 0, nil, 0, err
}
// Unmarshal fourth parameter as a string and convert to hash.
var ss string
err = json.Unmarshal(params[3], &ss)
if err != nil {
return nil, nil, 0, nil, 0, err
}
ssHash, err := chainhash.NewHashFromStr(ss)
if err != nil {
return nil, nil, 0, nil, 0, err
}
// Unmarshal fifth parameter as a uint16.
var vb uint16
err = json.Unmarshal(params[4], &vb)
if err != nil {
return nil, nil, 0, nil, 0, err
}
return thHash, bhHash, h, ssHash, vb, nil
}
示例2: parseRevocationCreatedNtfnParams
// parseRevocationCreatedNtfnParams parses out the hash and ticket input hash
// from a newly created SSGen in wallet.
func parseRevocationCreatedNtfnParams(params []json.RawMessage) (txHash *chainhash.Hash,
sstxIn *chainhash.Hash, err error) {
if len(params) != 2 {
return nil, nil, wrongNumParams(len(params))
}
// Unmarshal first parameter as a string and convert to hash.
var th string
err = json.Unmarshal(params[0], &th)
if err != nil {
return nil, nil, err
}
thHash, err := chainhash.NewHashFromStr(th)
if err != nil {
return nil, nil, err
}
// Unmarshal second parameter as a string and convert to hash.
var ss string
err = json.Unmarshal(params[1], &ss)
if err != nil {
return nil, nil, err
}
ssHash, err := chainhash.NewHashFromStr(ss)
if err != nil {
return nil, nil, err
}
return thHash, ssHash, nil
}
示例3: parseWinningTicketsNtfnParams
// parseWinningTicketsNtfnParams parses out the list of eligible tickets, block
// hash, and block height from a WinningTickets notification.
func parseWinningTicketsNtfnParams(params []json.RawMessage) (
*chainhash.Hash,
int64,
[]*chainhash.Hash,
error) {
if len(params) != 3 {
return nil, 0, nil, wrongNumParams(len(params))
}
// Unmarshal first parameter as a string.
var blockHashStr string
err := json.Unmarshal(params[0], &blockHashStr)
if err != nil {
return nil, 0, nil, err
}
// Create ShaHash from block sha string.
bHash, err := chainhash.NewHashFromStr(blockHashStr)
if err != nil {
return nil, 0, nil, err
}
// Unmarshal second parameter as an integer.
var blockHeight int32
err = json.Unmarshal(params[1], &blockHeight)
if err != nil {
return nil, 0, nil, err
}
bHeight := int64(blockHeight)
// Unmarshal third parameter as a slice.
tickets := make(map[string]string)
err = json.Unmarshal(params[2], &tickets)
if err != nil {
return nil, 0, nil, err
}
t := make([]*chainhash.Hash, len(tickets))
for i, ticketHashStr := range tickets {
// Create and cache Hash from tx hash.
ticketHash, err := chainhash.NewHashFromStr(ticketHashStr)
if err != nil {
return nil, 0, nil, err
}
itr, err := strconv.Atoi(i)
if err != nil {
return nil, 0, nil, err
}
t[itr] = ticketHash
}
return bHash, bHeight, t, nil
}
示例4: parseReorganizationNtfnParams
func parseReorganizationNtfnParams(params []json.RawMessage) (*chainhash.Hash,
int32, *chainhash.Hash, int32, error) {
errorOut := func(err error) (*chainhash.Hash, int32, *chainhash.Hash,
int32, error) {
return nil, 0, nil, 0, err
}
if len(params) != 4 {
return errorOut(wrongNumParams(len(params)))
}
// Unmarshal first parameter as a string.
var oldHashStr string
err := json.Unmarshal(params[0], &oldHashStr)
if err != nil {
return errorOut(err)
}
// Unmarshal second parameter as an integer.
var oldHeight int32
err = json.Unmarshal(params[1], &oldHeight)
if err != nil {
return errorOut(err)
}
// Unmarshal first parameter as a string.
var newHashStr string
err = json.Unmarshal(params[2], &newHashStr)
if err != nil {
return errorOut(err)
}
// Unmarshal second parameter as an integer.
var newHeight int32
err = json.Unmarshal(params[3], &newHeight)
if err != nil {
return errorOut(err)
}
// Create hash from block sha string.
oldHash, err := chainhash.NewHashFromStr(oldHashStr)
if err != nil {
return errorOut(err)
}
// Create hash from block sha string.
newHash, err := chainhash.NewHashFromStr(newHashStr)
if err != nil {
return errorOut(err)
}
return oldHash, oldHeight, newHash, newHeight, nil
}
示例5: TestFilterInsertUpdateNone
func TestFilterInsertUpdateNone(t *testing.T) {
f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateNone)
// Add the generation pubkey
inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" +
"876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" +
"2252247d97a46a91"
inputBytes, err := hex.DecodeString(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err)
return
}
f.Add(inputBytes)
// Add the output address for the 4th transaction
inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21"
inputBytes, err = hex.DecodeString(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err)
return
}
f.Add(inputBytes)
inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"
sha, err := chainhash.NewHashFromStr(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err)
return
}
outpoint := wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular)
if f.MatchesOutPoint(outpoint) {
t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)
return
}
inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"
sha, err = chainhash.NewHashFromStr(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone NewShaHashFromStr failed: %v", err)
return
}
outpoint = wire.NewOutPoint(sha, 0, dcrutil.TxTreeRegular)
if f.MatchesOutPoint(outpoint) {
t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)
return
}
}
示例6: convertJSONUnspentToOutPoints
// convertJSONUnspentToOutPoints converts a JSON raw dump from listunspent to
// a set of UTXOs.
func convertJSONUnspentToOutPoints(
utxos []dcrjson.ListUnspentResult) []*extendedOutPoint {
var eops []*extendedOutPoint
for _, utxo := range utxos {
if utxo.TxType == 1 && utxo.Vout == 0 {
continue
}
op := new(wire.OutPoint)
hash, _ := chainhash.NewHashFromStr(utxo.TxID)
op.Hash = *hash
op.Index = uint32(utxo.Vout)
op.Tree = int8(utxo.Tree)
pks, err := hex.DecodeString(utxo.ScriptPubKey)
if err != nil {
fmt.Println("failure decoding pkscript from unspent list")
os.Exit(1)
}
eop := new(extendedOutPoint)
eop.op = op
amtCast, _ := dcrutil.NewAmount(utxo.Amount)
eop.amt = int64(amtCast)
eop.pkScript = pks
eops = append(eops, eop)
}
return eops
}
示例7: parseOutPoint
func parseOutPoint(input *dcrjson.ListUnspentResult) (wire.OutPoint, error) {
txHash, err := chainhash.NewHashFromStr(input.TxID)
if err != nil {
return wire.OutPoint{}, err
}
return wire.OutPoint{Hash: *txHash, Index: input.Vout, Tree: input.Tree}, nil
}
示例8: newHashFromStr
// newHashFromStr converts the passed big-endian hex string into a
// chainhash.Hash. It only differs from the one available in chainhash in that
// it panics on an error since it will only (and must only) be called with
// hard-coded, and therefore known good, hashes.
func newHashFromStr(hexStr string) *chainhash.Hash {
hash, err := chainhash.NewHashFromStr(hexStr)
if err != nil {
panic(err)
}
return hash
}
示例9: BenchmarkDecodeNotFound
// BenchmarkDecodeNotFound performs a benchmark on how long it takes to decode
// a notfound message with the maximum number of entries.
func BenchmarkDecodeNotFound(b *testing.B) {
// Create a message with the maximum number of entries.
pver := ProtocolVersion
var m MsgNotFound
for i := 0; i < MaxInvPerMsg; i++ {
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
b.Fatalf("chainhash.NewHashFromStr: unexpected error: %v", err)
}
m.AddInvVect(NewInvVect(InvTypeBlock, hash))
}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver); err != nil {
b.Fatalf("MsgNotFound.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgNotFound
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
msg.BtcDecode(r, pver)
}
}
示例10: BenchmarkDecodeGetBlocks
// BenchmarkDecodeGetBlocks performs a benchmark on how long it takes to
// decode a getblocks message with the maximum number of block locator hashes.
func BenchmarkDecodeGetBlocks(b *testing.B) {
// Create a message with the maximum number of block locators.
pver := ProtocolVersion
var m MsgGetBlocks
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
b.Fatalf("chainhash.NewHashFromStr: unexpected error: %v", err)
}
m.AddBlockLocatorHash(hash)
}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver); err != nil {
b.Fatalf("MsgGetBlocks.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgGetBlocks
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
msg.BtcDecode(r, pver)
}
}
示例11: parseTicketPurchasedNtfnParams
// parseTicketPurchasedNtfnParams parses out the ticket hash and amount
// from a recent ticket purchase in the wallet.
func parseTicketPurchasedNtfnParams(params []json.RawMessage) (txHash *chainhash.Hash,
amount dcrutil.Amount, err error) {
if len(params) != 2 {
return nil, 0, wrongNumParams(len(params))
}
// Unmarshal first parameter as a string and convert to hash.
var th string
err = json.Unmarshal(params[0], &th)
if err != nil {
return nil, 0, err
}
thHash, err := chainhash.NewHashFromStr(th)
if err != nil {
return nil, 0, err
}
// Unmarshal second parameter as an int64.
var amt int64
err = json.Unmarshal(params[1], &amt)
if err != nil {
return nil, 0, err
}
return thHash, dcrutil.Amount(amt), nil
}
示例12: Receive
// Receive waits for the response promised by the future and returns a list of
// block hashes generated by the call.
func (r FutureGenerateResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal result as a list of strings.
var result []string
err = json.Unmarshal(res, &result)
if err != nil {
return nil, err
}
// Convert each block hash to a chainhash.Hash and store a pointer to
// each.
convertedResult := make([]*chainhash.Hash, len(result))
for i, hashString := range result {
convertedResult[i], err = chainhash.NewHashFromStr(hashString)
if err != nil {
return nil, err
}
}
return convertedResult, nil
}
示例13: Receive
// Receive waits for the response promised by the future and returns the hashes
// of all transactions in the memory pool.
func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal the result as an array of strings.
var txHashStrs []string
err = json.Unmarshal(res, &txHashStrs)
if err != nil {
return nil, err
}
// Create a slice of ShaHash arrays from the string slice.
txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
for _, hashStr := range txHashStrs {
txHash, err := chainhash.NewHashFromStr(hashStr)
if err != nil {
return nil, err
}
txHashes = append(txHashes, txHash)
}
return txHashes, nil
}
示例14: ExampleNewFilter
// This example demonstrates how to create a new bloom filter, add a transaction
// hash to it, and check if the filter matches the transaction.
func ExampleNewFilter() {
rand.Seed(time.Now().UnixNano())
tweak := rand.Uint32()
// Create a new bloom filter intended to hold 10 elements with a 0.01%
// false positive rate and does not include any automatic update
// functionality when transactions are matched.
filter := bloom.NewFilter(10, tweak, 0.0001, wire.BloomUpdateNone)
// Create a transaction hash and add it to the filter. This particular
// trasaction is the first transaction in block 310,000 of the main
// bitcoin block chain.
txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45"
txHash, err := chainhash.NewHashFromStr(txHashStr)
if err != nil {
fmt.Println(err)
return
}
filter.AddHash(txHash)
// Show that the filter matches.
matches := filter.Matches(txHash[:])
fmt.Println("Filter Matches?:", matches)
// Output:
// Filter Matches?: true
}
示例15: decodeHash
func decodeHash(reversedHash string) chainhash.Hash {
h, err := chainhash.NewHashFromStr(reversedHash)
if err != nil {
panic(err)
}
return *h
}