pubkey_encode

function pubkey_encode(pubkey: byte_array, compressed: boolean): byte_array

Convert between public key formats.

Accepts valid public keys of size 33, 64 or 65 bytes. Note that not all byte arrays of acceptable length constitute valid public keys.

The optional boolean flag compressed determines whether a compressed (33-byte), or uncompressed (65-byte) public key, is returned. Defaults to false (uncompressed) if not provided.

Examples:

// convert a 33-byte pubkey to a 65-byte pubkey:
crypto.pubkey_encode(my_33_byte_pubkey)

// convert a 65-byte pubkey to a 33-byte pubkey:
crypto.pubkey_encode(my_65_byte_pubkey, true)

// convert a 64-byte pubkey to a 65-byte pubkey:
crypto.pubkey_encode(my_64_byte_pubkey)

// convert a 64-byte pubkey to a 33-byte pubkey:
crypto.pubkey_encode(my_64_byte_pubkey, true)

Note that this function cannot return a 64-byte public key. However a 64-byte public key can be obtained from a 65-byte public key by dropping the first byte with byte_array.sub(1):

// convert a 65-byte pubkey to a 64-byte pubkey:
my_65_byte_pubkey.sub(1)

// convert a 33-byte pubkey to a 64-byte pubkey:
crypto.pubkey_encode(my_33_byte_pubkey).sub(1)

Return

a 65-byte public key in uncompressed mode, or a 33-byte public key in compressed mode

Since

0.13.5

Parameters

pubkey

the public key to compress/uncompress

compressed

whether the returned public key should be compressed, defaults to false

Throws

exception

when

  • the given byte array does not have length 33, 64 or 65

  • the given byte array is not a valid public key