github.com/btcsuite/btcd@v0.24.0/rpcclient/example_test.go (about) 1 // Copyright (c) 2020 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package rpcclient 6 7 import ( 8 "fmt" 9 "github.com/btcsuite/btcd/btcjson" 10 ) 11 12 var connCfg = &ConnConfig{ 13 Host: "localhost:8332", 14 User: "user", 15 Pass: "pass", 16 HTTPPostMode: true, 17 DisableTLS: true, 18 } 19 20 func ExampleClient_GetDescriptorInfo() { 21 client, err := New(connCfg, nil) 22 if err != nil { 23 panic(err) 24 } 25 defer client.Shutdown() 26 27 descriptorInfo, err := client.GetDescriptorInfo( 28 "wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)") 29 if err != nil { 30 panic(err) 31 } 32 33 fmt.Printf("%+v\n", descriptorInfo) 34 // &{Descriptor:wpkh([d34db33f/84'/0'/0']0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798)#n9g43y4k Checksum:qwlqgth7 IsRange:false IsSolvable:true HasPrivateKeys:false} 35 } 36 37 func ExampleClient_ImportMulti() { 38 client, err := New(connCfg, nil) 39 if err != nil { 40 panic(err) 41 } 42 defer client.Shutdown() 43 44 requests := []btcjson.ImportMultiRequest{ 45 { 46 Descriptor: btcjson.String( 47 "pkh([f34db33f/44'/0'/0']xpub6Cc939fyHvfB9pPLWd3bSyyQFvgKbwhidca49jGCM5Hz5ypEPGf9JVXB4NBuUfPgoHnMjN6oNgdC9KRqM11RZtL8QLW6rFKziNwHDYhZ6Kx/0/*)#ed7px9nu"), 48 Range: &btcjson.DescriptorRange{Value: []int{0, 100}}, 49 Timestamp: btcjson.TimestampOrNow{Value: 0}, // scan from genesis 50 WatchOnly: btcjson.Bool(true), 51 KeyPool: btcjson.Bool(false), 52 Internal: btcjson.Bool(false), 53 }, 54 } 55 opts := &btcjson.ImportMultiOptions{Rescan: true} 56 57 resp, err := client.ImportMulti(requests, opts) 58 if err != nil { 59 panic(err) 60 } 61 62 fmt.Println(resp[0].Success) 63 // true 64 } 65 66 func ExampleClient_DeriveAddresses() { 67 client, err := New(connCfg, nil) 68 if err != nil { 69 panic(err) 70 } 71 defer client.Shutdown() 72 73 addrs, err := client.DeriveAddresses( 74 "pkh([f34db33f/44'/0'/0']xpub6Cc939fyHvfB9pPLWd3bSyyQFvgKbwhidca49jGCM5Hz5ypEPGf9JVXB4NBuUfPgoHnMjN6oNgdC9KRqM11RZtL8QLW6rFKziNwHDYhZ6Kx/0/*)#ed7px9nu", 75 &btcjson.DescriptorRange{Value: []int{0, 2}}) 76 if err != nil { 77 panic(err) 78 } 79 80 fmt.Printf("%+v\n", addrs) 81 // &[14NjenDKkGGq1McUgoSkeUHJpW3rrKLbPW 1Pn6i3cvdGhqbdgNjXHfbaYfiuviPiymXj 181x1NbgGYKLeMXkDdXEAqepG75EgU8XtG] 82 } 83 84 func ExampleClient_GetAddressInfo() { 85 client, err := New(connCfg, nil) 86 if err != nil { 87 panic(err) 88 } 89 defer client.Shutdown() 90 91 info, err := client.GetAddressInfo("2NF1FbxtUAsvdU4uW1UC2xkBVatp6cYQuJ3") 92 if err != nil { 93 panic(err) 94 } 95 96 fmt.Println(info.Address) // 2NF1FbxtUAsvdU4uW1UC2xkBVatp6cYQuJ3 97 fmt.Println(info.ScriptType.String()) // witness_v0_keyhash 98 fmt.Println(*info.HDKeyPath) // m/49'/1'/0'/0/4 99 fmt.Println(info.Embedded.Address) // tb1q3x2h2kh57wzg7jz00jhwn0ycvqtdk2ane37j27 100 } 101 102 func ExampleClient_GetWalletInfo() { 103 client, err := New(connCfg, nil) 104 if err != nil { 105 panic(err) 106 } 107 defer client.Shutdown() 108 109 info, err := client.GetWalletInfo() 110 if err != nil { 111 panic(err) 112 } 113 114 fmt.Println(info.WalletVersion) // 169900 115 fmt.Println(info.TransactionCount) // 22 116 fmt.Println(*info.HDSeedID) // eb44e4e9b864ef17e7ba947da746375b000f5d94 117 fmt.Println(info.Scanning.Value) // false 118 } 119 120 func ExampleClient_GetTxOutSetInfo() { 121 client, err := New(connCfg, nil) 122 if err != nil { 123 panic(err) 124 } 125 defer client.Shutdown() 126 127 r, err := client.GetTxOutSetInfo() 128 if err != nil { 129 panic(err) 130 } 131 132 fmt.Println(r.TotalAmount.String()) // 20947654.56996054 BTC 133 fmt.Println(r.BestBlock.String()) // 000000000000005f94116250e2407310463c0a7cf950f1af9ebe935b1c0687ab 134 fmt.Println(r.TxOuts) // 24280607 135 fmt.Println(r.Transactions) // 9285603 136 fmt.Println(r.DiskSize) // 1320871611 137 } 138 139 func ExampleClient_CreateWallet() { 140 client, err := New(connCfg, nil) 141 if err != nil { 142 panic(err) 143 } 144 defer client.Shutdown() 145 146 r, err := client.CreateWallet( 147 "mywallet", 148 WithCreateWalletBlank(), 149 WithCreateWalletPassphrase("secret"), 150 ) 151 if err != nil { 152 panic(err) 153 } 154 155 fmt.Println(r.Name) // mywallet 156 }