我需要通过Solana CLI使用Phantom钱包,但是我无法对其进行配置。
例如,要使用
solana balance --keypair fileexportedfromphantom
但是无法阅读该信息。
我如何将私钥转换为Solana CLI可用的有效形式?
我需要通过Solana CLI使用Phantom钱包,但是我无法对其进行配置。
例如,要使用
solana balance --keypair fileexportedfromphantom
但是无法阅读该信息。
我如何将私钥转换为Solana CLI可用的有效形式?
尝试:
solana-keygen recover 'prompt://?key=0/0' -o <file.json>
并在“显示秘密恢复短语”下输入来自Phantom的24个单词的恢复短语。
这是因为Phantom对于钱包使用0/0
派生路径,需要提供额外的路径才能到达正确的账户。
您可以使用相同的命令与1/0
,2/0
... N/0
一起使用以获取不同的Phantom派生账户。
有关使用Solana工具进行分层派生的更多信息,请参见此处: https://docs.solana.com/wallet-guide/paper-wallet#hierarchical-derivation
或使用Solflare钱包在此处检查特定24个单词短语的导出路径:https://solflare.com/access
根据@FutForFut的最新评论,这假定您拥有或希望使用来自Phantom的秘密恢复短语。 在某些情况下,您可能仅拥有来自菜单中的Phantom的私钥(“显示私钥”)。 这是一个base58编码的密钥,您需要将其转换为JSON文件中的字节数组。
这里是使用bs58
包的JavaScript片段(https://www.npmjs.com/package/bs58):
const bs58 = require('bs58');
const fs = require('fs');
b = bs58.decode('privatekeyexportedfromphantom');
j = new Uint8Array(b.buffer, b.byteOffset, b.byteLength / Uint8Array.BYTES_PER_ELEMENT);
fs.writeFileSync('key.json', `[${j}]`);
根据需要更新字段 privatekeyexportedfromphantom
和 key.json
。
这有点麻烦,但你需要将Phantom返回的Base-58私钥解码为一个字节数组。以下是一个简单的Python代码片段,使用base58
包(https://pypi.org/project/base58/)实现:
import base58
byte_array = base58.b58decode(MY_PRIVATE_KEY_IN_BASE58)
json_string = "[" + ",".join(map(lambda b: str(b), byte_array)) + "]"
print(json_string)
你可以将输出导出到文件中,然后在CLI工具中使用该文件作为你的--keypair
。
你可以将输出导出到文件中,然后在CLI工具中使用该文件作为你的--keypair
。
MY_PRIVATE_KEY_IN_BASE58
,您应该输入一个字符串,例如
"4AfmonCVDRutyt3vMfsooagRsoNXTCVURGZ7nzSZPP9CWykXUmgA72Sfgrne7EcqtVJE6Gq6aA8xw21bHtbZMQzL"
这将给出
[158,99,132,43,140,119,254,9,6,225,141,208,225,184,48,219,246,115,45,3,0,67,160,190,25,199,130,208,179,210,219,1,41,32,144,228,51,198,235,65,64,71,155,119,235,189,157,49,2,65,230,165,161,159,153,77,213,153,226,174,35,2,212,89]
你能测试一下吗?这是在Ubuntu 20.04上使用Python 3.8运行的。 - Jon Cfrom pathlib import Path
import base58
SAVE_DIR = Path.home() / '.config' / 'solana'
def convert_key():
byte_array = base58.b58decode(input('Enter Public Key: '))
return "[" + ",".join(map(lambda b: str(b), byte_array)) + "]"
def create_file(save_dir: Path, key_name: str) -> Path:
keypair_path = save_dir / f'{key_name}.json'
if not SAVE_DIR.exists():
raise FileNotFoundError('Solana config directory not found')
if not SAVE_DIR.is_dir():
raise NotADirectoryError('Solana config directory not found')
if keypair_path.exists():
raise FileExistsError('Keypair already exists')
return keypair_path
def main():
keypair = input('Enter keypair name: ')
keypair_path = create_file(SAVE_DIR, keypair)
keypair_path.write_text(convert_key())
print(f'Keypair {keypair} created at {keypair_path}')
if __name__ == '__main__':
main()