Skip to content

Commit 8fe4b62

Browse files
authored
feat: implement PublicKey#fromMultiSignatureAsset (#109)
1 parent 0964beb commit 8fe4b62

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"php": "^7.2",
2121
"bitwasp/bitcoin": "^1.0",
2222
"kodekeep/binary": "^1.0",
23-
"kodekeep/bytebuffer": "^1.0"
23+
"kodekeep/bytebuffer": "^1.0",
24+
"simplito/elliptic-php": "^1.0"
2425
},
2526
"require-dev": {
2627
"friendsofphp/php-cs-fixer": "^2.16",

src/Identities/PublicKey.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
1818
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey as EcPublicKey;
1919
use BitWasp\Bitcoin\Key\Factory\PublicKeyFactory;
20+
use Elliptic\EC;
2021

2122
/**
2223
* This is the public key class.
@@ -37,6 +38,29 @@ public static function fromPassphrase(string $passphrase): EcPublicKey
3738
return PrivateKey::fromPassphrase($passphrase)->getPublicKey();
3839
}
3940

41+
/**
42+
* Create a public key instance from a multi-signature asset.
43+
*
44+
* @param int $min
45+
* @param array $publicKeys
46+
*
47+
* @return \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey
48+
*/
49+
public static function fromMultiSignatureAsset(int $min, array $publicKeys): EcPublicKey
50+
{
51+
$minKey = static::fromPassphrase('0'.dechex($min));
52+
$keys = [$minKey->getHex(), ...$publicKeys];
53+
54+
$curve = (new EC('secp256k1'))->curve;
55+
$P = $curve->jpoint(null, null, null);
56+
57+
foreach ($keys as $publicKey) {
58+
$P = $P->add($curve->decodePoint($publicKey, 'hex'));
59+
}
60+
61+
return static::fromHex(bin2hex(implode(array_map('chr', $P->encodeCompressed(true)))));
62+
}
63+
4064
/**
4165
* Create a public key instance from a hex string.
4266
*

tests/Unit/Identities/PublicKeyTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ public function it_should_get_the_public_key_from_passphrase()
3434
$this->assertSame($fixture['data']['publicKey'], $actual->getHex());
3535
}
3636

37+
/** @test */
38+
public function it_should_get_the_public_key_from_a_multi_signature_asset()
39+
{
40+
$actual = TestClass::fromMultiSignatureAsset(3, [
41+
TestClass::fromPassphrase('secret 1')->getHex(),
42+
TestClass::fromPassphrase('secret 2')->getHex(),
43+
TestClass::fromPassphrase('secret 3')->getHex(),
44+
]);
45+
46+
$this->assertSame(
47+
'0279f05076556da7173610a7676399c3620276ebbf8c67552ad3b1f26ec7627794',
48+
$actual->getHex()
49+
);
50+
}
51+
3752
/** @test */
3853
public function it_should_get_the_public_key_from_hex()
3954
{

0 commit comments

Comments
 (0)