Skip to content

Commit

Permalink
Merge pull-request #68
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Oct 28, 2024
2 parents 6923b43 + 92a77e5 commit 32b9bc0
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/cmd/turnkey/pkg/wallets.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
walletAccountCurve string
walletAccountPathFormat string
walletAccountPath string
walletAccountAddress string
)

func init() {
Expand All @@ -27,6 +28,9 @@ func init() {
walletExportCmd.Flags().StringVar(&walletNameOrID, "name", "", "name or ID of wallet to export.")
walletExportCmd.Flags().StringVar(&exportBundlePath, "export-bundle-output", "", "filepath to write the export bundle to.")

walletAccountExportCmd.Flags().StringVar(&walletAccountAddress, "address", "", "address of wallet account to export.")
walletAccountExportCmd.Flags().StringVar(&exportBundlePath, "export-bundle-output", "", "filepath to write the export bundle to.")

walletInitImportCmd.Flags().StringVar(&User, "user", "", "ID of user to importing the wallet")
walletInitImportCmd.Flags().StringVar(&importBundlePath, "import-bundle-output", "", "filepath to write the import bundle to.")

Expand Down Expand Up @@ -202,6 +206,70 @@ var walletExportCmd = &cobra.Command{
},
}

var walletAccountExportCmd = &cobra.Command{
Use: "export",
Short: "Export a wallet account",
PreRun: func(cmd *cobra.Command, args []string) {
if walletAccountAddress == "" {
OutputError(eris.New("--address must be specified"))
}

if EncryptionKeyName == "" {
OutputError(eris.New("--encryption-key-name must be specified"))
}

if exportBundlePath == "" {
OutputError(eris.New("--export-bundle-output must be specified"))
}
},
Run: func(cmd *cobra.Command, args []string) {
tkPublicKey := EncryptionKeypair.GetPublicKey()
kemPublicKey, err := encryptionkey.DecodeTurnkeyPublicKey(tkPublicKey)
if err != nil {
OutputError(eris.Wrap(err, "failed to decode encryption public key"))
}
kemPublicKeyBytes, err := (*kemPublicKey).MarshalBinary()
if err != nil {
OutputError(eris.Wrap(err, "failed to marshal encryption public key"))
}
targetPublicKey := hex.EncodeToString(kemPublicKeyBytes)

activity := string(models.ActivityTypeExportWalletAccount)

params := wallets.NewExportWalletAccountParams()
params.SetBody(&models.ExportWalletAccountRequest{
OrganizationID: &Organization,
Parameters: &models.ExportWalletAccountIntent{
Address: &walletAccountAddress,
TargetPublicKey: &targetPublicKey,
},
TimestampMs: util.RequestTimestamp(),
Type: &activity,
})

if err := params.Body.Validate(nil); err != nil {
OutputError(eris.Wrap(err, "request validation failed"))
}

resp, err := APIClient.V0().Wallets.ExportWalletAccount(params, APIClient.Authenticator)
if err != nil {
OutputError(eris.Wrap(err, "request failed"))
}

if !resp.IsSuccess() {
OutputError(eris.Errorf("failed to export wallet account: %s", resp.Error()))
}

exportBundle := resp.Payload.Activity.Result.ExportWalletAccountResult.ExportBundle
if err := writeFile(*exportBundle, exportBundlePath); err != nil {
OutputError(eris.Wrap(err, "failed to write export bundle to file"))
}

exportedWalletAccountAddress := resp.Payload.Activity.Result.ExportWalletAccountResult.Address
Output(exportedWalletAccountAddress)
},
}

var walletInitImportCmd = &cobra.Command{
Use: "init-import",
Short: "Initialize wallet import",
Expand Down

0 comments on commit 32b9bc0

Please sign in to comment.