-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAddress.php
81 lines (59 loc) · 2.13 KB
/
Address.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
class Address {
static function getBalance($address,$forkId) {
$decimalToInt = bcpow("10", chain::AMOUNT_DECIMAL_POINT);
$utxoTable = Utxo::getTable($forkId);
$balance = DB::result($r = DB::query("SELECT SUM(amount*{$decimalToInt}) FROM {$utxoTable} WHERE address='".DB::esc($address)."' LIMIT 1"),0,0);
$balance = Utils::safeDiv($balance,$decimalToInt);
@mysqli_free_result($r);
return $balance;
}
static function getUtxO($address,$start = null, $limit = null, $orderby=null) {
if (is_numeric($limit) AND is_numeric($start)) {
$limit_q = " LIMIT {$start},{$limit}";
} else if (is_numeric($limit)) {
$limit_q = " LIMIT {$limit}";
} else {
$limit_q = "";
}
if ($orderby == "amount ASC") {
$orderby_q = "ORDER BY amount ASC";
} else if ($orderby == "amount DESC") {
$orderby_q = "ORDER BY amount DESC";
} else {
$orderby_q = "ORDER BY id ASC";
}
if (strlen($address) == 34) {
} else {
$address = Address::getAddress($address);
}
$r = DB::query("SELECT * FROM `unspentTxOuts` WHERE address ='".DB::esc($address)."' {$orderby_q}{$limit_q}");
$utxo = [];
while($row = mysqli_fetch_assoc($r)) {
unset($row['id'],$row['blockIndex']);
$utxo[] = $row;
}
@mysqli_free_result($r);
return $utxo;
}
static function newAddress() {
$private = new PrivateKey();
$point = $private->getPubKeyPoints();
$derPublicKey=AddressCodec::Compress($point);
$hash=AddressCodec::Hash($derPublicKey);
$address = AddressCodec::Encode($hash,Chain::ADDRESS_PREFIX);
return array("address"=>$address, "privateKey" => $private->k,"publicKey" => $derPublicKey);
}
static function getAddress($private) {
$derPublicKey=self::getPublicKey($private);
$hash=AddressCodec::Hash($derPublicKey);
$address = AddressCodec::Encode($hash,Chain::ADDRESS_PREFIX);
return $address;
}
static function getPublicKey($private) {
$private = new PrivateKey($private);
$point = $private->getPubKeyPoints();
$derPublicKey=AddressCodec::Compress($point);
return $derPublicKey;
}
}