Skip to content

Commit

Permalink
Merge pull request #62 from MinterTeam/dev
Browse files Browse the repository at this point in the history
Update MinterWallet
  • Loading branch information
grkamil authored Jun 7, 2019
2 parents ded71a8 + db36f43 commit 91a5df1
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This is a pure PHP SDK for working with <b>Minter</b> blockchain
- [Get hash of transaction](#get-hash-of-transaction)
- [Decode Transaction](#decode-transaction)
- [Minter Check](#create-minter-check)
- [Minter Wallet](#minter-wallet)
* [Tests](#tests)

## Installing
Expand Down Expand Up @@ -722,6 +723,58 @@ $check->getBody(); // check body
$check->getOwnerAddress(); // check owner address
```

### Minter Wallet

###### Example

* Create wallet. This method returns generated seed, private key, public key, mnemonic and Minter address.

```php
use Minter\SDK\MinterWallet;

$wallet = MinterWallet::create();
```

* Generate mnemonic.

```php
use Minter\SDK\MinterWallet;

$mnemonic = MinterWallet::generateMnemonic();
```

* Get seed from mnemonic.

```php
use Minter\SDK\MinterWallet;

$seed = MinterWallet::mnemonicToSeed($mnemonic);
```

* Get private key from seed.

```php
use Minter\SDK\MinterWallet;

$privateKey = MinterWallet::seedToPrivateKey($seed);
```

* Get public key from private key.

```php
use Minter\SDK\MinterWallet;

$publicKey = MinterWallet::privateToPublic($privateKey);
```

* Get Minter address from public key.

```php
use Minter\SDK\MinterWallet;

$address = MinterWallet::getAddressFromPublicKey($publicKey);
```

## Tests

To run unit tests:
Expand Down
46 changes: 39 additions & 7 deletions src/Minter/SDK/MinterWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ class MinterWallet
*/
public static function create(): array
{
$entropy = BIP39::generateEntropy(self::BIP44_ENTROPY_BITS);
$mnemonic = BIP39::entropyToMnemonic($entropy);
$seed = BIP39::mnemonicToSeedHex($mnemonic, '');
$privateKey = BIP44::fromMasterSeed($seed)->derive(self::BIP44_SEED_ADDRESS_PATH)->privateKey;

$mnemonic = self::generateMnemonic();
$seed = self::mnemonicToSeed($mnemonic);
$privateKey = self::seedToPrivateKey($seed);
$publicKey = self::privateToPublic($privateKey);
$address = self::getAddressFromPublicKey($publicKey);

Expand All @@ -48,7 +46,7 @@ public static function create(): array
'private_key' => $privateKey
];
}

/**
* Generate public key
*
Expand All @@ -57,7 +55,7 @@ public static function create(): array
*/
public static function privateToPublic(string $privateKey): string
{
return MinterPrefix::PUBLIC_KEY . ECDSA::privateToPublic($privateKey);
return MinterPrefix::PUBLIC_KEY . ECDSA::privateToPublic($privateKey);
}

/**
Expand All @@ -78,6 +76,40 @@ public static function getAddressFromPublicKey(string $publicKey): string
return MinterPrefix::ADDRESS . substr($hash, -40);
}

/**
* Generate mnemonic phrase from entropy.
*
* @return string
*/
public static function generateMnemonic(): string
{
return BIP39::entropyToMnemonic(
BIP39::generateEntropy(self::BIP44_ENTROPY_BITS)
);
}

/**
* Get seed from the mnemonic phrase.
*
* @param string $mnemonic
* @return string
*/
public static function mnemonicToSeed(string $mnemonic): string
{
return BIP39::mnemonicToSeedHex($mnemonic, '');
}

/**
* Get private key from seed.
*
* @param string $seed
* @return string
*/
public static function seedToPrivateKey(string $seed): string
{
return BIP44::fromMasterSeed($seed)->derive(self::BIP44_SEED_ADDRESS_PATH)->privateKey;
}

/**
* Validate that address is valid Minter address
*
Expand Down
21 changes: 21 additions & 0 deletions tests/MinterWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ final class MinterWalletTest extends TestCase
*/
const VALID_ADDRESS = 'Mx17b1240ba6d45258f836b45ae0c4fc1106f5ce59';

// Data for testing mnemonicToSeed, seedToPrivateKey
const MNEMONIC = 'suffer draft bacon typical start retire air sniff large biology mail diagram';
const VALID_SEED = '33fa1096997d9b0f47469463710b3a2e91971144265b281dc71f831539a3b8e3413e5969e5ffb4d3c5a37cfa0f964bcc779efe4ae37fceef048175105caad624';
const VALID_PRIVATE_KEY_FROM_SEED = 'd3520cc797f12b8a81e805ddf5a5bf8b994e347003ea25c9ccaecb5073f3fef1';

/**
* Test converting private key to public key.
*/
Expand Down Expand Up @@ -103,4 +108,20 @@ public function testCreateWallet()
MinterWallet::privateToPublic($wallet['private_key'])
);
}

/**
* Test mnemonic to seed.
*/
public function testMnemonicToSeed()
{
$this->assertEquals(self::VALID_SEED, MinterWallet::mnemonicToSeed(self::MNEMONIC));
}

/**
* Test seed to private key.
*/
public function testSeedToPrivateKey()
{
$this->assertEquals(self::VALID_PRIVATE_KEY_FROM_SEED, MinterWallet::seedToPrivateKey(self::VALID_SEED));
}
}

0 comments on commit 91a5df1

Please sign in to comment.