From 361fe2a0f000917cd8bffb7cf678d179675966aa Mon Sep 17 00:00:00 2001 From: Christian Rogobete Date: Thu, 16 Jul 2020 22:50:25 +0200 Subject: [PATCH] add sep-005 examples --- CHANGELOG.md | 6 ++ README.md | 32 +++++- documentation/README.md | 2 +- documentation/sdk_examples/README.md | 1 + .../sdk_examples/sep-0005-key-derivation.md | 98 +++++++++++++++++++ lib/src/stellar_sdk.dart | 2 +- pubspec.yaml | 2 +- test/examples_test.dart | 50 ++++++++++ 8 files changed, 188 insertions(+), 5 deletions(-) create mode 100644 documentation/sdk_examples/sep-0005-key-derivation.md diff --git a/CHANGELOG.md b/CHANGELOG.md index b82f448..5735b1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [1.0.3] - 16.Jul.2020. +- SEP-0005 implementation: +- Key Derivation Methods for Stellar Keys +- Update documentation +- Add tests and examples + ## [1.0.2] - 14.Jul.2020. - SEP-0002 implementation - Federation - update documentation diff --git a/README.md b/README.md index ed25554..60674ea 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The Soneso open source Stellar SDK for Flutter is build with Dart and provides A 1. Add the dependency to your pubspec.yaml file: ``` dependencies: - stellar_flutter_sdk: ^1.0.2 + stellar_flutter_sdk: ^1.0.3 ``` 2. Install it (command line or IDE): ``` @@ -77,6 +77,33 @@ print("${keyPair.secretSeed}"); // SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7 ``` +#### Deterministic generation + +The Stellar Ecosystem Proposal [SEP-005](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md) describes methods for key derivation for Stellar Accounts. This improves key storage and moving keys between wallets and apps. + +##### Generate mnemonic + +```dart +String mnemonic = Wallet.generate24WordsMnemonic(); +print(mnemonic); +// mango debris lumber vivid bar risk prosper verify photo put ridge sell range pet indoor lava sister around panther brush twice cattle sauce romance +``` + +##### Generate key pairs +```dart +Wallet wallet = Wallet.from("mango debris lumber vivid bar risk prosper verify photo put ridge sell range pet indoor lava sister around panther brush twice cattle sauce romance"); + +KeyPair keyPair0 = wallet.getKeyPair(index: 0); +print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); +// GBYTVBTOVXBIT23X4YUEE32QBAAA537OAF553FWABUAZHT3FNPN3FUGG : SBEQZ4XGS434POXNQYUXQYFV6JYUHV56U2MNMUZBBBLBGR5X6PUUCYO5 + +KeyPair keyPair1 = wallet.getKeyPair(index: 1); +print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); +// GD5JFZ6U4TBKLWOVGAJQZ4CWRHNVXIFF65BBXZG6UEQE74RUXWAKQVQN : SD3IXULYMZKB6ML7AJW4OLAXKN6U3BYDUMOZLKUZTCCGZXUFXAS7NKIO +``` + +Supported languages are: english, french, spanish, italian, korean, japanese, simplified chinese and traditional chinese. Find more details in our [SEP-005 examples](documentation/sdk_examples/sep-0005-key-derivation.md). + ### 2. Create an account After the key pair generation, you have already got the address, but it is not activated until someone transfers at least 1 lumen into it. @@ -268,7 +295,8 @@ print(response.memo); | [Fee bump transaction](documentation/sdk_examples/fee_bump.md) | Fee bump transactions allow an arbitrary account to pay the fee for a transaction.| [Fee bump transactions](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0015.md)| | [Muxed accounts](documentation/sdk_examples/muxed_account_payment.md) | In this example we will see how to use a muxed account in a payment operation.| [First-class multiplexed accounts](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0027.md)| | [SEP-0001: stellar.toml](documentation/sdk_examples/sep-0001-toml.md) | In this example you can find out how to obtain data about an organization’s Stellar integration.| [SEP-0001](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0001.md)| -| [SEP-0002: Federation](documentation/sdk_examples/sep-0002-federation.md) | This examples shows how to resolve a stellar address, a stellar account id, a transaction id and a forward by using the federation protocol. | [SEP-0002](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0002.md)| +| [SEP-0002: Federation](documentation/sdk_examples/sep-0002-federation.md) | This example shows how to resolve a stellar address, a stellar account id, a transaction id and a forward by using the federation protocol. | [SEP-0002](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0002.md)| +| [SEP-0005: Key derivation](documentation/sdk_examples/sep-0005-key-derivation.md) | In this examples you can see how to generate 12 or 24 words mnemonics for different languages using the Flutter SDK, how to generate key pairs from a mnemonic (with and without BIP 39 passphrase) and how to generate key pairs from a BIP 39 seed. | [SEP-0005](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md)| Additional examples can be found in the [tests](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/). diff --git a/documentation/README.md b/documentation/README.md index 8b0c0de..2692a31 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -34,7 +34,7 @@ The Soneso open source Stellar SDK for Flutter is build with Dart and provides A | [Muxed accounts](sdk_examples/muxed_account_payment.md) | In this example we will see how to use a muxed account in a payment operation.| [First-class multiplexed accounts](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0027.md)| | [SEP-0001: stellar.toml](sdk_examples/sep-0001-toml.md) | In this example you can find out how to obtain data about an organization’s Stellar integration.| [SEP-0001](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0001.md)| | [SEP-0002: Federation](sdk_examples/sep-0002-federation.md) | This examples shows how to resolve a stellar address, a stellar account id, a transaction id and a forward by using the federation protocol. | [SEP-0002](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0002.md)| - +| [SEP-0005: Key derivation](sdk_examples/sep-0005-key-derivation.md) | In this examples you can see how to generate 12 or 24 words mnemonics for different languages using the Flutter SDK, how to generate key pairs from a mnemonic (with and without BIP 39 passphrase) and how to generate key pairs from a BIP 39 seed. | [SEP-0005](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md)| More examples and use cases can be found in the [test classes](../test). diff --git a/documentation/sdk_examples/README.md b/documentation/sdk_examples/README.md index 7d0ff08..a0221bf 100644 --- a/documentation/sdk_examples/README.md +++ b/documentation/sdk_examples/README.md @@ -25,4 +25,5 @@ The [Soneso open source Stellar SDK for Flutter](https://github.com/Soneso/stell | [Muxed accounts](muxed_account_payment.md) | In this example we will see how to use a muxed account in a payment operation.| [First-class multiplexed accounts](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0027.md)| | [SEP-0001: stellar.toml](sep-0001-toml.md) | In this example you can find out how to obtain data about an organization’s Stellar integration.| [SEP-0001](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0001.md)| | [SEP-0002: Federation](sep-0002-federation.md) | This examples shows how to resolve a stellar address, a stellar account id, a transaction id and a forward by using the federation protocol. | [SEP-0002](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0002.md)| +| [SEP-0005: Key derivation](sdk_examples/sep-0005-key-derivation.md) | In this examples you can see how to generate 12 or 24 words mnemonics for different languages using the Flutter SDK, how to generate key pairs from a mnemonic (with and without BIP 39 passphrase) and how to generate key pairs from a BIP 39 seed. | [SEP-0005](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md)| diff --git a/documentation/sdk_examples/sep-0005-key-derivation.md b/documentation/sdk_examples/sep-0005-key-derivation.md new file mode 100644 index 0000000..eaa9315 --- /dev/null +++ b/documentation/sdk_examples/sep-0005-key-derivation.md @@ -0,0 +1,98 @@ + +### SEP-0005 - Key Derivation Methods for Stellar Keys + +Methods for key derivation for Stellar are described in [SEP-005](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0005.md). This improves key storage and moving keys between wallets and apps. + +In the following examples you can see how to generate 12 or 24 words mnemonics for different languages using the Flutter SDK, how to generate key pairs from a mnemonic (with and without BIP 39 passphrase) and how to generate key pairs from a BIP 39 seed. + +### Generate mnemonic + +```dart +String mnemonic = Wallet.generate12WordsMnemonic(); +print(mnemonic); +// twice news void fiction lamp chaos few code rate donkey supreme primary + +mnemonic = Wallet.generate24WordsMnemonic(); +print(mnemonic); +// mango debris lumber vivid bar risk prosper verify photo put ridge sell range pet indoor lava sister around panther brush twice cattle sauce romance +``` +Default language is english. + +### Generate other language mnemonic + +```dart +String frenchMnemonic = Wallet.generate12WordsMnemonic(language: LANGUAGE_FRENCH); +print(frenchMnemonic); +// pouvoir aménager lagune alliage bermuda taxer dogme avancer espadon sucre bermuda aboyer + +String koreanMnemonic = Wallet.generate24WordsMnemonic(language: LANGUAGE_KOREAN); +print(koreanMnemonic); +// 합리적 채널 침대 달걀 기념 정성 세종대왕 한식 불안 독창적 착각 체계 순서 학급 평화 마약 냉면 멀리 남매 초반 치약 여권 지방 물음 +``` +Supported languages are: + +- english +- french +- spanish +- italian +- korean +- japanese +- simplified chinese +- traditional chinese + +### Generate key pairs from mnemonic + +```dart +Wallet wallet = Wallet.from("shell green recycle learn purchase able oxygen right echo claim hill again hidden evidence nice decade panic enemy cake version say furnace garment glue"); + +KeyPair keyPair0 = wallet.getKeyPair(index: 0); +print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); +// GCVSEBHB6CTMEHUHIUY4DDFMWQ7PJTHFZGOK2JUD5EG2ARNVS6S22E3K : SATLGMF3SP2V47SJLBFVKZZJQARDOBDQ7DNSSPUV7NLQNPN3QB7M74XH + +KeyPair keyPair1 = wallet.getKeyPair(index: 1); +print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); +// GBPHPX7SZKYEDV5CVOA5JOJE2RHJJDCJMRWMV4KBOIE5VSDJ6VAESR2W : SCAYXPIDEUVDGDTKF4NGVMN7HCZOTZJ43E62EEYKVUYXEE7HMU4DFQA6 +``` + +### Generate key pairs from mnemonic of other language + +```dart +Wallet wallet = Wallet.from("절차 튀김 건강 평가 테스트 민족 몹시 어른 주민 형제 발레 만점 산길 물고기 방면 여학생 결국 수명 애정 정치 관심 상자 축하 고무신", + language: LANGUAGE_KOREAN); +KeyPair keyPair0 = wallet.getKeyPair(index: 0); +print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); +// GCITEFHNYX3ZCD6XQXPWPZGGS2KTYE4C6RPDUIYOW33PC3PU3PGU667E : SB6KJ2HFH32PXSRATDPSV65DNYCN2XA6RVHKSFI3NSGU5YRSDLB56M76 + +KeyPair keyPair1 = wallet.getKeyPair(index: 1); +print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); +// GB6LTLB32AFIZL5DPOLHYRVZNHGFBBWGJ5DZCVHMBEW3U4DOXHTX3UQV : SBJJXYH3HPBZ2BDJ5NBE3EJLYDPMVBGG7ZZIYGEED2EKWMNKLCVFPAY7 +``` + +### Generate key pairs from mnemonic with BIP 39 passphrase + +```dart +Wallet wallet = Wallet.from("cable spray genius state float twenty onion head street palace net private method loan turn phrase state blanket interest dry amazing dress blast tube", + passphrase: "p4ssphr4se"); + +KeyPair keyPair0 = wallet.getKeyPair(index: 0); +print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); +// GDAHPZ2NSYIIHZXM56Y36SBVTV5QKFIZGYMMBHOU53ETUSWTP62B63EQ : SAFWTGXVS7ELMNCXELFWCFZOPMHUZ5LXNBGUVRCY3FHLFPXK4QPXYP2X + +KeyPair keyPair1 = wallet.getKeyPair(index: 1); +print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); +// GDY47CJARRHHL66JH3RJURDYXAMIQ5DMXZLP3TDAUJ6IN2GUOFX4OJOC : SBQPDFUGLMWJYEYXFRM5TQX3AX2BR47WKI4FDS7EJQUSEUUVY72MZPJF +``` + +### Generate key pairs from BIP 39 seed + +```dart +Wallet wallet = Wallet.fromBip39HexSeed("e4a5a632e70943ae7f07659df1332160937fad82587216a4c64315a0fb39497ee4a01f76ddab4cba68147977f3a147b6ad584c41808e8238a07f6cc4b582f186"); + +KeyPair keyPair0 = wallet.getKeyPair(index: 0); +print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); +// GDRXE2BQUC3AZNPVFSCEZ76NJ3WWL25FYFK6RGZGIEKWE4SOOHSUJUJ6 : SBGWSG6BTNCKCOB3DIFBGCVMUPQFYPA2G4O34RMTB343OYPXU5DJDVMN + +KeyPair keyPair1 = wallet.getKeyPair(index: 1); +print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); +// GBAW5XGWORWVFE2XTJYDTLDHXTY2Q2MO73HYCGB3XMFMQ562Q2W2GJQX : SCEPFFWGAG5P2VX5DHIYK3XEMZYLTYWIPWYEKXFHSK25RVMIUNJ7CTIS +``` \ No newline at end of file diff --git a/lib/src/stellar_sdk.dart b/lib/src/stellar_sdk.dart index 4478529..847f986 100644 --- a/lib/src/stellar_sdk.dart +++ b/lib/src/stellar_sdk.dart @@ -27,7 +27,7 @@ import 'requests/trades_request_builder.dart'; /// Main class of the flutter stellar sdk. class StellarSDK { - static const versionNumber = "1.0.2"; + static const versionNumber = "1.0.3"; static final StellarSDK PUBLIC = new StellarSDK("https://horizon.stellar.org"); diff --git a/pubspec.yaml b/pubspec.yaml index acc3762..d5deb00 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: stellar_flutter_sdk description: A stellar blockchain sdk that query's horizon, build, signs and submits transactions to the stellar netweok. -version: 1.0.2 +version: 1.0.3 homepage: https://github.com/Soneso/stellar_flutter_sdk environment: diff --git a/test/examples_test.dart b/test/examples_test.dart index c1d5c83..6fe44de 100644 --- a/test/examples_test.dart +++ b/test/examples_test.dart @@ -1619,4 +1619,54 @@ void main() { print(response.memoType); print(response.memo); }); + + test('sep 0005 - key derivation ', () async { + String mnemonic = Wallet.generate12WordsMnemonic(); + print(mnemonic); + + mnemonic = Wallet.generate24WordsMnemonic(); + print(mnemonic); + + String frenchMnemonic = + Wallet.generate12WordsMnemonic(language: LANGUAGE_FRENCH); + print(frenchMnemonic); + + String koreanMnemonic = + Wallet.generate24WordsMnemonic(language: LANGUAGE_KOREAN); + print(koreanMnemonic); + + Wallet wallet = Wallet.from( + "shell green recycle learn purchase able oxygen right echo claim hill again hidden evidence nice decade panic enemy cake version say furnace garment glue"); + + KeyPair keyPair0 = wallet.getKeyPair(index: 0); + print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); + + KeyPair keyPair1 = wallet.getKeyPair(index: 1); + print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); + + wallet = Wallet.from( + "절차 튀김 건강 평가 테스트 민족 몹시 어른 주민 형제 발레 만점 산길 물고기 방면 여학생 결국 수명 애정 정치 관심 상자 축하 고무신", + language: LANGUAGE_KOREAN); + keyPair0 = wallet.getKeyPair(index: 0); + print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); + + keyPair1 = wallet.getKeyPair(index: 1); + print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); + + wallet = Wallet.from( + "cable spray genius state float twenty onion head street palace net private method loan turn phrase state blanket interest dry amazing dress blast tube", + passphrase: "p4ssphr4se"); + + keyPair0 = wallet.getKeyPair(index: 0); + print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); + keyPair1 = wallet.getKeyPair(index: 1); + print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); + + wallet = Wallet.fromBip39HexSeed( + "e4a5a632e70943ae7f07659df1332160937fad82587216a4c64315a0fb39497ee4a01f76ddab4cba68147977f3a147b6ad584c41808e8238a07f6cc4b582f186"); + keyPair0 = wallet.getKeyPair(index: 0); + print("${keyPair0.accountId} : ${keyPair0.secretSeed}"); + keyPair1 = wallet.getKeyPair(index: 1); + print("${keyPair1.accountId} : ${keyPair1.secretSeed}"); + }); }