Skip to content

Commit

Permalink
Construct address from PubkeyHash
Browse files Browse the repository at this point in the history
- Added additional constructor to let one create an Address object
  from a pubkeyhash.
  • Loading branch information
Stephan February committed Oct 20, 2023
1 parent b43c85e commit 09e4d45
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/src/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class Address {

}

Address.fromPubkeyHash(String pubkeyHashHex, NetworkType networkType){
_fromPubkeyHash(pubkeyHashHex, networkType);
}


/// Serialise this address object to a base58-encoded string
///
Expand Down Expand Up @@ -158,6 +162,24 @@ class Address {
return utf8Decoded;
}

void _fromPubkeyHash(String pubkeyHashHex, NetworkType networkType){

var versionByte;
if (networkType == NetworkType.MAIN) {
versionByte = Networks.getNetworkVersion(NetworkAddressType.MAIN_PKH);
}
else {
versionByte = Networks.getNetworkVersion(NetworkAddressType.TEST_PKH);
}
_version = versionByte & 0XFF;

_networkTypes = Networks.getNetworkTypes(_version!);
_addressType = Networks.getAddressType(_version!);
_networkType = Networks.getNetworkTypes(_version!)[0];
_publicKeyHash = pubkeyHashHex;

}

void _fromBase58(String address){

address = address.trim();
Expand Down
7 changes: 7 additions & 0 deletions test/address_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,12 @@ void main() {
expect(address.toString(), equals('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX'));
});

test('should create an address from Public Key Hash (PKH)',(){
var pubkeyHash = '3c3fa3d4adcaf8f52d5b1843975e122548269937';
var address = Address.fromPubkeyHash(pubkeyHash, NetworkType.MAIN);

expect(address.toBase58(), equals("16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r"));
});

}

0 comments on commit 09e4d45

Please sign in to comment.