Skip to content

Commit

Permalink
feat: add deriveddresses call (#11)
Browse files Browse the repository at this point in the history
* feat: add testDeriveAddressResult to mock

Signed-off-by: Lorenz Herzberger <[email protected]>
  • Loading branch information
LaurentMontBlanc authored Feb 8, 2024
1 parent 4960ced commit c4ecb3e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ var (
MethodSendToAddress = "sendtoaddress"
MethodSignRawTransactionWithWallet = "signrawtransactionwithwallet"
MethodTestMempoolAccept = "testmempoolaccept"
MethodDeriveAddresses = "deriveaddresses"
)
4 changes: 4 additions & 0 deletions types/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package types

// DeriveAddressesResult models the data from the deriveaddresses command.
type DeriveAddressesResult []string
16 changes: 16 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package elementsrpc

import (
"github.com/rddl-network/elements-rpc/types"
"github.com/rddl-network/elements-rpc/utils"
)

func DeriveAddresses(url string, params []string) (addresses types.DeriveAddressesResult, err error) {
result, err := SendRequest(url, types.MethodDeriveAddresses, params)
if err != nil {
return
}

addresses = utils.ParseResultToArray(result)
return
}
5 changes: 4 additions & 1 deletion utils/mocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ var (
transactionResult = types.GetTransactionResult{Hex: zeros, TxID: zeros, Amount: map[string]float64{
"7add40beb27df701e02ee85089c5bc0021bc813823fedb5f1dcb5debda7f3da9": 10000,
}}
zeros = "0000000000000000000000000000000000000000000000000000000000000000"
testDeriveAddressesResult = types.DeriveAddressesResult{address}
zeros = "0000000000000000000000000000000000000000000000000000000000000000"
)

// GetDoFunc fetches the mock client's `Do` func
Expand Down Expand Up @@ -80,6 +81,8 @@ func GetDoFunc(req *http.Request) (*http.Response, error) {
response.Result = signRawTransactionWithWalletResult
case types.MethodTestMempoolAccept:
response.Result = testMempoolAcceptResults
case types.MethodDeriveAddresses:
response.Result = testDeriveAddressesResult
default:
response.Result = nil
response.Error.Code = -1337
Expand Down
11 changes: 11 additions & 0 deletions utils/parse_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

import "strings"

func ParseResultToArray(result []byte) (resultArr []string) {
resultStr := string(result)
truncatedStr := resultStr[1 : len(resultStr)-1] // remove [] at beginning and end of Str
sanitizedStr := strings.ReplaceAll(truncatedStr, "\"", "")

return strings.Split(sanitizedStr, ",")
}

0 comments on commit c4ecb3e

Please sign in to comment.