Skip to content
This repository was archived by the owner on Apr 4, 2019. It is now read-only.

Commit 0e6316f

Browse files
committed
address: Update Address interface
1 parent d0d324d commit 0e6316f

File tree

12 files changed

+105
-111
lines changed

12 files changed

+105
-111
lines changed

chaincfg/params.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ var SimNetParams = Params{
534534
HDPrivateKeyID: [4]byte{0x04, 0x20, 0xb9, 0x00}, // starts with sprv
535535
HDPublicKeyID: [4]byte{0x04, 0x20, 0xbd, 0x3a}, // starts with spub
536536

537-
CashAddrPrefix: "bchsim",
537+
CashAddrPrefix: "bchreg",
538538

539539
// BIP44 coin type used in the hierarchical deterministic path for
540540
// address generation.

config.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,7 @@ func loadConfig() (*config, []string, error) {
827827
// Check mining addresses are valid and saved parsed versions.
828828
cfg.miningAddrs = make([]cashutil.Address, 0, len(cfg.MiningAddrs))
829829
for _, strAddr := range cfg.MiningAddrs {
830-
var addr cashutil.Address
831-
var err error
832-
833-
if len(strAddr) >= 42 {
834-
addr, err = cashutil.DecodeCashAddr(strAddr, activeNetParams.Params)
835-
} else {
836-
addr, err = cashutil.DecodeAddress(strAddr, activeNetParams.Params)
837-
}
830+
addr, err := cashutil.DecodeAddress(strAddr, activeNetParams.Params)
838831
if err != nil {
839832
str := "%s: mining address '%s' failed to decode: %v"
840833
err := fmt.Errorf(str, funcName, strAddr, err)

rpcclient/extensions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (c *Client) ListAddressTransactionsAsync(addresses []cashutil.Address, acco
132132
// Convert addresses to strings.
133133
addrs := make([]string, 0, len(addresses))
134134
for _, addr := range addresses {
135-
addrs = append(addrs, addr.EncodeAddress())
135+
addrs = append(addrs, addr.EncodeAddress(true))
136136
}
137137
cmd := btcjson.NewListAddressTransactionsCmd(addrs, &account)
138138
return c.sendCmd(cmd)

rpcclient/notify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ func (c *Client) LoadTxFilterAsync(reload bool, addresses []cashutil.Address,
13321332

13331333
addrStrs := make([]string, len(addresses))
13341334
for i, a := range addresses {
1335-
addrStrs[i] = a.EncodeAddress()
1335+
addrStrs[i] = a.EncodeAddress(true)
13361336
}
13371337
outPointObjects := make([]btcjson.OutPoint, len(outPoints))
13381338
for i := range outPoints {

rpcclient/rawtransactions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func (r FutureSearchRawTransactionsResult) Receive() ([]*wire.MsgTx, error) {
551551
//
552552
// See SearchRawTransactions for the blocking version and more details.
553553
func (c *Client) SearchRawTransactionsAsync(address cashutil.Address, skip, count int, reverse bool, filterAddrs []string) FutureSearchRawTransactionsResult {
554-
addr := address.EncodeAddress()
554+
addr := address.EncodeAddress(true)
555555
verbose := btcjson.Int(0)
556556
cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count,
557557
nil, &reverse, &filterAddrs)
@@ -600,7 +600,7 @@ func (r FutureSearchRawTransactionsVerboseResult) Receive() ([]*btcjson.SearchRa
600600
func (c *Client) SearchRawTransactionsVerboseAsync(address cashutil.Address, skip,
601601
count int, includePrevOut, reverse bool, filterAddrs *[]string) FutureSearchRawTransactionsVerboseResult {
602602

603-
addr := address.EncodeAddress()
603+
addr := address.EncodeAddress(true)
604604
verbose := btcjson.Int(1)
605605
var prevOut *int
606606
if includePrevOut {

rpcclient/wallet.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (c *Client) ListUnspentMinMaxAsync(minConf, maxConf int) FutureListUnspentR
206206
func (c *Client) ListUnspentMinMaxAddressesAsync(minConf, maxConf int, addrs []cashutil.Address) FutureListUnspentResult {
207207
addrStrs := make([]string, 0, len(addrs))
208208
for _, a := range addrs {
209-
addrStrs = append(addrStrs, a.EncodeAddress())
209+
addrStrs = append(addrStrs, a.EncodeAddress(true))
210210
}
211211

212212
cmd := btcjson.NewListUnspentCmd(&minConf, &maxConf, &addrStrs)
@@ -471,7 +471,7 @@ func (r FutureSendToAddressResult) Receive() (*chainhash.Hash, error) {
471471
//
472472
// See SendToAddress for the blocking version and more details.
473473
func (c *Client) SendToAddressAsync(address cashutil.Address, amount cashutil.Amount) FutureSendToAddressResult {
474-
addr := address.EncodeAddress()
474+
addr := address.EncodeAddress(true)
475475
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBCH(), nil, nil)
476476
return c.sendCmd(cmd)
477477
}
@@ -497,7 +497,7 @@ func (c *Client) SendToAddressCommentAsync(address cashutil.Address,
497497
amount cashutil.Amount, comment,
498498
commentTo string) FutureSendToAddressResult {
499499

500-
addr := address.EncodeAddress()
500+
addr := address.EncodeAddress(true)
501501
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBCH(), &comment,
502502
&commentTo)
503503
return c.sendCmd(cmd)
@@ -550,7 +550,7 @@ func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) {
550550
//
551551
// See SendFrom for the blocking version and more details.
552552
func (c *Client) SendFromAsync(fromAccount string, toAddress cashutil.Address, amount cashutil.Amount) FutureSendFromResult {
553-
addr := toAddress.EncodeAddress()
553+
addr := toAddress.EncodeAddress(true)
554554
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBCH(), nil,
555555
nil, nil)
556556
return c.sendCmd(cmd)
@@ -574,7 +574,7 @@ func (c *Client) SendFrom(fromAccount string, toAddress cashutil.Address, amount
574574
//
575575
// See SendFromMinConf for the blocking version and more details.
576576
func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress cashutil.Address, amount cashutil.Amount, minConfirms int) FutureSendFromResult {
577-
addr := toAddress.EncodeAddress()
577+
addr := toAddress.EncodeAddress(true)
578578
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBCH(),
579579
&minConfirms, nil, nil)
580580
return c.sendCmd(cmd)
@@ -603,7 +603,7 @@ func (c *Client) SendFromCommentAsync(fromAccount string,
603603
toAddress cashutil.Address, amount cashutil.Amount, minConfirms int,
604604
comment, commentTo string) FutureSendFromResult {
605605

606-
addr := toAddress.EncodeAddress()
606+
addr := toAddress.EncodeAddress(true)
607607
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBCH(),
608608
&minConfirms, &comment, &commentTo)
609609
return c.sendCmd(cmd)
@@ -660,7 +660,7 @@ func (r FutureSendManyResult) Receive() (*chainhash.Hash, error) {
660660
func (c *Client) SendManyAsync(fromAccount string, amounts map[cashutil.Address]cashutil.Amount) FutureSendManyResult {
661661
convertedAmounts := make(map[string]float64, len(amounts))
662662
for addr, amount := range amounts {
663-
convertedAmounts[addr.EncodeAddress()] = amount.ToBCH()
663+
convertedAmounts[addr.EncodeAddress(true)] = amount.ToBCH()
664664
}
665665
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil)
666666
return c.sendCmd(cmd)
@@ -689,7 +689,7 @@ func (c *Client) SendManyMinConfAsync(fromAccount string,
689689

690690
convertedAmounts := make(map[string]float64, len(amounts))
691691
for addr, amount := range amounts {
692-
convertedAmounts[addr.EncodeAddress()] = amount.ToBCH()
692+
convertedAmounts[addr.EncodeAddress(true)] = amount.ToBCH()
693693
}
694694
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts,
695695
&minConfirms, nil)
@@ -723,7 +723,7 @@ func (c *Client) SendManyCommentAsync(fromAccount string,
723723

724724
convertedAmounts := make(map[string]float64, len(amounts))
725725
for addr, amount := range amounts {
726-
convertedAmounts[addr.EncodeAddress()] = amount.ToBCH()
726+
convertedAmounts[addr.EncodeAddress(true)] = amount.ToBCH()
727727
}
728728
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts,
729729
&minConfirms, &comment)
@@ -972,7 +972,7 @@ func (r FutureGetAccountResult) Receive() (string, error) {
972972
//
973973
// See GetAccount for the blocking version and more details.
974974
func (c *Client) GetAccountAsync(address cashutil.Address) FutureGetAccountResult {
975-
addr := address.EncodeAddress()
975+
addr := address.EncodeAddress(true)
976976
cmd := btcjson.NewGetAccountCmd(addr)
977977
return c.sendCmd(cmd)
978978
}
@@ -999,7 +999,7 @@ func (r FutureSetAccountResult) Receive() error {
999999
//
10001000
// See SetAccount for the blocking version and more details.
10011001
func (c *Client) SetAccountAsync(address cashutil.Address, account string) FutureSetAccountResult {
1002-
addr := address.EncodeAddress()
1002+
addr := address.EncodeAddress(true)
10031003
cmd := btcjson.NewSetAccountCmd(addr, account)
10041004
return c.sendCmd(cmd)
10051005
}
@@ -1202,7 +1202,7 @@ func (r FutureValidateAddressResult) Receive() (*btcjson.ValidateAddressWalletRe
12021202
//
12031203
// See ValidateAddress for the blocking version and more details.
12041204
func (c *Client) ValidateAddressAsync(address cashutil.Address) FutureValidateAddressResult {
1205-
addr := address.EncodeAddress()
1205+
addr := address.EncodeAddress(true)
12061206
cmd := btcjson.NewValidateAddressCmd(addr)
12071207
return c.sendCmd(cmd)
12081208
}
@@ -1575,7 +1575,7 @@ func (r FutureGetReceivedByAddressResult) Receive() (cashutil.Amount, error) {
15751575
//
15761576
// See GetReceivedByAddress for the blocking version and more details.
15771577
func (c *Client) GetReceivedByAddressAsync(address cashutil.Address) FutureGetReceivedByAddressResult {
1578-
addr := address.EncodeAddress()
1578+
addr := address.EncodeAddress(true)
15791579
cmd := btcjson.NewGetReceivedByAddressCmd(addr, nil)
15801580
return c.sendCmd(cmd)
15811581

@@ -1596,7 +1596,7 @@ func (c *Client) GetReceivedByAddress(address cashutil.Address) (cashutil.Amount
15961596
//
15971597
// See GetReceivedByAddressMinConf for the blocking version and more details.
15981598
func (c *Client) GetReceivedByAddressMinConfAsync(address cashutil.Address, minConfirms int) FutureGetReceivedByAddressResult {
1599-
addr := address.EncodeAddress()
1599+
addr := address.EncodeAddress(true)
16001600
cmd := btcjson.NewGetReceivedByAddressCmd(addr, &minConfirms)
16011601
return c.sendCmd(cmd)
16021602
}
@@ -1885,7 +1885,7 @@ func (r FutureSignMessageResult) Receive() (string, error) {
18851885
//
18861886
// See SignMessage for the blocking version and more details.
18871887
func (c *Client) SignMessageAsync(address cashutil.Address, message string) FutureSignMessageResult {
1888-
addr := address.EncodeAddress()
1888+
addr := address.EncodeAddress(true)
18891889
cmd := btcjson.NewSignMessageCmd(addr, message)
18901890
return c.sendCmd(cmd)
18911891
}
@@ -1926,7 +1926,7 @@ func (r FutureVerifyMessageResult) Receive() (bool, error) {
19261926
//
19271927
// See VerifyMessage for the blocking version and more details.
19281928
func (c *Client) VerifyMessageAsync(address cashutil.Address, signature, message string) FutureVerifyMessageResult {
1929-
addr := address.EncodeAddress()
1929+
addr := address.EncodeAddress(true)
19301930
cmd := btcjson.NewVerifyMessageCmd(addr, signature, message)
19311931
return c.sendCmd(cmd)
19321932
}
@@ -1972,7 +1972,7 @@ func (r FutureDumpPrivKeyResult) Receive() (*cashutil.WIF, error) {
19721972
//
19731973
// See DumpPrivKey for the blocking version and more details.
19741974
func (c *Client) DumpPrivKeyAsync(address cashutil.Address) FutureDumpPrivKeyResult {
1975-
addr := address.EncodeAddress()
1975+
addr := address.EncodeAddress(true)
19761976
cmd := btcjson.NewDumpPrivKeyCmd(addr)
19771977
return c.sendCmd(cmd)
19781978
}

rpcserver.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func handleCreateRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan
554554
}
555555

556556
// Decode the provided address.
557-
addr, err := cashutil.DecodeCashAddr(encodedAddr, params)
557+
addr, err := cashutil.DecodeAddress(encodedAddr, params)
558558
if err != nil {
559559
return nil, &btcjson.RPCError{
560560
Code: btcjson.ErrRPCInvalidAddressOrKey,
@@ -688,7 +688,7 @@ func createVoutList(mtx *wire.MsgTx, chainParams *chaincfg.Params, filterAddrMap
688688
passesFilter := len(filterAddrMap) == 0
689689
encodedAddrs := make([]string, len(addrs))
690690
for j, addr := range addrs {
691-
encodedAddr := cashutil.EncodeCashAddr(addr, chainParams)
691+
encodedAddr := addr.EncodeAddress(true)
692692
encodedAddrs[j] = encodedAddr
693693

694694
// No need to check the map again if the filter already
@@ -811,7 +811,7 @@ func handleDecodeScript(s *rpcServer, cmd interface{}, closeChan <-chan struct{}
811811
s.cfg.ChainParams)
812812
addresses := make([]string, len(addrs))
813813
for i, addr := range addrs {
814-
addresses[i] = cashutil.EncodeCashAddr(addr, s.cfg.ChainParams)
814+
addresses[i] = addr.EncodeAddress(true)
815815
}
816816

817817
// Convert the script itself to a pay-to-script-hash address.
@@ -829,7 +829,7 @@ func handleDecodeScript(s *rpcServer, cmd interface{}, closeChan <-chan struct{}
829829
Addresses: addresses,
830830
}
831831
if scriptClass != txscript.ScriptHashTy {
832-
reply.P2sh = cashutil.EncodeCashAddr(p2sh, s.cfg.ChainParams)
832+
reply.P2sh = p2sh.EncodeAddress(true)
833833
}
834834
return reply, nil
835835
}
@@ -2698,7 +2698,7 @@ func handleGetTxOut(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
26982698
s.cfg.ChainParams)
26992699
addresses := make([]string, len(addrs))
27002700
for i, addr := range addrs {
2701-
addresses[i] = cashutil.EncodeCashAddr(addr, s.cfg.ChainParams)
2701+
addresses[i] = addr.EncodeAddress(true)
27022702
}
27032703

27042704
txOutReply := &btcjson.GetTxOutResult{
@@ -2946,7 +2946,7 @@ func createVinListPrevOut(s *rpcServer, mtx *wire.MsgTx, chainParams *chaincfg.P
29462946
// filter when needed.
29472947
encodedAddrs := make([]string, len(addrs))
29482948
for j, addr := range addrs {
2949-
encodedAddr := cashutil.EncodeCashAddr(addr, chainParams)
2949+
encodedAddr := addr.EncodeAddress(true)
29502950
encodedAddrs[j] = encodedAddr
29512951

29522952
// No need to check the map again if the filter already
@@ -3036,7 +3036,7 @@ func handleSearchRawTransactions(s *rpcServer, cmd interface{}, closeChan <-chan
30363036

30373037
// Attempt to decode the supplied address.
30383038
params := s.cfg.ChainParams
3039-
addr, err := cashutil.DecodeCashAddr(c.Address, params)
3039+
addr, err := cashutil.DecodeAddress(c.Address, params)
30403040
if err != nil {
30413041
return nil, &btcjson.RPCError{
30423042
Code: btcjson.ErrRPCInvalidAddressOrKey,
@@ -3433,13 +3433,13 @@ func handleValidateAddress(s *rpcServer, cmd interface{}, closeChan <-chan struc
34333433
c := cmd.(*btcjson.ValidateAddressCmd)
34343434

34353435
result := btcjson.ValidateAddressChainResult{}
3436-
addr, err := cashutil.DecodeCashAddr(c.Address, s.cfg.ChainParams)
3436+
addr, err := cashutil.DecodeAddress(c.Address, s.cfg.ChainParams)
34373437
if err != nil {
34383438
// Return the default value (false) for IsValid.
34393439
return result, nil
34403440
}
34413441

3442-
result.Address = cashutil.EncodeCashAddr(addr, s.cfg.ChainParams)
3442+
result.Address = addr.EncodeAddress(true)
34433443
result.IsValid = true
34443444

34453445
return result, nil
@@ -3448,21 +3448,22 @@ func handleValidateAddress(s *rpcServer, cmd interface{}, closeChan <-chan struc
34483448
func convCashAddr(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
34493449
c := cmd.(*btcjson.ConvCashAddrCmd)
34503450

3451-
if len(c.Address) >= 42 {
3452-
addr, err := cashutil.DecodeCashAddr(c.Address, s.cfg.ChainParams)
3453-
if err != nil {
3454-
return nil, btcjson.ErrRPCInvalidParams
3455-
}
3456-
3457-
return addr.EncodeAddress(), nil
3458-
}
3459-
34603451
addr, err := cashutil.DecodeAddress(c.Address, s.cfg.ChainParams)
34613452
if err != nil {
34623453
return nil, btcjson.ErrRPCInvalidParams
34633454
}
34643455

3465-
return cashutil.EncodeCashAddr(addr, s.cfg.ChainParams), nil
3456+
// bitcoin cash base32-encoded address:
3457+
// 1. mainnet: bitcoincash: + 42 bytes = 12 + 42
3458+
// 2. testnet3: bchtest: + 42 bytes = 8 + 42
3459+
// 3. regtest: bchreg: + 42 bytes = 7 + 42
3460+
// if the address does not have the prefix: length = 42
3461+
// if the address have the prefix: max length = 42 + 12
3462+
if len(c.Address) >= 42 && len(c.Address) <= 42+12 {
3463+
return addr.EncodeAddress(false), nil
3464+
}
3465+
3466+
return addr.EncodeAddress(true), nil
34663467
}
34673468

34683469
func verifyChain(s *rpcServer, level, depth int32) error {
@@ -3522,7 +3523,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{
35223523

35233524
// Decode the provided address.
35243525
params := s.cfg.ChainParams
3525-
addr, err := cashutil.DecodeCashAddr(c.Address, params)
3526+
addr, err := cashutil.DecodeAddress(c.Address, params)
35263527
if err != nil {
35273528
return nil, &btcjson.RPCError{
35283529
Code: btcjson.ErrRPCInvalidAddressOrKey,
@@ -3576,7 +3577,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{
35763577
}
35773578

35783579
// Return boolean if addresses match.
3579-
return cashutil.EncodeCashAddr(address, s.cfg.ChainParams) == c.Address, nil
3580+
return address.EncodeAddress(true) == c.Address, nil
35803581
}
35813582

35823583
// handleVersion implements the version command.

rpcwebsocket.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (f *wsClientFilter) addAddress(a cashutil.Address) {
321321
}
322322
}
323323

324-
f.otherAddresses[a.EncodeAddress()] = struct{}{}
324+
f.otherAddresses[a.EncodeAddress(true)] = struct{}{}
325325
}
326326

327327
// addAddressStr parses an address from a string and then adds it to the
@@ -373,7 +373,7 @@ func (f *wsClientFilter) existsAddress(a cashutil.Address) bool {
373373
}
374374
}
375375

376-
_, ok := f.otherAddresses[a.EncodeAddress()]
376+
_, ok := f.otherAddresses[a.EncodeAddress(true)]
377377
return ok
378378
}
379379

@@ -405,7 +405,7 @@ func (f *wsClientFilter) removeAddress(a cashutil.Address) {
405405
}
406406
}
407407

408-
delete(f.otherAddresses, a.EncodeAddress())
408+
delete(f.otherAddresses, a.EncodeAddress(true))
409409
}
410410

411411
// removeAddressStr parses an address from a string and then removes it from the
@@ -1006,7 +1006,7 @@ func (m *wsNotificationManager) notifyForTxOuts(ops map[wire.OutPoint]map[chan s
10061006
}
10071007

10081008
for _, txAddr := range txAddrs {
1009-
cmap, ok := addrs[txAddr.EncodeAddress()]
1009+
cmap, ok := addrs[txAddr.EncodeAddress(true)]
10101010
if !ok {
10111011
continue
10121012
}
@@ -2098,7 +2098,7 @@ func rescanBlock(wsc *wsClient, lookups *rescanKeys, blk *cashutil.Block) {
20982098
default:
20992099
// A new address type must have been added. Encode as a
21002100
// payment address string and check the fallback map.
2101-
addrStr := addr.EncodeAddress()
2101+
addrStr := addr.EncodeAddress(true)
21022102
_, ok := lookups.fallbacks[addrStr]
21032103
if !ok {
21042104
continue

0 commit comments

Comments
 (0)