Addresses
It is important to note that Bitcoin Addresses are a wallet concern. Wallets use Bitcoin Addresses as a “user-friendly” means of obtaining public key hashes from wallet users. I.e. the addresses we create never end up inside bitcoin Transactions. Addresses are envelopes for public-key-hashes. Public key hashes do appear in bitcoin transactions.
Obtaining an Address
Since addresses contain hashes of public keys, it stands to reason that we can obtain an address directly from a public key.
//create a random private key
var pk = SVPrivateKey(networkType: NetworkType.REGTEST);
SVPublicKey pubKey = pk.publicKey;
//obtain an address from the public key
Address addr = pubKey.toAddress(NetworkType.REGTEST);
Serialising Addresses
The generally accepted format for sharing Addresses publicly, is in their base58-encoded form. It typically looks like this:
//example base58-encoded address : 2NB72XtkjpnATMggui83aEtPawyyKvnbX2o
-
Construct from base58 encoding
var addr = Address.fromBase58('2NB72XtkjpnATMggui83aEtPawyyKvnbX2o');
-
Serialise to base58 encoding
Address addr = pubKey.toAddress(NetworkType.REGTEST); var addr = Address.toBase58();