Nhảy tới nội dung
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Address

Utils for converting public address, public key.

Import the getCompressedPublicKey from @kaiachain/web3js-ext packages

To compress a public key from {x, y} format to hexadecimal, use getCompressedPublicKey with the public key object as param

Otherwise if you want to compress the public key from uncompressed hexadecimal, use getCompressedPublicKey with the uncompress public key as param

addressUtils.js

const { getCompressedPublicKey } = require('@kaiachain/web3js-ext')
async function main() {
const publicKeyObject = {
x: '0xdc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd',
y: '0xaf06ca34ae8714cf3dae06bacdb78c7c2d4054bd38961d40853cd5f15955da79',
}
console.log(
'pubkey from { x, y } object =',
getCompressedPublicKey(publicKeyObject)
)
const uncompressedPublicKeyString =
'0x04dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdaf06ca34ae8714cf3dae06bacdb78c7c2d4054bd38961d40853cd5f15955da79'
console.log(
'pubkey from uncompressed format =',
getCompressedPublicKey(uncompressedPublicKeyString)
)
}
main()

output

❯ node addressUtils.js
pubkey from { x, y } object = 0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd
pubkey from uncompressed format = 0x03dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cd

Cải thiện trang này