-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from tkhq/olivia/export
Export private keys and wallets
- Loading branch information
Showing
12 changed files
with
506 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
59e258836b3ac1d15efb49c2ff9637bcf8d72aa6a74165999525b0520af3c16c turnkey.darwin-aarch64 | ||
da1534435bf06f6c988ec01c48c3a0645984327a349e26156c1d142df3d27ef4 turnkey.darwin-x86_64 | ||
ea239121f7c7816532f9bc36563439b86f53da6b8d2ce7b7342a2eda29dbfd07 turnkey.linux-aarch64 | ||
10e01fce0e0287bead3a3a600d43eb64510a68937d7c9345d9484bb1e3853e38 turnkey.linux-x86_64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/rotisserie/eris" | ||
"github.com/spf13/cobra" | ||
"github.com/tkhq/go-sdk/pkg/enclave_encrypt" | ||
"github.com/tkhq/go-sdk/pkg/encryptionkey" | ||
) | ||
|
||
var ( | ||
// Filepath to write the export bundle to. | ||
exportBundlePath string | ||
|
||
// EncryptionKeypair is the loaded Encryption Keypair. | ||
EncryptionKeypair *encryptionkey.Key | ||
) | ||
|
||
func init() { | ||
decryptCmd.Flags().StringVar(&exportBundlePath, "export-bundle-input", "", "filepath to read the export bundle from.") | ||
decryptCmd.Flags().StringVar(&plaintextPath, "plaintext-output", "", "optional filepath to write the plaintext from that will be decrypted.") | ||
|
||
rootCmd.AddCommand(decryptCmd) | ||
} | ||
|
||
var decryptCmd = &cobra.Command{ | ||
Use: "decrypt", | ||
Short: "Decrypt a ciphertext", | ||
Long: `Decrypt a ciphertext from a bundle exported from a Turnkey secure enclave.`, | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
basicSetup(cmd) | ||
LoadEncryptionKeypair("") | ||
}, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if exportBundlePath == "" { | ||
OutputError(eris.New("--export-bundle-input must be specified")) | ||
} | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// read from export bundle path | ||
exportBundle, err := readFile(exportBundlePath) | ||
if err != nil { | ||
OutputError(err) | ||
} | ||
|
||
// get encryption key | ||
tkPrivateKey := EncryptionKeypair.GetPrivateKey() | ||
kemPrivateKey, err := encryptionkey.DecodeTurnkeyPrivateKey(tkPrivateKey) | ||
if err != nil { | ||
OutputError(eris.Wrap(err, "failed to decode encryption private key")) | ||
} | ||
|
||
// set up enclave encrypt client | ||
signerPublic, err := hexToPublicKey(signerPublicKey) | ||
if err != nil { | ||
OutputError(err) | ||
} | ||
|
||
encryptClient, err := enclave_encrypt.NewEnclaveEncryptClientFromTargetKey(signerPublic, *kemPrivateKey) | ||
if err != nil { | ||
OutputError(err) | ||
} | ||
|
||
// decrypt ciphertext | ||
plaintextBytes, err := encryptClient.Decrypt([]byte(exportBundle), Organization) | ||
if err != nil { | ||
OutputError(err) | ||
} | ||
|
||
plaintext := string(plaintextBytes) | ||
|
||
// output the plaintext if no filepath is passed | ||
if plaintextPath == "" { | ||
Output(plaintext) | ||
return | ||
} | ||
|
||
err = writeFile(plaintext, plaintextPath) | ||
if err != nil { | ||
OutputError(err) | ||
} | ||
}, | ||
} | ||
|
||
// LoadEncryptionKeypair require-loads the keypair referenced by the given name or as referenced form the global KeyName variable, if name is empty. | ||
func LoadEncryptionKeypair(name string) { | ||
if name == "" { | ||
name = EncryptionKeyName | ||
} | ||
|
||
if encryptionKeyStore == nil { | ||
OutputError(eris.New("encryption keystore not loaded")) | ||
} | ||
|
||
encryptionKey, err := encryptionKeyStore.Load(name) | ||
if err != nil { | ||
OutputError(err) | ||
} | ||
|
||
if encryptionKey == nil { | ||
OutputError(eris.New("Encryption key not loaded")) | ||
} | ||
|
||
EncryptionKeypair = encryptionKey | ||
|
||
// If we haven't had the organization explicitly set try to load it from key metadata. | ||
if Organization == "" { | ||
Organization = encryptionKey.Organization | ||
} | ||
|
||
// If org is _still_ empty, the encryption key is not usable. | ||
if Organization == "" { | ||
OutputError(eris.New("failed to associate the encryption key with an organization; please manually specify the organization ID")) | ||
} | ||
|
||
// If we haven't had the user explicitly set try to load it from key metadata. | ||
if User == "" { | ||
User = encryptionKey.User | ||
} | ||
|
||
// If user is _still_ empty, the encryption key is not usable. | ||
if User == "" { | ||
OutputError(eris.New("failed to associate the encryption key with a user; please manually specify the user ID")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.