-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
352 additions
and
82 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
101 changes: 101 additions & 0 deletions
101
tools/wasp-cli/cli/keychain/keychain_zalando_windows.go
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,101 @@ | ||
package keychain | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"unsafe" | ||
|
||
"github.com/awnumar/memguard" | ||
"github.com/zalando/go-keyring" | ||
|
||
iotago "github.com/iotaledger/iota.go/v3" | ||
"github.com/iotaledger/wasp/packages/cryptolib" | ||
) | ||
|
||
const ( | ||
strongholdKey = "wasp-cli.stronghold.key" | ||
jwtTokenKeyPrefix = "wasp-cli.auth.jwt" | ||
seedKey = "wasp-cli.seed" | ||
) | ||
|
||
var ( | ||
ErrTokenDoesNotExist = errors.New("jwt token not found, call 'login'") | ||
ErrPasswordDoesNotExist = errors.New("stronghold entry not found, call 'init'") | ||
ErrSeedDoesNotExist = errors.New("seed not found, call 'init'") | ||
ErrSeedDoesNotMatchLength = errors.New("returned seed does not have a valid length") | ||
) | ||
|
||
const WaspCliServiceName = "IOTAFoundation.WaspCLI" | ||
|
||
func SetSeed(seed cryptolib.Seed) error { | ||
err := keyring.Set(WaspCliServiceName, seedKey, iotago.EncodeHex(seed[:])) | ||
return err | ||
} | ||
|
||
func GetSeed() (*cryptolib.Seed, error) { | ||
seedItem, err := keyring.Get(WaspCliServiceName, seedKey) | ||
if errors.Is(err, keyring.ErrNotFound) { | ||
return nil, ErrSeedDoesNotExist | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
seedBytes, err := iotago.DecodeHex(seedItem) | ||
if len(seedBytes) != cryptolib.SeedSize { | ||
return nil, ErrSeedDoesNotMatchLength | ||
} | ||
|
||
seed := cryptolib.SeedFromBytes(seedBytes) | ||
return &seed, nil | ||
} | ||
|
||
func SetStrongholdPassword(password *memguard.Enclave) error { | ||
buffer, err := password.Open() | ||
if err != nil { | ||
return err | ||
} | ||
defer buffer.Destroy() | ||
|
||
return keyring.Set(WaspCliServiceName, strongholdKey, buffer.String()) | ||
} | ||
|
||
func GetStrongholdPassword() (*memguard.Enclave, error) { | ||
enclave, err := Get("service", "user") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
secretPassword, err := enclave.Open() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer secretPassword.Destroy() | ||
|
||
seedItem, err := keyring.Get(WaspCliServiceName, strongholdKey) | ||
if errors.Is(err, keyring.ErrNotFound) { | ||
return nil, ErrPasswordDoesNotExist | ||
} | ||
|
||
memguard.WipeBytes(*(*[]byte)(unsafe.Pointer(&seedItem))) | ||
|
||
return memguard.NewEnclave([]byte(seedItem)), nil | ||
} | ||
|
||
func jwtTokenKey(node string) string { | ||
return fmt.Sprintf("%s.%s", jwtTokenKeyPrefix, node) | ||
} | ||
|
||
func SetJWTAuthToken(node string, token string) error { | ||
return keyring.Set(WaspCliServiceName, jwtTokenKey(node), token) | ||
} | ||
|
||
func GetJWTAuthToken(node string) (string, error) { | ||
seedItem, err := keyring.Get(WaspCliServiceName, jwtTokenKey(node)) | ||
// Special case. If the key is not found, return an empty token. | ||
if errors.Is(err, keyring.ErrNotFound) { | ||
return "", nil | ||
} | ||
|
||
return seedItem, nil | ||
} |
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,49 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/awnumar/memguard" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tyler-smith/go-bip39" | ||
|
||
iotago "github.com/iotaledger/iota.go/v3" | ||
wasp_wallet_sdk "github.com/iotaledger/wasp-wallet-sdk" | ||
"github.com/iotaledger/wasp-wallet-sdk/types" | ||
"github.com/iotaledger/wasp/packages/cryptolib" | ||
) | ||
|
||
func TestMnemonic(t *testing.T) { | ||
seedBytes, _ := iotago.DecodeHex("0xbc278147b72c6af948eced45252c496901e194c9610bfbffea639e18769c7715") | ||
seed := cryptolib.SeedFromBytes(seedBytes) | ||
kp := cryptolib.KeyPairFromSeed(seed) | ||
address := kp.Address().Bech32("rms") | ||
require.Equal(t, address, "rms1qzy0uqyzcm6asngsjxwc76nuar479uukvxa4dapzaz8fx5e3234wxw5mlmz") | ||
fmt.Println(address) | ||
|
||
mnemonic, err := bip39.NewMnemonic(seed[:]) | ||
require.NoError(t, err) | ||
|
||
cwd, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
sdk, err := wasp_wallet_sdk.NewIotaSDK(path.Join(cwd, "../../../../libiota_sdk_native.so")) | ||
require.NoError(t, err) | ||
|
||
manager, err := wasp_wallet_sdk.NewStrongholdSecretManager(sdk, memguard.NewEnclaveRandom(32), "./test.snapshot") | ||
defer os.Remove("./test.snapshot") | ||
|
||
require.NoError(t, err) | ||
|
||
mnemonicBytes := []byte(mnemonic) | ||
success, err := manager.StoreMnemonic(memguard.NewEnclave(mnemonicBytes)) | ||
require.NoError(t, err) | ||
require.True(t, success) | ||
|
||
strongholdAddress, err := manager.GenerateEd25519Address(0, 0, "rms", types.CoinTypeSMR, nil) | ||
require.NoError(t, err) | ||
fmt.Println(strongholdAddress) | ||
} |
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.