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 .php-cs-fixer.cache

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/Transactions/Deserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public function deserialize(): AbstractTransaction
$data['s'] = $this->parseHex($decodedRlp[8]);
}

// TODO: second signature handling
if (count($decodedRlp) === 10) {
$data['legacySecondSignature'] = $this->parseHex($decodedRlp[9]);
}

$transaction = $this->guessTransactionFromData($data);

Expand Down
14 changes: 12 additions & 2 deletions src/Transactions/Types/AbstractTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\CompactSignatureInterface;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;
use kornrunner\Keccak;

abstract class AbstractTransaction
{
/**
* 0x8f ‖ "MAINSAIL_LSS_V1".
*/
private const LEGACY_SECOND_SIGNATURE_DOMAIN = '8f4d41494e5341494c5f4c53535f5631';

public array $data;

public Buffer $serialized;
Expand Down Expand Up @@ -60,9 +66,13 @@ public function sign(PrivateKey $privateKey): static

public function legacySecondSign(PrivateKey $privateKey): static
{
$hash = $this->hash(skipSignature: true);
if (! isset($this->data['v'], $this->data['r'], $this->data['s']) || $this->data['v'] === '') {
throw new \RuntimeException('Transaction must be signed before adding a legacy second signature');
}

$hash = Keccak::hash(hex2bin(self::LEGACY_SECOND_SIGNATURE_DOMAIN.$this->hash()->getHex()), 256);

$this->data['legacySecondSignature'] = $privateKey->signToEcdsa($hash);
$this->data['legacySecondSignature'] = $privateKey->signToEcdsa(Buffer::hex($hash));

return $this;
}
Expand Down
22 changes: 15 additions & 7 deletions src/Utils/TransactionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class TransactionUtils
*
* @param array $transaction The transaction data.
* @param bool $skipSignature
* @param bool $skipLegacySecondSignature
*/
public static function toBuffer(array $transaction, bool $skipSignature = false): Buffer
public static function toBuffer(array $transaction, bool $skipSignature = false, bool $skipLegacySecondSignature = false): Buffer
{
$fields = [
self::toBeArray(isset($transaction['nonce']) ? $transaction['nonce'] : 0),
Expand All @@ -34,10 +35,10 @@ public static function toBuffer(array $transaction, bool $skipSignature = false)

if (! $skipSignature && isset($transaction['v'], $transaction['r'], $transaction['s']) && $transaction['v'] !== '') {
$fields[] = self::toBeArray($transaction['v'] + Network::get()->chainId() * 2 + 35);
$fields[] = '0x'.$transaction['r'];
$fields[] = '0x'.$transaction['s'];
$fields[] = self::stripLeadingZeroBytes($transaction['r']);
$fields[] = self::stripLeadingZeroBytes($transaction['s']);

if (isset($transaction['legacySecondSignature'])) {
if (isset($transaction['legacySecondSignature']) && ! $skipLegacySecondSignature) {
$fields[] = '0x'.$transaction['legacySecondSignature'];
}
} else {
Expand All @@ -47,8 +48,6 @@ public static function toBuffer(array $transaction, bool $skipSignature = false)
$fields[] = self::toBeArray(0);
}

// TODO: second signature handling

$encoded = RlpEncoder::encode($fields);

return new Buffer(hex2bin(substr($encoded, 2)));
Expand All @@ -62,13 +61,22 @@ public static function toBuffer(array $transaction, bool $skipSignature = false)
*/
public static function toHash(array $transaction, bool $skipSignature = false): BufferInterface
{
$encoding = self::toBuffer($transaction, $skipSignature);
// The transaction hash never covers the legacy second signature
$encoding = self::toBuffer($transaction, $skipSignature, skipLegacySecondSignature: true);

$keccak = Keccak::hash($encoding->getBinary(), 256);

return Buffer::hex($keccak);
}

/**
* Strips the leading zero bytes so `r` and `s` are encoded as minimal integers.
*/
private static function stripLeadingZeroBytes(string $hex): string
{
return '0x'.preg_replace('/^(00)+/', '', $hex);
}

/**
* Converts a big integer to a big-endian byte array.
*
Expand Down
67 changes: 67 additions & 0 deletions tests/Unit/Transactions/LegacySecondSignatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

use ArkEcosystem\Crypto\Configuration\Network;
use ArkEcosystem\Crypto\Identities\PrivateKey;
use ArkEcosystem\Crypto\Networks\Testnet;
use ArkEcosystem\Crypto\Transactions\Deserializer;
use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Test vector from the Mainsail LSS v1 spec, built for chain 10_000
beforeEach(function () {
$this->fixture = $this->getTransactionFixture('evm_call', 'transfer-legacy-second-signature-vector');

Network::set(new class($this->fixture['chainId']) extends Testnet {
public function __construct(private readonly int $id)
{
parent::__construct();
}

public function chainId(): int
{
return $this->id;
}
});

$this->unsignedData = array_diff_key($this->fixture['data'], array_flip(['legacySecondSignature', 'hash']));
});

it('should sign the hash of the signed transaction with the second key', function () {
$transaction = new Transfer($this->unsignedData);

$transaction->sign(PrivateKey::fromHex($this->fixture['privateKey']));

expect($transaction->serialize()->getHex())->toBe($this->fixture['signed']);
expect($transaction->hash()->getHex())->toBe($this->fixture['data']['hash']);

$transaction->legacySecondSign(PrivateKey::fromHex($this->fixture['secondPrivateKey']));

expect($transaction->data['legacySecondSignature'])->toBe($this->fixture['data']['legacySecondSignature']);
expect($transaction->serialize()->getHex())->toBe($this->fixture['serialized']);
expect($transaction->hash()->getHex())->toBe($this->fixture['data']['hash']);
});

it('should use a 0 or 1 recovery id', function () {
$transaction = new Transfer($this->unsignedData);

$transaction->sign(PrivateKey::fromHex($this->fixture['privateKey']));
$transaction->legacySecondSign(PrivateKey::fromHex($this->fixture['secondPrivateKey']));

expect(substr($transaction->data['legacySecondSignature'], -2))->toBeIn(['00', '01']);
});

it('should require the primary signature first', function () {
$transaction = new Transfer($this->unsignedData);

$transaction->legacySecondSign(PrivateKey::fromHex($this->fixture['secondPrivateKey']));
})->throws(RuntimeException::class, 'Transaction must be signed before adding a legacy second signature');

it('should deserialize and hash only the first 9 fields', function () {
$transaction = Deserializer::new($this->fixture['serialized'])->deserialize();

expect($transaction->data['legacySecondSignature'])->toBe($this->fixture['data']['legacySecondSignature']);
expect($transaction->data['hash'])->toBe($this->fixture['data']['hash']);
expect($transaction->serialize()->getHex())->toBe($this->fixture['serialized']);
expect($transaction->verify())->toBeTrue();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"chainId": 10000,
"privateKey": "314f59dbc54460b18fc036d67dac36ad6e1fa1c3c0e0b3427a84e43faabfcecb",
"secondPrivateKey": "4427335cab240f6cc651b1df5ce5ff489c5a0bcb952507b542afe8f6c90e98e7",
"data": {
"nonce": "0",
"gasPrice": "5000000000",
"gasLimit": "21000",
"to": "0x0DFeDD974c2e58Dc8E292ec159d1DC9AdeaFb1C0",
"value": "1000000000000000000",
"data": "",
"legacySecondSignature": "ea77adb968255ec1570c0e48cede8cceec3a9aff29e9dac1c53c2a6de4f2a18740df289edfcdfe97a189975cba8945334e5f230d66232751e468c5500dbe7dec01",
"hash": "27af7a389f34f32569c90815ced4e23e8770eb2d77ace6f29dabec1a10bef205"
},
"signed": "f86e8085012a05f200825208940dfedd974c2e58dc8e292ec159d1dc9adeafb1c0880de0b6b3a764000080824e44a00f4f4ac7f1effa669ea1a8970ed37f367e4aa0b549636619feead2d4a2408ffca04492af59379f74385bd951d229456b3b23ead33ce1d10106535c6830a688733d",
"serialized": "f8b18085012a05f200825208940dfedd974c2e58dc8e292ec159d1dc9adeafb1c0880de0b6b3a764000080824e44a00f4f4ac7f1effa669ea1a8970ed37f367e4aa0b549636619feead2d4a2408ffca04492af59379f74385bd951d229456b3b23ead33ce1d10106535c6830a688733db841ea77adb968255ec1570c0e48cede8cceec3a9aff29e9dac1c53c2a6de4f2a18740df289edfcdfe97a189975cba8945334e5f230d66232751e468c5500dbe7dec01"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"v": 0,
"r": "a1f79cb40a4bb409d6cebd874002ceda3ec0ccb614c1d8155f5c2f7f798135f9",
"s": "2d2ef517aaf6feed747385e260c206f46b2ce9d6b2a585427a111685a097bd79",
"legacySecondSignature": "094b33b2d10c4d48cff9a9b10f79990c9a902a762b00d2f2a4d2ddad7823b59332b2da193265068c67cefea9ca8bc84e47acec406f3300a0a10b77a56ea8c18801",
"legacySecondSignature": "a7576f15a3eb2b4cb54b83e97d25b70c88efb63c783120b81ab0eb4a958631d444347954726642779f019de3db05f568e78915b7f9cf319f7deb093353fa245b01",
"senderPublicKey": "0243333347c8cbf4e3cbc7a96964181d02a2b0c854faa2fef86b4b8d92afcf473d",
"from": "0x1E6747BEAa5B4076a6A98D735DF8c35a70D18Bdd",
"hash": "a39435ec5de418e77479856d06a653efc171afe43e091472af22ee359eeb83be"
},
"serialized": "f8ad0185012a05f200825208946f0182a0cc707b055322ccf6d4cb6a5aff1aeb228405f5e10080825c6ba0a1f79cb40a4bb409d6cebd874002ceda3ec0ccb614c1d8155f5c2f7f798135f9a02d2ef517aaf6feed747385e260c206f46b2ce9d6b2a585427a111685a097bd79b841094b33b2d10c4d48cff9a9b10f79990c9a902a762b00d2f2a4d2ddad7823b59332b2da193265068c67cefea9ca8bc84e47acec406f3300a0a10b77a56ea8c18801"
"serialized": "f8ad0185012a05f200825208946f0182a0cc707b055322ccf6d4cb6a5aff1aeb228405f5e10080825c6ba0a1f79cb40a4bb409d6cebd874002ceda3ec0ccb614c1d8155f5c2f7f798135f9a02d2ef517aaf6feed747385e260c206f46b2ce9d6b2a585427a111685a097bd79b841a7576f15a3eb2b4cb54b83e97d25b70c88efb63c783120b81ab0eb4a958631d444347954726642779f019de3db05f568e78915b7f9cf319f7deb093353fa245b01"
}
Loading