Skip to content

Commit

Permalink
Merge pull request #81 from MinterTeam/dev
Browse files Browse the repository at this point in the history
MN-228: Update deep links
  • Loading branch information
grkamil authored Mar 11, 2020
2 parents bc6e4f9 + 67c5ecf commit d13bb9f
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 71 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,6 @@ $address = MinterWallet::getAddressFromPublicKey($publicKey);

* Create Minter deep link.
* You can pass data of any Minter transaction to the constructor.
* Payload is required.

```php
use Minter\SDK\MinterDeepLink;
Expand All @@ -876,12 +875,11 @@ $txData = new MinterSendCoinTx([
]);

$link = new MinterDeepLink($txData);
$link->setPayload('Hello World');

$link->encode(); // returns encoded link as string
```

* You can define optional fields such as nonce, gas price, gas coin.
* You can define optional fields such as host, payload, nonce, gas price, gas coin, check password.

```php
use Minter\SDK\MinterDeepLink;
Expand All @@ -894,10 +892,12 @@ $txData = new MinterSendCoinTx([
]);

$link = new MinterDeepLink($txData);
$link->setPayload('Hello World');
$link->setNonce($nonce);
$link->setGasPrice($gasPrice);
$link->setGasCoin($gasCoin);
$link->setPayload('Hello World')
->setNonce($nonce);
->setGasPrice($gasPrice);
->setGasCoin($gasCoin);
->setHost('https://testnet.bip.to/tx');
->setPassword('some check password');

$link->encode(); // returns encoded link as string
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"minter/minter-php-bip-44": "1.1.0",
"bitwasp/bitcoin-lib": "1.0.4",
"simplito/elliptic-php": "1.0.6",
"minter/php-rlp": "^0.2.4",
"minter/php-rlp": "^1.0.0",
"ext-json": "*"
},
"minimum-stability": "dev",
Expand Down
11 changes: 10 additions & 1 deletion src/Minter/Library/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static function rlpArrayToHexArray(array $rlp): array
{
return array_map(function($item) {
if(!is_array($item)) {
return $item->toString('hex');
return (string) $item;
}

return self::rlpArrayToHexArray($item);
Expand Down Expand Up @@ -189,4 +189,13 @@ public static function str2buffer(string $str): Buffer
$splitted = str_split($str, 1);
return new Buffer($splitted);
}

/**
* @param $data
* @return string
*/
public static function base64urlEncode($data): string
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
}
8 changes: 4 additions & 4 deletions src/Minter/SDK/MinterCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function sign(string $privateKey): string
$this->structure = array_merge($this->structure, Helper::hex2buffer($signature));

// rlp encode data and add Minter wallet prefix
return MinterPrefix::CHECK . $this->rlp->encode($this->structure)->toString('hex');
return MinterPrefix::CHECK . $this->rlp->encode($this->structure);
}

/**
Expand Down Expand Up @@ -172,7 +172,7 @@ protected function decode(string $check): array
break;

case 'value':
$data[$field] = MinterConverter::convertValue(Helper::hexDecode($value), 'bip');
$data[$field] = MinterConverter::convertToBase(Helper::hexDecode($value));
break;

default:
Expand Down Expand Up @@ -251,7 +251,7 @@ protected function encode(array $check): array

'coin' => MinterConverter::convertCoinName($check['coin']),

'value' => MinterConverter::convertValue($check['value'], 'pip'),
'value' => MinterConverter::convertToPip($check['value']),

'gasCoin' => MinterConverter::convertCoinName($check['gasCoin'])
];
Expand All @@ -265,7 +265,7 @@ protected function encode(array $check): array
protected function serialize($data): string
{
// create msg hash with lock field
$msgHash = $this->rlp->encode($data)->toString('hex');
$msgHash = $this->rlp->encode($data);

return Helper::createKeccakHash($msgHash);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Minter/SDK/MinterCoins/MinterBuyCoinTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public function encode(): array
'coinToBuy' => MinterConverter::convertCoinName($this->data['coinToBuy']),

// Convert field from BIP to PIP
'valueToBuy' => MinterConverter::convertValue($this->data['valueToBuy'], 'pip'),
'valueToBuy' => MinterConverter::convertToPip($this->data['valueToBuy']),

// Add nulls before symbol
'coinToSell' => MinterConverter::convertCoinName($this->data['coinToSell']),

// Convert field from BIP to PIP
'maximumValueToSell' => MinterConverter::convertValue($this->data['maximumValueToSell'], 'pip')
'maximumValueToSell' => MinterConverter::convertToPip($this->data['maximumValueToSell'])
];
}

Expand All @@ -69,13 +69,13 @@ public function decode(array $txData): array
'coinToBuy' => Helper::hex2str($txData[0]),

// Convert field from PIP to BIP
'valueToBuy' => MinterConverter::convertValue(Helper::hexDecode($txData[1]), 'bip'),
'valueToBuy' => MinterConverter::convertToBase(Helper::hexDecode($txData[1])),

// Pack symbol
'coinToSell' => Helper::hex2str($txData[2]),

// Convert field from PIP to BIP
'maximumValueToSell' => MinterConverter::convertValue(Helper::hexDecode($txData[3]), 'bip')
'maximumValueToSell' => MinterConverter::convertToBase(Helper::hexDecode($txData[3]))
];
}
}
12 changes: 6 additions & 6 deletions src/Minter/SDK/MinterCoins/MinterCreateCoinTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public function encode(): array
'symbol' => MinterConverter::convertCoinName($this->data['symbol']),

// Convert field from BIP to PIP
'initialAmount' => MinterConverter::convertValue($this->data['initialAmount'], 'pip'),
'initialAmount' => MinterConverter::convertToPip($this->data['initialAmount']),

// Convert field from BIP to PIP
'initialReserve' => MinterConverter::convertValue($this->data['initialReserve'], 'pip'),
'initialReserve' => MinterConverter::convertToPip($this->data['initialReserve']),

// Define crr field
'crr' => $this->data['crr'] === 0 ? '' : $this->data['crr'],

// Convert field from BIP to PIP
'maxSupply' => MinterConverter::convertValue($this->data['maxSupply'], 'pip')
'maxSupply' => MinterConverter::convertToPip($this->data['maxSupply'])
];
}

Expand All @@ -80,16 +80,16 @@ public function decode(array $txData): array
'symbol' => Helper::hex2str($txData[1]),

// Convert field from PIP to BIP
'initialAmount' => MinterConverter::convertValue(Helper::hexDecode($txData[2]), 'bip'),
'initialAmount' => MinterConverter::convertToBase(Helper::hexDecode($txData[2])),

// Convert field from PIP to BIP
'initialReserve' => MinterConverter::convertValue(Helper::hexDecode($txData[3]), 'bip'),
'initialReserve' => MinterConverter::convertToBase(Helper::hexDecode($txData[3])),

// Convert crr field from hex string to number
'crr' => hexdec($txData[4]),

// Convert field from BIP to PIP
'maxSupply' => MinterConverter::convertValue(Helper::hexDecode($txData[5]), 'bip')
'maxSupply' => MinterConverter::convertToBase(Helper::hexDecode($txData[5]))
];
}
}
4 changes: 2 additions & 2 deletions src/Minter/SDK/MinterCoins/MinterDeclareCandidacyTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function encode(): array
'coin' => MinterConverter::convertCoinName($this->data['coin']),

// Convert stake field from BIP to PIP
'stake' => MinterConverter::convertValue($this->data['stake'], 'pip')
'stake' => MinterConverter::convertToPip($this->data['stake'])
];
}

Expand All @@ -87,7 +87,7 @@ public function decode(array $txData): array
'coin' => Helper::hex2str($txData[3]),

// Convert stake from PIP to BIP
'stake' => MinterConverter::convertValue(Helper::hexDecode($txData[4]), 'bip')
'stake' => MinterConverter::convertToBase(Helper::hexDecode($txData[4]))
];
}
}
4 changes: 2 additions & 2 deletions src/Minter/SDK/MinterCoins/MinterDelegateTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function encode(): array
'coin' => MinterConverter::convertCoinName($this->data['coin']),

// Convert stake field from BIP to PIP
'stake' => MinterConverter::convertValue($this->data['stake'], 'pip')
'stake' => MinterConverter::convertToPip($this->data['stake'])
];
}

Expand All @@ -71,7 +71,7 @@ public function decode(array $txData): array
'coin' => Helper::hex2str($txData[1]),

// Convert stake from PIP to BIP
'stake' => MinterConverter::convertValue(Helper::hexDecode($txData[2]), 'bip')
'stake' => MinterConverter::convertToBase(Helper::hexDecode($txData[2]))
];
}
}
4 changes: 2 additions & 2 deletions src/Minter/SDK/MinterCoins/MinterSellAllCoinTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function encode(): array
'coinToBuy' => MinterConverter::convertCoinName($this->data['coinToBuy']),

// Convert field from BIP to PIP
'minimumValueToBuy' => MinterConverter::convertValue($this->data['minimumValueToBuy'], 'pip')
'minimumValueToBuy' => MinterConverter::convertToPip($this->data['minimumValueToBuy'])
];
}

Expand All @@ -68,7 +68,7 @@ public function decode(array $txData): array
'coinToBuy' => Helper::hex2str($txData[1]),

// Convert field from PIP to BIP
'minimumValueToBuy' => MinterConverter::convertValue(Helper::hexDecode($txData[2]), 'bip')
'minimumValueToBuy' => MinterConverter::convertToBase(Helper::hexDecode($txData[2]))
];
}
}
8 changes: 4 additions & 4 deletions src/Minter/SDK/MinterCoins/MinterSellCoinTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public function encode(): array
'coinToSell' => MinterConverter::convertCoinName($this->data['coinToSell']),

// Convert field from BIP to PIP
'valueToSell' => MinterConverter::convertValue($this->data['valueToSell'], 'pip'),
'valueToSell' => MinterConverter::convertToPip($this->data['valueToSell']),

// Add nulls before symbol
'coinToBuy' => MinterConverter::convertCoinName($this->data['coinToBuy']),

// Convert field from BIP to PIP
'minimumValueToBuy' => MinterConverter::convertValue($this->data['minimumValueToBuy'], 'pip')
'minimumValueToBuy' => MinterConverter::convertToPip($this->data['minimumValueToBuy'])
];
}

Expand All @@ -69,13 +69,13 @@ public function decode(array $txData): array
'coinToSell' => Helper::hex2str($txData[0]),

// Convert field from PIP to BIP
'valueToSell' => MinterConverter::convertValue(Helper::hexDecode($txData[1]), 'bip'),
'valueToSell' => MinterConverter::convertToBase(Helper::hexDecode($txData[1])),

// Pack symbol
'coinToBuy' => Helper::hex2str($txData[2]),

// Convert field from PIP to BIP
'minimumValueToBuy' => MinterConverter::convertValue(Helper::hexDecode($txData[3]), 'bip')
'minimumValueToBuy' => MinterConverter::convertToBase(Helper::hexDecode($txData[3]))
];
}
}
4 changes: 2 additions & 2 deletions src/Minter/SDK/MinterCoins/MinterSendCoinTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function encode(): array
),

// Convert from BIP to PIP
'value' => MinterConverter::convertValue($this->data['value'], 'pip')
'value' => MinterConverter::convertToPip($this->data['value'])
];
}

Expand All @@ -70,7 +70,7 @@ public function decode(array $txData): array
'to' => Helper::addWalletPrefix($txData[1]),

// Convert value from PIP to BIP
'value' => MinterConverter::convertValue(Helper::hexDecode($txData[2]), 'bip')
'value' => MinterConverter::convertToBase(Helper::hexDecode($txData[2]))
];
}
}
4 changes: 2 additions & 2 deletions src/Minter/SDK/MinterCoins/MinterUnbondTx.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function encode(): array
'coin' => MinterConverter::convertCoinName($this->data['coin']),

// Convert from BIP to PIP
'value' => MinterConverter::convertValue($this->data['value'], 'pip')
'value' => MinterConverter::convertToPip($this->data['value'])
];
}

Expand All @@ -71,7 +71,7 @@ public function decode(array $txData): array
'coin' => Helper::hex2str($txData[1]),

// Convert value from PIP to BIP
'value' => MinterConverter::convertValue(Helper::hexDecode($txData[2]), 'bip')
'value' => MinterConverter::convertToBase(Helper::hexDecode($txData[2]))
];
}
}
19 changes: 19 additions & 0 deletions src/Minter/SDK/MinterConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ class MinterConverter
*/
const DEFAULT = '1000000000000000000';

/**
* @param string $num
* @return string
*/
public static function convertToPip(string $num): string
{
return bcmul(self::DEFAULT, $num, 0);
}

/**
* @param string $num
* @return string
*/
public static function convertToBase(string $num): string
{
$num = bcdiv($num, self::DEFAULT, 25);
return Helper::niceNumber($num);
}

/**
* Convert value
*
Expand Down
Loading

0 comments on commit d13bb9f

Please sign in to comment.