Skip to content

Commit 244c47f

Browse files
author
Thomas Kerin
committed
PSBTPathDerivation: use typed array for path, and revert to encapsulating raw pubkey bytes
1 parent e79f5fd commit 244c47f

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/Transaction/PSBT/PSBTBip32Derivation.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@ class PSBTBip32Derivation
2323
*/
2424
private $path;
2525

26+
/**
27+
* @var BufferInterface
28+
*/
29+
private $rawKey;
30+
2631
/**
2732
* PSBTBip32Derivation constructor.
33+
* @param BufferInterface $rawKey
2834
* @param int $fpr
29-
* @param int[] $path
35+
* @param int ...$path
3036
*/
31-
public function __construct(int $fpr, array $path)
37+
public function __construct(BufferInterface $rawKey, int $fpr, int ...$path)
3238
{
39+
$this->rawKey = $rawKey;
3340
$this->masterKeyFpr = $fpr;
3441
$this->path = $path;
3542
}
3643

37-
public function getMasterKeyFpr(): int
38-
{
39-
return $this->masterKeyFpr;
40-
}
41-
4244
/**
4345
* @return int[]
4446
*/
@@ -47,6 +49,16 @@ public function getPath(): array
4749
return $this->path;
4850
}
4951

52+
public function getMasterKeyFpr(): int
53+
{
54+
return $this->masterKeyFpr;
55+
}
56+
57+
public function getRawPublicKey(): BufferInterface
58+
{
59+
return $this->rawKey;
60+
}
61+
5062
public function getPublicKey(EcAdapterInterface $ecAdapter = null): PublicKeyInterface
5163
{
5264
$ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();

src/Transaction/PSBT/PSBTInput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ public static function fromParser(Parser $parser, VarString $vs): PSBTInput
225225
if (array_key_exists($pubKey->getBinary(), $bip32Derivations)) {
226226
throw new InvalidPSBTException("Duplicate derivation");
227227
}
228-
$bip32Derivations[$pubKey->getBinary()] = self::parseBip32DerivationValue($value);
228+
list ($fpr, $path) = self::parseBip32DerivationValue($value);
229+
$bip32Derivations[$pubKey->getBinary()] = new PSBTBip32Derivation($pubKey, $fpr, ...$path);
229230
break;
230231
case self::FINAL_SCRIPTSIG:
231232
if ($finalScriptWitness !== null) {

src/Transaction/PSBT/PSBTOutput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public static function fromKeyValues(Parser $parser, VarString $vs): PSBTOutput
111111
if (array_key_exists($pubKey->getBinary(), $bip32Derivations)) {
112112
throw new InvalidPSBTException("Duplicate derivation");
113113
}
114-
$bip32Derivations[$pubKey->getBinary()] = self::parseBip32DerivationValue($value);
114+
list ($fpr, $path) = self::parseBip32DerivationValue($value);
115+
$bip32Derivations[$pubKey->getBinary()] = new PSBTBip32Derivation($pubKey, $fpr, ...$path);
115116
break;
116117
default:
117118
if (array_key_exists($key->getBinary(), $unknown)) {

src/Transaction/PSBT/ParseUtil.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ private static function parsePublicKeyKey(BufferInterface $key): BufferInterface
2727
* Returns array[fpr(int), path(int[])]
2828
*
2929
* @param BufferInterface $value
30-
* @return PSBTBip32Derivation
30+
* @return array
3131
* @throws InvalidPSBTException
3232
*/
33-
private static function parseBip32DerivationValue(BufferInterface $value): PSBTBip32Derivation
33+
private static function parseBip32DerivationValue(BufferInterface $value): array
3434
{
3535
$len = $value->getSize();
3636
if ($len % 4 !== 0 || $len === 0) {
@@ -40,6 +40,6 @@ private static function parseBip32DerivationValue(BufferInterface $value): PSBTB
4040
$pieces = $len / 4;
4141
$path = unpack("N{$pieces}", $value->getBinary());
4242
$fpr = array_shift($path);
43-
return new PSBTBip32Derivation($fpr, $path);
43+
return [$fpr, $path];
4444
}
4545
}

tests/Transaction/PSBT/UpdatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testUpdateWithScripts()
7171
$sequence = new HierarchicalKeySequence();
7272
$keyFactory = new PublicKeyFactory();
7373
$key = $keyFactory->fromHex($hexKey);
74-
$derivs[$key->getPubKeyHash()->getBinary()] = new PSBTBip32Derivation($fpr, $sequence->decodeAbsolute($path)[1]);
74+
$derivs[$key->getPubKeyHash()->getBinary()] = new PSBTBip32Derivation($key->getBuffer(), $fpr, ...$sequence->decodeAbsolute($path)[1]);
7575
};
7676
// not all used in script
7777
$createDeriv("029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f", $fpr, "m/0'/0'/0'");

0 commit comments

Comments
 (0)