从 UINT8Array 转换 Solana 公钥

问题描述 投票:0回答:2

我已使用 Solana CLI 生成帐户,但我获取的公钥和私钥为

publicKey: Uint8Array(32) [
  102, 255,  46,  44,  90, 176, 207,  98,
  251,  66, 136, 190, 240,  59, 198, 177,
  169,  35, 153,   3, 163,  68, 188, 214,
  225,  46,  55, 111, 159, 157, 182, 111
], 

但我想要可读的格式来维持下一笔交易的密钥。

blockchain solana
2个回答
6
投票

您可以尝试以下

console.log(wallet.publicKey.toBase58());

1
投票

Pubkey
base58
所以这个(脏)香草(或任何
base58
库)应该可以工作。

// ref: https://gist.github.com/diafygi/90a3e80ca1c2793220e5/
const MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const encode = function (B) { const A = MAP; var d = [], s = "", i, j, c, n; for (i in B) { j = 0, c = B[i]; s += c || s.length ^ i ? "" : 1; while (j in d || c) { n = d[j]; n = n ? n * 256 + c : c; c = n / 58 | 0; d[j] = n % 58; j++ } } while (j--) s += A[d[j]]; return s };
const decode = function (S) { const A = MAP; var d = [], b = [], i, j, c, n; for (i in S) { j = 0, c = A.indexOf(S[i]); if (c < 0) return undefined; c || b.length ^ i ? i : b.push(0); while (j in d || c) { n = d[j]; n = n ? n * 58 + c : c; c = n >> 8; d[j] = n % 256; j++ } } while (j--) b.push(d[j]); return new Uint8Array(b) };

encode(new Uint8Array([
  102, 255,  46,  44,  90, 176, 207,  98,
  251,  66, 136, 190, 240,  59, 198, 177,
  169,  35, 153,   3, 163,  68, 188, 214,
  225,  46,  55, 111, 159, 157, 182, 111
]))

输出

7w4GYV1BxE9Vvn93BG3NZzZQAYoiTXWnNX8rdXBgQ5Tt
© www.soinside.com 2019 - 2024. All rights reserved.