Skip to content

Commit

Permalink
feat: add loadwallet, unloadwallet and walletpassphrase (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Strobl <[email protected]>
  • Loading branch information
jmastr authored Feb 19, 2024
1 parent 6b55493 commit 46160b2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ Output:
62eefd1ab286bd4c344f8806fc507d5effd26176e91add5afb7f4f6531cf543e
```

### Wallet management
```
package main
import (
"fmt"
"log"
elements "github.com/rddl-network/elements-rpc"
)
func main() {
url := "http://user:[email protected]:18891/wallet/foowallet"
loadWalletResult, err := elements.LoadWallet(url, []string{"foowallet", "true"})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", loadWalletResult)
// wallet was created with passphrase "foobar"
err = elements.Walletpassphrase(url, []string{"foobar", "60"})
if err != nil {
log.Fatal(err)
}
unloadWalletResult, err := elements.UnloadWallet(url, []string{"foowallet", "true"})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", unloadWalletResult)
}
```
Output:
```
{Name:foowallet Warning:}
{Warning:}
```

### Complex Example

```
Expand Down
3 changes: 3 additions & 0 deletions types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ var (
MethodGetNewAddress = "getnewaddress"
MethodGetRawTransaction = "getrawtransaction"
MethodGetTransaction = "gettransaction"
MethodLoadWallet = "loadwallet"
MethodRawIssueAsset = "rawissueasset"
MethodReissueAsset = "reissueasset"
MethodSendRawTransaction = "sendrawtransaction"
MethodSendToAddress = "sendtoaddress"
MethodSignRawTransactionWithWallet = "signrawtransactionwithwallet"
MethodTestMempoolAccept = "testmempoolaccept"
MethodDeriveAddresses = "deriveaddresses"
MethodUnloadWallet = "unloadwallet"
MethodWalletpassphrase = "walletpassphrase"
)
11 changes: 11 additions & 0 deletions types/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ type GetTransactionResult struct {
Hex string `json:"hex"`
}

// LoadWalletResult models the result of the loadwallet command.
type LoadWalletResult struct {
Name string `json:"name"`
Warning string `json:"warning"`
}

// ReissueAssetResult models the result of a "reissueasset" transaction.
type ReissueAssetResult struct {
TxID string `json:"txid"`
Expand All @@ -65,3 +71,8 @@ type SignRawTransactionWithWalletResult struct {
Hex string `json:"hex"`
Complete bool `json:"complete"`
}

// UnloadWalletResult models the result of the unloadwallet command.
type UnloadWalletResult struct {
Warning string `json:"warning"`
}
37 changes: 37 additions & 0 deletions wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ func GetTransaction(url string, params []string) (transactionResult types.GetTra
return
}

// LoadWallet loads a wallet from a wallet file or directory.
func LoadWallet(url string, params []string) (loadWalletResult types.LoadWalletResult, err error) {
result, err := SendRequest(url, types.MethodLoadWallet, params)
if err != nil {
return
}
err = json.Unmarshal(result, &loadWalletResult)
if err != nil {
return
}
return
}

// ReissueAsset creates more of an already issued asset. Must have reissuance
// token in wallet to do so.
func ReissueAsset(url string, params []string) (transactionResult types.ReissueAssetResult, err error) {
Expand Down Expand Up @@ -95,3 +108,27 @@ func SignRawTransactionWithWallet(url string, params []string) (transactionResul
}
return
}

// UnloadWallet unloads the wallet referenced by the request endpoint otherwise
// unloads the wallet specified in the argument.
func UnloadWallet(url string, params []string) (unloadWalletResult types.UnloadWalletResult, err error) {
result, err := SendRequest(url, types.MethodUnloadWallet, params)
if err != nil {
return
}
err = json.Unmarshal(result, &unloadWalletResult)
if err != nil {
return
}
return
}

// Walletpassphrase stores the wallet decryption key in memory for 'timeout'
// seconds.
func Walletpassphrase(url string, params []string) (err error) {
_, err = SendRequest(url, types.MethodWalletpassphrase, params)
if err != nil {
return
}
return
}

0 comments on commit 46160b2

Please sign in to comment.