Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"php-ds/php-ds": "^1.4",
"simplito/elliptic-php": "^1.0.12",
"simplito/bn-php": "^1.1.4",
"hardcastle/buffer": "^0.2.0",
"hardcastle/buffer": "^1.0.0",
"vlucas/phpdotenv": "^5.6"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/CoreUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static function deriveAddress(Buffer|string $publicKey): string
$_this = self::getInstance();

if (is_string($publicKey)) {
$publicKey = Buffer::from($publicKey);
$publicKey = Buffer::from($publicKey, 'hex');
}

$publicKeyHash = MathUtilities::computePublicKeyHash($publicKey);
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Ctid.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Ctid
*/
public function __construct(string $ctidAsHex)
{
$this->internal = Buffer::from($ctidAsHex);
$this->internal = Buffer::from($ctidAsHex, 'hex');
}

/**
Expand Down
18 changes: 8 additions & 10 deletions src/Core/MathUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ public static function unsignedRightShift(int $value, int $steps): int

public static function computePublicKeyHash(Buffer $bytes): Buffer
{
$binaryValue = hex2bin($bytes->toString());
$hash256 = hash('sha256', $binaryValue, true);
$hash256 = hash('sha256', $bytes->toUtf8(), true);
$hash160 = hash('ripemd160', $hash256, true);
$hexValue = bin2hex($hash160);

return Buffer::from($hexValue)->slice(0, 32);
return Buffer::from($hash160);
}

public static function sha512Half(Buffer|string $input): Buffer
{
if(!is_string($input)) {
$input = $input->toString();
if ($input instanceof Buffer) {
$input = $input->toUtf8();
} else if (preg_match('/^[0-9a-fA-F]+$/', $input)) {
$input = hex2bin($input);
}

$binaryValue = hex2bin($input);
$binaryHash = hash('sha512', $binaryValue, true);
$hexValue = bin2hex($binaryHash);
$binaryHash = hash('sha512', $input, true);

return Buffer::from($hexValue)->slice(0, 32);
return Buffer::from(substr($binaryHash, 0, 32));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleAddressCodec/AddressCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function encodeXAddress(Buffer $accountId, $tag, bool $test = false): str

$hex = array_map(fn($item) => sprintf('%02X', $item), $bytes);

return $this->encodeChecked(Buffer::from(join('', $hex)));
return $this->encodeChecked(Buffer::from(join('', $hex), 'hex'));
}

public function xAddressToClassicAddress(string $xAddress): array
Expand Down
8 changes: 2 additions & 6 deletions src/Core/RippleAddressCodec/BaseX.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,14 @@ public function decodeUnsafe(string $source): ?Buffer
$it4++;
}

$vch = Buffer::from(str_repeat('00', $zeroes + ($size - $it4)));
$vch = $vch->toArray();
$vch = array_fill(0, $zeroes + ($size - $it4), 0);
$j = $zeroes;

while ($it4 !== $size) {
$vch[$j++] = $b256[$it4++];
}

$hexStr = join('', array_map(fn($item) => sprintf('%02X', $item), $vch));

//decimalArrayToHexStr
return Buffer::from($hexStr);
return Buffer::from($vch);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/Core/RippleAddressCodec/Codec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ public function decode(string $base58String, array $options): array

$defaultOptions = [
'versionTypes' => ['ed25519', 'secp256k1'],
'version' => [[1, 225, 75], 33],
'versions' => [[1, 225, 75], [33]],
'expectedLength' => 16
];

$options = array_replace($defaultOptions, $options);

$versions = $options['versions'];
$types = $options['versionTypes'];
$types = $options['versionTypes'] ?? null;


if (count($options['versions']) > 1 && !$options['expectedLength']) {
if (count($versions) > 1 && !$options['expectedLength']) {
throw new Exception('expectedLength is required because there are >= 2 possible versions');
}

Expand All @@ -63,12 +62,14 @@ public function decode(string $base58String, array $options): array
];
}
}

throw new Exception('Unknown version');
}

public function encodeChecked(Buffer $bytes): string
{
$check = $this->sha256($this->sha256($bytes))->slice(0,4);
return $this->encodeRaw(Buffer::concat([$bytes->toArray(), $check->toArray()]));
return $this->encodeRaw(Buffer::concat([$bytes, $check]));
}

public function decodeChecked(string $base58string): Buffer
Expand All @@ -92,7 +93,8 @@ private function encodeVersioned(Buffer $bytes, array $versions, int $expectedLe
throw new Exception('unexpected_payload_length: bytes.length does not match expectedLength. Ensure that the bytes are a Buffer.');
}

$bytes->prependBuffer(Buffer::from($versions));
$versionBytes = is_numeric($versions[0]) ? $versions : $versions[0];
$bytes = Buffer::concat([Buffer::from($versionBytes), $bytes]);

return $this->encodeChecked($bytes);
}
Expand All @@ -109,16 +111,14 @@ private function decodeRaw(string $base58string): Buffer

private function sha256(Buffer $bytes): Buffer
{
$binaryValue = hex2bin($bytes->toString());
$binaryHash = hash('sha256', $binaryValue, true);
$hexValue = bin2hex($binaryHash);
return Buffer::from($hexValue);
$binaryHash = hash('sha256', $bytes->toUtf8(), true);
return Buffer::from($binaryHash);
}

private function verifyCheckSum(Buffer $bytes): bool
{
$computed = $this->sha256($this->sha256($bytes->slice(0,-4)))->slice(0,4);
$checksum = $bytes->slice(-4);
return $computed->toString() === $checksum->toString(); //TODO: Perhaps make Buffer comparable
return $computed->toUtf8() === $checksum->toUtf8();
}
}
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/AccountId.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function fromJson(string $serializedJson): SerializedType
$isHex = (preg_match(self::HEX_REGEX, $serializedJson) === 1);

if ($isHex) {
return new AccountId(Buffer::from($serializedJson));
return new AccountId(Buffer::from($serializedJson, 'hex'));
}

$addressCodec = new AddressCodec();
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/Amount.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function fromJson(string $serializedJson): SerializedType

$hex1 = str_pad($bigInteger->shiftedRight(32)->toBase(16), 8, '0', STR_PAD_LEFT);
$hex2 = str_pad($bigInteger->and(0x00000000ffffffff)->toBase(16), 8, '0', STR_PAD_LEFT);
$amount = Buffer::from($hex1 . $hex2);
$amount = Buffer::from($hex1 . $hex2, 'hex');

$amount[0] |= 0x80;

Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)

public static function fromJson(string $serializedJson): SerializedType
{
return new Blob(Buffer::from($serializedJson));
return new Blob(Buffer::from($serializedJson, 'hex'));
}
}
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function fromJson(string $serializedJson): SerializedType
throw new \Exception('Unsupported Currency representation: ' . $serializedJson);
}

$bytes = strlen($serializedJson) === 3 ? static::isoToBytes($serializedJson) : Buffer::from($serializedJson);
$bytes = strlen($serializedJson) === 3 ? static::isoToBytes($serializedJson) : Buffer::from($serializedJson, 'hex');

return new Currency($bytes);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/Hash128.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)

public static function fromJson(string $serializedJson): SerializedType
{
return new Hash128(Buffer::from($serializedJson));
return new Hash128(Buffer::from($serializedJson, 'hex'));
}
}
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/Hash160.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)

public static function fromJson(string $serializedJson): SerializedType
{
return new Hash160(Buffer::from($serializedJson));
return new Hash160(Buffer::from($serializedJson, 'hex'));
}
}
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/Hash256.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)

public static function fromJson(string $serializedJson): SerializedType
{
return new Hash256(Buffer::from($serializedJson));
return new Hash256(Buffer::from($serializedJson, 'hex'));
}
}
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/UnsignedInt16.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function fromJson(string|int $serializedJson): SerializedType
$serializedJson = (int) json_decode($serializedJson);
}

return new UnsignedInt16(Buffer::from(dechex($serializedJson)));
return new UnsignedInt16(Buffer::from(str_pad(dechex($serializedJson), 4, '0', STR_PAD_LEFT), 'hex'));
}

public function toBytes(): Buffer
Expand Down
4 changes: 2 additions & 2 deletions src/Core/RippleBinaryCodec/Types/UnsignedInt192.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)
public static function fromJson(string|int $serializedJson): SerializedType
{
if (is_int($serializedJson)) {
$serializedJson = (string)$serializedJson;
$serializedJson = (string)$serializedJson;
}

return new UnsignedInt192(Buffer::from($serializedJson));
return new UnsignedInt192(Buffer::from($serializedJson, 'hex'));
}

public function toBytes(): Buffer
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/UnsignedInt32.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function fromJson(string|int $serializedJson): SerializedType
$serializedJson = (int) json_decode($serializedJson);
}

return new UnsignedInt32(Buffer::from(dechex($serializedJson)));
return new UnsignedInt32(Buffer::from(str_pad(dechex($serializedJson), 8, '0', STR_PAD_LEFT), 'hex'));
}

public function toBytes(): Buffer
Expand Down
4 changes: 2 additions & 2 deletions src/Core/RippleBinaryCodec/Types/UnsignedInt384.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)
public static function fromJson(string|int $serializedJson): SerializedType
{
if (is_int($serializedJson)) {
$serializedJson = (string)$serializedJson;
$serializedJson = (string)$serializedJson;
}

return new UnsignedInt384(Buffer::from($serializedJson));
return new UnsignedInt384(Buffer::from($serializedJson, 'hex'));
}

public function toBytes(): Buffer
Expand Down
4 changes: 2 additions & 2 deletions src/Core/RippleBinaryCodec/Types/UnsignedInt512.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)
public static function fromJson(string|int $serializedJson): SerializedType
{
if (is_int($serializedJson)) {
$serializedJson = (string)$serializedJson;
$serializedJson = (string)$serializedJson;
}

return new UnsignedInt512(Buffer::from($serializedJson));
return new UnsignedInt512(Buffer::from($serializedJson, 'hex'));
}

public function toBytes(): Buffer
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/UnsignedInt64.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function fromParser(BinaryParser $parser, ?int $lengthHint = null)
public static function fromJson(string $serializedJson): UnsignedInt64
{
$bigInteger = BigInteger::fromBase($serializedJson, 10);
return new UnsignedInt64(Buffer::from($bigInteger->toBase(16)));
return new UnsignedInt64(Buffer::from(str_pad($bigInteger->toBase(16), 16, '0', STR_PAD_LEFT), 'hex'));
}

public function toBytes(): Buffer
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/UnsignedInt8.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function fromJson(string|int $serializedJson): SerializedType
$serializedJson = (int) json_decode($serializedJson);
}

return new UnsignedInt8(Buffer::from(dechex($serializedJson)));
return new UnsignedInt8(Buffer::from(str_pad(dechex($serializedJson), 2, '0', STR_PAD_LEFT), 'hex'));
}

public function valueOf(): int|string
Expand Down
2 changes: 1 addition & 1 deletion src/Core/RippleBinaryCodec/Types/UnsignedInt96.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function fromJson(string|int $serializedJson): SerializedType
$serializedJson = (string)$serializedJson;
}

return new UnsignedInt96(Buffer::from($serializedJson));
return new UnsignedInt96(Buffer::from($serializedJson, 'hex'));
}

public function toBytes(): Buffer
Expand Down
10 changes: 5 additions & 5 deletions src/Core/RippleKeyPairs/Ed25519KeyPairService.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function deriveKeyPair(Buffer|string $seed, bool $validator = false, int
}

$rawPrivateKey = MathUtilities::sha512Half($seed);
$rawKeyPair = $this->elliptic->keyFromSecret($rawPrivateKey->toString());
$rawKeyPair = $this->elliptic->keyFromSecret(bin2hex($rawPrivateKey->toUtf8()));

$publicKey = self::PREFIX_ED25519 . Buffer::from($rawKeyPair->getPublic())->toString();
$privateKey = self::PREFIX_ED25519 . Buffer::from($rawKeyPair->getSecret())->toString();
Expand All @@ -56,8 +56,8 @@ public function deriveKeyPair(Buffer|string $seed, bool $validator = false, int

public function sign(Buffer|string $message, string $privateKey): string
{
if (!is_string($message)) {
$message = $message->toString();
if ($message instanceof Buffer) {
$message = bin2hex($message->toUtf8());
}

$signed = $this->elliptic->sign($message, substr($privateKey, 2));
Expand All @@ -67,8 +67,8 @@ public function sign(Buffer|string $message, string $privateKey): string

public function verify(Buffer|string $message, string $signature, string $publicKey): bool
{
if (!is_string($message)) {
$message = $message->toString();
if ($message instanceof Buffer) {
$message = bin2hex($message->toUtf8());
}

return $this->elliptic->verify($message, $signature, substr($publicKey, 2));
Expand Down
Loading
Loading