Skip to content

Commit

Permalink
Merge pull request #2 from sc0Vu/init-wallet
Browse files Browse the repository at this point in the history
basic ethereum wallet
  • Loading branch information
sc0Vu authored Apr 28, 2023
2 parents b0afa94 + c6ab461 commit 5d4d554
Show file tree
Hide file tree
Showing 10 changed files with 4,739 additions and 1 deletion.
48 changes: 48 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: PHP

on: ["push", "pull_request", "workflow_dispatch"]

jobs:
build_and_test:
name: Build and test ethereum-wallet with ${{ matrix.php-version }}
strategy:
matrix:
# bitcoin didn't support 8.0 yet, see: https://github.com/Bit-Wasp/bitcoin-php/pull/898
php-version: ["7.2", "7.3", "7.4"]

runs-on: ubuntu-latest

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}

- name: PHP version
run: |
php --version
- uses: actions/checkout@v2

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php-version }}-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: vendor/bin/phpunit --coverage-clover=coverage.xml

- uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ composer.phar

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
composer.lock
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,109 @@
# ethereum-wallet
[![PHP](https://github.com/web3p/ethereum-wallet/actions/workflows/php.yml/badge.svg)](https://github.com/web3p/ethereum-wallet/actions/workflows/php.yml)
[![codecov](https://codecov.io/gh/web3p/ethereum-wallet/branch/master/graph/badge.svg)](https://codecov.io/gh/web3p/ethereum-wallet)
[![Licensed under the MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/web3p/ethereum-wallet/blob/master/LICENSE)

Ethereum wallet.

# Install

```
composer require web3p/ethereum-wallet
```

# Usage

Generate a new wallet:

```php
use Web3p\EthereumWallet\Wallet;

$wallet = new Wallet();
$mnemonicLength = 15;
$wallet->generate($mnemonicLength);

// $wallet->address;
// danger zone, if the data was leaked, money would be stolen
// $wallet->privateKey;
// $wallet->mnemonic;
```

Recover wallet from mnemonic:

```php
use Web3p\EthereumWallet\Wallet;

$wallet = new Wallet();
$mnemonic = '..........';
$wallet->fromMnemonic($mnemonic);

// $wallet->address;
// danger zone, if the data was leaked, money would be stolen
// $wallet->privateKey;
// $wallet->mnemonic;
```

# API

### Web3p\EthereumWallet\Wallet

#### setWordlist

Set different mnemonic wordlist, the default is english.

`setWordlist(WordList $wordlist)`

wordList - \BitWasp\Bitcoin\Mnemonic\WordList

###### Example

```php
use Web3p\EthereumWallet\Wallet;
use Web3p\EthereumWallet\Wordlist\BIP39ChineseTraditionalWordList;

$wallet = new Wallet();
$zh_TW_wordlist = new BIP39ChineseTraditionalWordList;
$wallet->wordlist = $zh_TW_wordlist;
```

#### generate

Returns a new wallet for the given mnemonic length.

`generate(int $mnemonicLength)`

mnemonicLength - integer.

###### Example

* Generate a new wallet with 12 mnemonic.

```php
use Web3p\EthereumWallet\Wallet;

$wallet = new Wallet();
$wallet->generate(12);
```

#### fromMnemonic

Returns a wallet recover from the mnemonic.

`fromMnemonic(string $mnemonic)`

mnemonic - string.

###### Example

* Recover from a wallet.

```php
use Web3p\EthereumWallet\Wallet;

$wallet = new Wallet();
$mnemonic = '..........';
$wallet->fromMnemonic($mnemonic);
```

# License
MIT
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "web3p/ethereum-wallet",
"description": "Ethereum wallet library in PHP.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "sc0Vu",
"email": "[email protected]"
}
],
"require-dev": {
"phpunit/phpunit": "~7 | ~8.0"
},
"autoload": {
"psr-4": {
"Web3p\\EthereumWallet\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Test\\": "test/"
}
},
"require": {
"PHP": "^7.1 | ^8.0",
"web3p/ethereum-util": "^0.1",
"bitwasp/bitcoin": "^1.0"
}
}
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuite name="Ethereum wallet unit test">
<directory suffix="Test.php">./test/unit</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 5d4d554

Please sign in to comment.