github.com/algorand/go-algorand-sdk@v1.24.0/client/kmd/wrappers.go (about)

     1  package kmd
     2  
     3  import (
     4  	"golang.org/x/crypto/ed25519"
     5  
     6  	"github.com/algorand/go-algorand-sdk/encoding/msgpack"
     7  	"github.com/algorand/go-algorand-sdk/types"
     8  )
     9  
    10  // Version returns a VersionResponse containing a list of kmd API versions
    11  // supported by this running kmd instance.
    12  func (kcl Client) Version() (resp VersionsResponse, err error) {
    13  	req := VersionsRequest{}
    14  	err = kcl.DoV1Request(req, &resp)
    15  	return
    16  }
    17  
    18  // ListWallets returns a ListWalletsResponse containing the list of wallets
    19  // known to kmd. Using a wallet ID returned from this endpoint, you can
    20  // initialize a wallet handle with client.InitWalletHandle
    21  func (kcl Client) ListWallets() (resp ListWalletsResponse, err error) {
    22  	req := ListWalletsRequest{}
    23  	err = kcl.DoV1Request(req, &resp)
    24  	return
    25  }
    26  
    27  // CreateWallet creates a wallet with the specified name, password, driver,
    28  // and master derivation key. If the master derivation key is blank, one is
    29  // generated internally to kmd. CreateWallet returns a CreateWalletResponse
    30  // containing information about the new wallet.
    31  func (kcl Client) CreateWallet(walletName, walletPassword, walletDriverName string, walletMDK types.MasterDerivationKey) (resp CreateWalletResponse, err error) {
    32  	req := CreateWalletRequest{
    33  		WalletName:          walletName,
    34  		WalletDriverName:    walletDriverName,
    35  		WalletPassword:      walletPassword,
    36  		MasterDerivationKey: walletMDK,
    37  	}
    38  	err = kcl.DoV1Request(req, &resp)
    39  	return
    40  }
    41  
    42  // InitWalletHandle accepts a wallet ID and a wallet password, and returns an
    43  // InitWalletHandleResponse containing a wallet handle token. This wallet
    44  // handle token can be used for subsequent operations on this wallet, like key
    45  // generation, transaction signing, etc.. WalletHandleTokens expire after a
    46  // configurable number of seconds, and must be renewed periodically with
    47  // RenewWalletHandle. It is good practice to call ReleaseWalletHandle when
    48  // you're done interacting with this wallet.
    49  func (kcl Client) InitWalletHandle(walletID, walletPassword string) (resp InitWalletHandleResponse, err error) {
    50  	req := InitWalletHandleRequest{
    51  		WalletID:       walletID,
    52  		WalletPassword: walletPassword,
    53  	}
    54  	err = kcl.DoV1Request(req, &resp)
    55  	return
    56  }
    57  
    58  // ReleaseWalletHandle invalidates the passed wallet handle token, making
    59  // it unusable for subsequent wallet operations.
    60  func (kcl Client) ReleaseWalletHandle(walletHandle string) (resp ReleaseWalletHandleResponse, err error) {
    61  	req := ReleaseWalletHandleRequest{
    62  		WalletHandleToken: walletHandle,
    63  	}
    64  	err = kcl.DoV1Request(req, &resp)
    65  	return
    66  }
    67  
    68  // RenewWalletHandle accepts a wallet handle and attempts to renew it, moving
    69  // the expiration time to some number of seconds in the future. It returns a
    70  // RenewWalletHandleResponse containing the walletHandle and the number of
    71  // seconds until expiration
    72  func (kcl Client) RenewWalletHandle(walletHandle string) (resp RenewWalletHandleResponse, err error) {
    73  	req := RenewWalletHandleRequest{
    74  		WalletHandleToken: walletHandle,
    75  	}
    76  	err = kcl.DoV1Request(req, &resp)
    77  	return
    78  }
    79  
    80  // RenameWallet accepts a wallet ID, wallet password, and a new wallet name,
    81  // and renames the underlying wallet.
    82  func (kcl Client) RenameWallet(walletID, walletPassword, newWalletName string) (resp RenameWalletResponse, err error) {
    83  	req := RenameWalletRequest{
    84  		WalletID:       walletID,
    85  		WalletPassword: walletPassword,
    86  		NewWalletName:  newWalletName,
    87  	}
    88  	err = kcl.DoV1Request(req, &resp)
    89  	return
    90  }
    91  
    92  // GetWallet accepts a wallet handle and returns high level information about
    93  // this wallet in a GetWalletResponse.
    94  func (kcl Client) GetWallet(walletHandle string) (resp GetWalletResponse, err error) {
    95  	req := GetWalletRequest{
    96  		WalletHandleToken: walletHandle,
    97  	}
    98  	err = kcl.DoV1Request(req, &resp)
    99  	return
   100  }
   101  
   102  // ExportMasterDerivationKey accepts a wallet handle and a wallet password, and
   103  // returns an ExportMasterDerivationKeyResponse containing the master
   104  // derivation key. This key can be used as an argument to CreateWallet in
   105  // order to recover the keys generated by this wallet. The master derivation
   106  // key can be encoded as a sequence of words using the mnemonic library, and
   107  // displayed to the user as a backup phrase.
   108  func (kcl Client) ExportMasterDerivationKey(walletHandle, walletPassword string) (resp ExportMasterDerivationKeyResponse, err error) {
   109  	req := ExportMasterDerivationKeyRequest{
   110  		WalletHandleToken: walletHandle,
   111  		WalletPassword:    walletPassword,
   112  	}
   113  	err = kcl.DoV1Request(req, &resp)
   114  	return
   115  }
   116  
   117  // ImportKey accepts a wallet handle and an ed25519 private key, and imports
   118  // the key into the wallet. It returns an ImportKeyResponse containing the
   119  // address corresponding to this private key.
   120  func (kcl Client) ImportKey(walletHandle string, secretKey ed25519.PrivateKey) (resp ImportKeyResponse, err error) {
   121  	req := ImportKeyRequest{
   122  		WalletHandleToken: walletHandle,
   123  		PrivateKey:        secretKey,
   124  	}
   125  	err = kcl.DoV1Request(req, &resp)
   126  	return
   127  }
   128  
   129  // ExportKey accepts a wallet handle, wallet password, and address, and returns
   130  // an ExportKeyResponse containing the ed25519 private key corresponding to the
   131  // address stored in the wallet.
   132  func (kcl Client) ExportKey(walletHandle, walletPassword, addr string) (resp ExportKeyResponse, err error) {
   133  	req := ExportKeyRequest{
   134  		WalletHandleToken: walletHandle,
   135  		WalletPassword:    walletPassword,
   136  		Address:           addr,
   137  	}
   138  	err = kcl.DoV1Request(req, &resp)
   139  	return
   140  }
   141  
   142  // GenerateKey accepts a wallet handle, and then generates the next key in the
   143  // wallet using its internal master derivation key. Two wallets with the same
   144  // master derivation key will generate the same sequence of keys.
   145  func (kcl Client) GenerateKey(walletHandle string) (resp GenerateKeyResponse, err error) {
   146  	req := GenerateKeyRequest{
   147  		WalletHandleToken: walletHandle,
   148  		DisplayMnemonic:   false,
   149  	}
   150  	err = kcl.DoV1Request(req, &resp)
   151  	return
   152  }
   153  
   154  // DeleteKey accepts a wallet handle, wallet password, and address, and deletes
   155  // the information about this address from the wallet (including address and
   156  // secret key). If DeleteKey is called on a key generated using GenerateKey,
   157  // the same key will not be generated again. However, if a wallet is recovered
   158  // using the master derivation key, a key generated in this way can be
   159  // recovered.
   160  func (kcl Client) DeleteKey(walletHandle, walletPassword, addr string) (resp DeleteKeyResponse, err error) {
   161  	req := DeleteKeyRequest{
   162  		WalletHandleToken: walletHandle,
   163  		WalletPassword:    walletPassword,
   164  		Address:           addr,
   165  	}
   166  	err = kcl.DoV1Request(req, &resp)
   167  	return
   168  }
   169  
   170  // ListKeys accepts a wallet handle and returns a ListKeysResponse containing
   171  // all of the addresses for which this wallet contains secret keys.
   172  func (kcl Client) ListKeys(walletHandle string) (resp ListKeysResponse, err error) {
   173  	req := ListKeysRequest{
   174  		WalletHandleToken: walletHandle,
   175  	}
   176  	err = kcl.DoV1Request(req, &resp)
   177  	return
   178  }
   179  
   180  // SignTransactionWithSpecificPublicKey accepts a wallet handle, a wallet password, a transaction,
   181  // and a public key to sign with its corresponding sk, and returns and SignTransactionResponse containing
   182  // an encoded, signed transaction. The transaction is signed using the key corresponding to the
   183  // Sender field.
   184  func (kcl Client) SignTransactionWithSpecificPublicKey(walletHandle, walletPassword string, tx types.Transaction, pk ed25519.PublicKey) (resp SignTransactionResponse, err error) {
   185  	txBytes := msgpack.Encode(tx)
   186  	req := SignTransactionRequest{
   187  		WalletHandleToken: walletHandle,
   188  		WalletPassword:    walletPassword,
   189  		Transaction:       txBytes,
   190  		PublicKey:         pk,
   191  	}
   192  	err = kcl.DoV1Request(req, &resp)
   193  	return
   194  }
   195  
   196  // SignTransaction accepts a wallet handle, a wallet password, a transaction,
   197  // and returns SignTransactionResponse containing an encoded, signed
   198  // transaction. The transaction is signed using the key corresponding to the
   199  // Sender field.
   200  func (kcl Client) SignTransaction(walletHandle, walletPassword string, tx types.Transaction) (resp SignTransactionResponse, err error) {
   201  	txBytes := msgpack.Encode(tx)
   202  	req := SignTransactionRequest{
   203  		WalletHandleToken: walletHandle,
   204  		WalletPassword:    walletPassword,
   205  		Transaction:       txBytes,
   206  	}
   207  	err = kcl.DoV1Request(req, &resp)
   208  	return
   209  }
   210  
   211  // ListMultisig accepts a wallet handle and returns a ListMultisigResponse
   212  // containing the multisig addresses whose preimages are stored in this wallet.
   213  // A preimage is the information needed to reconstruct this multisig address,
   214  // including multisig version information, threshold information, and a list
   215  // of public keys.
   216  func (kcl Client) ListMultisig(walletHandle string) (resp ListMultisigResponse, err error) {
   217  	req := ListMultisigRequest{
   218  		WalletHandleToken: walletHandle,
   219  	}
   220  	err = kcl.DoV1Request(req, &resp)
   221  	return
   222  }
   223  
   224  // ImportMultisig accepts a wallet handle and the information required to
   225  // generate a multisig address. It derives this address, and stores all of the
   226  // information within the wallet. It returns a ImportMultisigResponse with the
   227  // derived address.
   228  func (kcl Client) ImportMultisig(walletHandle string, version, threshold uint8, pks []ed25519.PublicKey) (resp ImportMultisigResponse, err error) {
   229  	req := ImportMultisigRequest{
   230  		WalletHandleToken: walletHandle,
   231  		Version:           version,
   232  		Threshold:         threshold,
   233  		PKs:               pks,
   234  	}
   235  	err = kcl.DoV1Request(req, &resp)
   236  	return
   237  }
   238  
   239  // ExportMultisig accepts a wallet handle, wallet password, and multisig
   240  // address, and returns an ExportMultisigResponse containing the stored
   241  // multisig preimage. The preimage contains all of the information necessary
   242  // to derive the multisig address, including version, threshold, and a list of
   243  // public keys.
   244  func (kcl Client) ExportMultisig(walletHandle, walletPassword, addr string) (resp ExportMultisigResponse, err error) {
   245  	req := ExportMultisigRequest{
   246  		WalletHandleToken: walletHandle,
   247  		WalletPassword:    walletPassword,
   248  		Address:           addr,
   249  	}
   250  	err = kcl.DoV1Request(req, &resp)
   251  	return
   252  }
   253  
   254  // DeleteMultisig accepts a wallet handle, wallet password, and address, and deletes
   255  // the information about this multisig address from the wallet.
   256  func (kcl Client) DeleteMultisig(walletHandle, walletPassword, addr string) (resp DeleteMultisigResponse, err error) {
   257  	req := DeleteMultisigRequest{
   258  		WalletHandleToken: walletHandle,
   259  		WalletPassword:    walletPassword,
   260  		Address:           addr,
   261  	}
   262  	err = kcl.DoV1Request(req, &resp)
   263  	return
   264  }
   265  
   266  // MultisigSignTransaction accepts a wallet handle, wallet password,
   267  // transaction, public key (*not* an address), and an optional partial
   268  // MultisigSig. It looks up the secret key corresponding to the public key, and
   269  // returns a SignMultisigTransactionResponse containing a MultisigSig with a
   270  // signature by the secret key included.
   271  func (kcl Client) MultisigSignTransaction(walletHandle, walletPassword string, tx types.Transaction, pk ed25519.PublicKey, partial types.MultisigSig) (resp SignMultisigTransactionResponse, err error) {
   272  	txBytes := msgpack.Encode(tx)
   273  	req := SignMultisigTransactionRequest{
   274  		WalletHandleToken: walletHandle,
   275  		WalletPassword:    walletPassword,
   276  		Transaction:       txBytes,
   277  		PublicKey:         pk,
   278  		PartialMsig:       partial,
   279  	}
   280  	err = kcl.DoV1Request(req, &resp)
   281  	return
   282  }