Skip to content

Commit

Permalink
implement compressed WIF format
Browse files Browse the repository at this point in the history
  • Loading branch information
rgex committed Jul 7, 2017
1 parent 5aeb24a commit 7356fdd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Examples/wif.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
echo "imported address : " . $address . PHP_EOL;
} else {
echo "invalid WIF key" . PHP_EOL;
}
}
25 changes: 23 additions & 2 deletions src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -813,19 +813,39 @@ public function validateAddress($address)
* @return string (base58)
* @throws \Exception
*/
public function getWif()
public function getWif($compressed = true)
{
if(!isset($this->k))
{
throw new \Exception('No Private Key was defined');
}

$k = $this->k;

while(strlen($k) < 64)
$k = '0' . $k;

$secretKey = '80' . $k;

if($compressed) {
$secretKey .= '01';
}

$secretKey .= substr($this->hash256(hex2bin($secretKey)), 0, 8);

return $this->base58_encode($secretKey);
}

/***
* returns the private key under the Wallet Import Format for an uncompressed address
*
* @return string (base58)
* @throws \Exception
*/
public function getUncompressedWif()
{
return getWif(false);
}

/***
* Tests if the Wif key (Wallet Import Format) is valid or not.
Expand Down Expand Up @@ -855,7 +875,8 @@ public function setPrivateKeyWithWif($wif)
}

$key = $this->base58_decode($wif, true);
$this->setPrivateKey(substr($key, 1, strlen($key) - 9));

$this->setPrivateKey(substr($key, 2, 64));
}

/***
Expand Down

1 comment on commit 7356fdd

@rgex
Copy link
Contributor Author

@rgex rgex commented on 7356fdd Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like to be a Dascoin WIF to me

Please sign in to comment.