Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schnorr signing workaround #123

Merged
merged 73 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
b51e602
adjust serialization
alfonsobries Jul 22, 2024
c2e5d10
add js signer
alfonsobries Jul 22, 2024
b62aff1
compile script
alfonsobries Jul 22, 2024
5a4836a
use the new signer
alfonsobries Jul 22, 2024
2edffef
style: resolve style guide violations
alfonsobries Jul 22, 2024
4e570b4
remove second passphrase tests
alfonsobries Jul 22, 2024
f86ef4d
style: resolve style guide violations
alfonsobries Jul 22, 2024
f83ece5
add verify
alfonsobries Jul 22, 2024
173957f
add verify method
alfonsobries Jul 22, 2024
2c61550
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 22, 2024
972a6ac
style: resolve style guide violations
alfonsobries Jul 22, 2024
0be7c86
fix verify script
alfonsobries Jul 22, 2024
f695575
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 22, 2024
c13edbe
style: resolve style guide violations
alfonsobries Jul 22, 2024
cafc233
remove duplicated fixtures and adjust transfer tests
alfonsobries Jul 22, 2024
2be4914
style: resolve style guide violations
alfonsobries Jul 22, 2024
678ce18
adjust serialize/deserailize tests
alfonsobries Jul 22, 2024
6b489fc
fix script
alfonsobries Jul 22, 2024
f1d0bb2
adjust vote tests
alfonsobries Jul 23, 2024
9e88081
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 23, 2024
ba5071f
style: resolve style guide violations
alfonsobries Jul 23, 2024
e37de02
remove deprecated tests
alfonsobries Jul 23, 2024
68f094a
update schnorr signer
alfonsobries Jul 23, 2024
c5f2455
fix signer
alfonsobries Jul 23, 2024
a9b85d1
handle leading byte
alfonsobries Jul 23, 2024
7e115d0
cleanup
alfonsobries Jul 23, 2024
87b7240
style: resolve style guide violations
alfonsobries Jul 23, 2024
0e70766
update transfer tests and deserialization
alfonsobries Jul 23, 2024
99fe695
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 23, 2024
47f40ad
style: resolve style guide violations
alfonsobries Jul 23, 2024
7b10671
refactor for reusability
alfonsobries Jul 23, 2024
b9ce602
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 23, 2024
2e1be86
style: resolve style guide violations
alfonsobries Jul 23, 2024
ea7a2c5
remove unused parameter
alfonsobries Jul 23, 2024
0f02a78
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 23, 2024
961b056
style: resolve style guide violations
alfonsobries Jul 23, 2024
b8f0a90
update vote tests
alfonsobries Jul 23, 2024
9aa8e0d
style: resolve style guide violations
alfonsobries Jul 23, 2024
8f9a5e4
refactor address utils
alfonsobries Jul 23, 2024
45e062b
Update Transaction.php
alfonsobries Jul 23, 2024
95d1945
multipayment adjustments
alfonsobries Jul 23, 2024
3c067d1
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 23, 2024
c063c25
style: resolve style guide violations
alfonsobries Jul 23, 2024
7ff3be5
rename to validator registration
alfonsobries Jul 24, 2024
e7cd314
adjust validator registration tests
alfonsobries Jul 24, 2024
bd54fb8
style: resolve style guide violations
alfonsobries Jul 24, 2024
06fbb52
adjustments for validator resignation
alfonsobries Jul 24, 2024
165e9ef
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 24, 2024
04762bd
style: resolve style guide violations
alfonsobries Jul 24, 2024
f289958
cleanup
alfonsobries Jul 24, 2024
5211abe
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 24, 2024
640508e
style: resolve style guide violations
alfonsobries Jul 24, 2024
77e54e8
add node js
alfonsobries Jul 24, 2024
8f0ea2b
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 24, 2024
b0bda4d
Update test.yml
alfonsobries Jul 24, 2024
1d94ecb
install npm dependencies
alfonsobries Jul 24, 2024
01f3529
remove deprecated
alfonsobries Jul 24, 2024
2bc7931
update multisignature registration builder
alfonsobries Jul 24, 2024
11b5d57
adjust serialization
alfonsobries Jul 24, 2024
22f4ff4
style: resolve style guide violations
alfonsobries Jul 24, 2024
364dad8
remove deprecated
alfonsobries Jul 24, 2024
717e807
add multisignature compatibility
alfonsobries Jul 24, 2024
55b1d4e
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 24, 2024
18ff15f
style: resolve style guide violations
alfonsobries Jul 24, 2024
71e033e
fix broken test
alfonsobries Jul 24, 2024
9ada369
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 24, 2024
a0388be
style: resolve style guide violations
alfonsobries Jul 24, 2024
ff9fbf7
wip
alfonsobries Jul 24, 2024
aa70f3b
fix broken tests
alfonsobries Jul 24, 2024
eed6217
style: resolve style guide violations
alfonsobries Jul 24, 2024
08ceee1
refactor temporary command
alfonsobries Jul 24, 2024
222624f
Merge branch 'feat/schnorr-hack' of github.com:ArkEcosystem/php-crypt…
alfonsobries Jul 24, 2024
b242e23
style: resolve style guide violations
alfonsobries Jul 24, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Install schnorr workaround script dependencies
run: npm --prefix ./scripts/ install

- name: Install
run: composer update --no-interaction --no-suggest --ignore-platform-reqs

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ clover.xml
.phpunit.result.cache
.coverage
coverage.xml
# Used for the schnorr signature hack, remove once a php version is implemented
scripts/node_modules
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions scripts/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
150 changes: 150 additions & 0 deletions scripts/dist/schnorr-signer.js

Large diffs are not rendered by default.

2,722 changes: 2,722 additions & 0 deletions scripts/package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "schnorr-signer",
"version": "1.0.0",
"description": "node-schorr-signer",
"main": "dist/schnorr-signer.js",
"directories": {
"test": "tests"
},
"scripts": {
"build": "babel src --out-dir dist",
"dev": "node src/schnorr-signer.js",
"start": "node dist/schnorr-signer.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.14.5",
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7"
},
"dependencies": {
"bcrypto": "^5.5.2"
}
}
86 changes: 86 additions & 0 deletions scripts/src/schnorr-signer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { schnorr } = require("bcrypto");

// Function to sign a message using the provided private key
const signMessage = async (privateKeyHex, messageHex) => {
const privateKey = Buffer.from(privateKeyHex, "hex");
const message = Buffer.from(messageHex, "hex");

try {
const signature = await schnorr.sign(message, privateKey);
const signatureHex = signature.toString("hex");

return {
status: "success",
signature: signatureHex,
};
} catch (error) {
return {
status: "error",
message: error.message,
};
}
};

// Function to verify a signature using the provided public key
const verifySignature = async (publicKeyHex, messageHex, signatureHex) => {
let publicKey = Buffer.from(publicKeyHex, "hex");
const message = Buffer.from(messageHex, "hex");
const signature = Buffer.from(signatureHex, "hex");

// Remove leading byte ('02' / '03') from ECDSA key
if (publicKey.byteLength === 33) {
publicKey = publicKey.subarray(1);
}

try {
const isValid = await schnorr.verify(message, signature, publicKey);

return {
status: "success",
isValid: isValid,
};
} catch (error) {
return {
status: "error",
message: error.message,
};
}
};

// Function to parse command line arguments and call the appropriate function
const main = async () => {
const args = process.argv.slice(2);

if (args.length < 3) {
console.error(
JSON.stringify({
status: "error",
message: "Usage: npm start <mode> <parameters>",
})
);
process.exit(1);
}

const mode = args[0];
let result;

if (mode === "sign" && args.length === 3) {
const [privateKeyHex, messageHex] = args.slice(1);
result = await signMessage(privateKeyHex, messageHex);
} else if (mode === "verify" && args.length === 4) {
const [publicKeyHex, messageHex, signatureHex] = args.slice(1);
result = await verifySignature(publicKeyHex, messageHex, signatureHex);
} else {
console.error(
JSON.stringify({
status: "error",
message: `Usage: npm start ${mode} <parameters>.\nFor 'sign': npm start sign <privateKeyHex> <messageHex>.\nFor 'verify': npm start verify <publicKeyHex> <messageHex> <signatureHex>.`,
})
);
process.exit(1);
}

console.log(JSON.stringify(result));
};

main();
4 changes: 2 additions & 2 deletions src/Configuration/Fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class Fee
private static $fees = [
Types::TRANSFER => Fees::TRANSFER,
Types::SECOND_SIGNATURE_REGISTRATION => Fees::SECOND_SIGNATURE_REGISTRATION,
Types::DELEGATE_REGISTRATION => Fees::DELEGATE_REGISTRATION,
Types::VALIDATOR_REGISTRATION => Fees::VALIDATOR_REGISTRATION,
Types::VOTE => Fees::VOTE,
Types::MULTI_SIGNATURE_REGISTRATION => Fees::MULTI_SIGNATURE_REGISTRATION,
Types::IPFS => Fees::IPFS,
Types::MULTI_PAYMENT => Fees::MULTI_PAYMENT,
Types::DELEGATE_RESIGNATION => Fees::DELEGATE_RESIGNATION,
Types::VALIDATOR_RESIGNATION => Fees::VALIDATOR_RESIGNATION,
Types::HTLC_LOCK => Fees::HTLC_LOCK,
Types::HTLC_CLAIM => Fees::HTLC_CLAIM,
Types::HTLC_REFUND => Fees::HTLC_REFUND,
Expand Down
4 changes: 2 additions & 2 deletions src/Enums/Fees.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Fees

public const SECOND_SIGNATURE_REGISTRATION = '500000000';

public const DELEGATE_REGISTRATION = '2500000000';
public const VALIDATOR_REGISTRATION = '2500000000';

public const VOTE = '100000000';

Expand All @@ -34,7 +34,7 @@ class Fees

public const MULTI_PAYMENT = '10000000';

public const DELEGATE_RESIGNATION = '2500000000';
public const VALIDATOR_RESIGNATION = '2500000000';

public const HTLC_LOCK = '10000000';

Expand Down
4 changes: 2 additions & 2 deletions src/Enums/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Types

public const SECOND_SIGNATURE_REGISTRATION = 1;

public const DELEGATE_REGISTRATION = 2;
public const VALIDATOR_REGISTRATION = 2;

public const VOTE = 3;

Expand All @@ -34,7 +34,7 @@ class Types

public const MULTI_PAYMENT = 6;

public const DELEGATE_RESIGNATION = 7;
public const VALIDATOR_RESIGNATION = 7;

public const HTLC_LOCK = 8;

Expand Down
43 changes: 8 additions & 35 deletions src/Identities/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace ArkEcosystem\Crypto\Identities;

use ArkEcosystem\Crypto\Networks\AbstractNetwork;
use ArkEcosystem\Crypto\Utils\Address as AddressUtils;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey as EccPrivateKey;
use BitWasp\Bitcoin\Crypto\Hash;
use Elliptic\EC;
use kornrunner\Keccak;

Expand All @@ -25,13 +25,12 @@ class Address
* Derive the address from the given passphrase.
*
* @param string $passphrase
* @param AbstractNetwork|null $network
*
* @return string
*/
public static function fromPassphrase(string $passphrase, AbstractNetwork $network = null): string
public static function fromPassphrase(string $passphrase): string
{
return static::fromPrivateKey(PrivateKey::fromPassphrase($passphrase), $network);
return static::fromPrivateKey(PrivateKey::fromPassphrase($passphrase));
}

/**
Expand All @@ -51,11 +50,10 @@ public static function fromMultiSignatureAsset(int $min, array $publicKeys): str
* Derive the address from the given public key.
*
* @param string $publicKey
* @param AbstractNetwork|null $network
*
* @return string
*/
public static function fromPublicKey(string $publicKey, $network = null): string
public static function fromPublicKey(string $publicKey): string
{
// Convert the public key to a byte array
$publicKeyBytes = hex2bin($publicKey);
Expand All @@ -81,22 +79,21 @@ public static function fromPublicKey(string $publicKey, $network = null): string
$address = '0x'.$address;

// Convert to checksum address
return self::toChecksumAddress($address);
return AddressUtils::toChecksumAddress($address);
}

/**
* Derive the address from the given private key.
*
* @param EccPrivateKey $privateKey
* @param AbstractNetwork|null $network
*
* @return string
*/
public static function fromPrivateKey(EccPrivateKey $privateKey, AbstractNetwork $network = null): string
public static function fromPrivateKey(EccPrivateKey $privateKey): string
{
$publicKey = $privateKey->getPublicKey()->getHex();

return static::fromPublicKey($publicKey, $network);
return static::fromPublicKey($publicKey);
}

/**
Expand All @@ -107,33 +104,9 @@ public static function fromPrivateKey(EccPrivateKey $privateKey, AbstractNetwork
*
* @return bool
*/
public static function validate(string $address, $network = null): bool
public static function validate(string $address): bool
{
// Simple validation to check if the address starts with 0x and is 42 characters long
return preg_match('/^0x[a-fA-F0-9]{40}$/', $address) === 1;
}

/**
* Convert to checksum address.
*
* @param string $address
*
* @return string
*/
private static function toChecksumAddress(string $address): string
{
$address = strtolower(substr($address, 2));
$hash = Keccak::hash($address, 256);
$checksumAddress = '0x';

for ($i = 0; $i < 40; $i++) {
if (intval($hash[$i], 16) >= 8) {
$checksumAddress .= strtoupper($address[$i]);
} else {
$checksumAddress .= $address[$i];
}
}

return $checksumAddress;
}
}
23 changes: 7 additions & 16 deletions src/Transactions/Builder/AbstractTransactionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct()
$this->transaction->data['nonce'] = '0';
$this->transaction->data['amount'] = '0';
$this->transaction->data['fee'] = $this->getFee();
$this->transaction->data['version'] = 2;
$this->transaction->data['version'] = 1;
$this->transaction->data['network'] = Network::get()->pubKeyHash();
}

Expand Down Expand Up @@ -123,16 +123,17 @@ public function sign(string $passphrase): self
}

/**
* Sign the transaction using the given second passphrase.
* Sign the transaction using the given passphrase.
*
* @param string $secondPassphrase
* @param string $passphrase
*
* @return AbstractTransactionBuilder
*/
public function secondSign(string $secondPassphrase): self
public function multiSign(string $passphrase, int $index = -1): self
{
$this->transaction = $this->transaction->secondSign(PrivateKey::fromPassphrase($secondPassphrase));
$this->transaction->data['id'] = $this->transaction->getId();
$keys = PrivateKey::fromPassphrase($passphrase);

$this->transaction = $this->transaction->multiSign($keys, $index);

return $this;
}
Expand All @@ -147,16 +148,6 @@ public function verify(): bool
return $this->transaction->verify();
}

/**
* Verify the transaction validity with a second signature.
*
* @return bool
*/
public function secondVerify(string $secondPublicKey): bool
{
return $this->transaction->secondVerify($secondPublicKey);
}

/**
* Convert the transaction to its array representation.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Transactions/Builder/MultiPaymentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function add(string $recipientId, string $amount): self
{
$this->transaction->data['asset']['payments'][] = compact('recipientId', 'amount');

$this->transaction->data['amount'] = strval(+$this->transaction->data['amount'] + +$amount);

return $this;
}

Expand Down
Loading
Loading