'can I access the private key of solana account (via phantom) to encrypt data?
I am very new to web3 etc but as far as I know, every account is represented by a public/private key. The public key can be accessed anytime and the private key is (obviously) never shown.
Is there however a way to access the private key of Solana (via phantom) so data can be encrypted and then saved to the blockchain?
The idea is: two addresses interact and via Diffie Hellman key exchange use the combined key to encrypt the message/content so that only these two can read the message
Solution 1:[1]
If you look at the @solana/wallet-adapter, the wallet interface provides a signMessage
function: https://github.com/solana-labs/wallet-adapter/blob/469edb5dd45231d397751b0268c86dffd6ed730a/packages/core/react/src/useWallet.ts#L37 which just encrypts any byte array that you pass into it. See the interface definition at https://github.com/solana-labs/wallet-adapter/blob/a5b1ebd70ae9753d188fec60e95e252402f3f371/packages/core/base/src/signer.ts#L55
With that, you don't get access to the private key, but you can sign any arbitrary message, which should be enough. If it isn't, please feel free to create an issue and suggest an addition to the interface.
Solution 2:[2]
You cannot access any private key. If you could it would not be called a private key. Unless private key owner explicitly publishes it.
You can create a keypair for the account and then save this keypair. For example create a new file keypairGenerator.js
and add this:
const fs=require('fs')
const anchor=require("@project-serum/anchor")
const account=anchor.web3.Keypair.generate()
fs.writeFileSync('./keypair.json',JSON.stringify(account))
this will write the keypair to keypair.json
file and it will look like this:
{"_keypair":{
"publicKey":{"0":129,"1":88,"2":146,"3":38,"4":224,"5":124,"6":88,"7":26,"8":244,"9":238,"10":238,"11":61,"12":140,"13":33,"14":116,"15":74,"16":161,"17":184,"18":56,"19":194,"20":189,"21":151,"22":82,"23":29,"24":226,"25":204,"26":94,"27":142,"28":1,"29":130,"30":5,"31":222},
"secretKey":{"0":220,"1":93,"2":217,"3":140,"4":50,"5":48,"6":225,"7":236,"8":144,"9":88,"10":167,"11":3,"12":60,"13":179,"14":31,"15":102,"16":161,"17":190,"18":91,"19":173,"20":89,"21":19,"22":159,"23":203,"24":66,"25":188,"26":210,"27":50,"28":159,"29":16,"30":28,"31":234,"32":129,"33":88,"34":146,"35":28,"36":224,"37":144,"38":88,"39":26,"40":244,"41":238,"42":238,"43":61,"44":140,"45":33,"46":116,"47":74,"48":161,"49":184,"50":56,"51":194,"52":189,"53":151,"54":82,"55":29,"56":226,"57":204,"58":93,"59":132,"60":1,"61":130,"62":5,"63":222}}}
in your project, assuming written in javascript:
import keypair from './keypair.json'
// Object.values will get the values of keypair._keypair.secretKey object into array
const keypairArray=Object.values(keypair._keypair.secretKey)
const secret=new Uint8Array(keypairArray)
const account=web3.Keypair.fromSecretKey(secret)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jon C |
Solution 2 | Yilmaz |