Search code examples
nearprotocol

How do I sign a message with a newly created Near account in a 3rd party app?


I'm setting up a new Near account, and I want to use its keys to sign a message in an app I'm building. How can I do this?

I used the wallet.nearprotocol.com page to create an account. Then, I used nearlib to connect to the testnet, and verify the account's balance and public keys.

But I couldn't find a way to add the account into the localStorage key store or otherwise access a method to sign a message. Nor could I find a wallet plugin or extension that would provide me access.


Solution

  • Generally the idea is that you never transfer given private key between 2 devices / security contexts.

    So normally instead of getting private key out of wallet you just want to generate new key pair and request wallet to add public key.

    https://github.com/nearprotocol/nearlib/blob/master/src.ts/wallet-account.ts provides relatively easy way to do it for webapp. Note that it limits access to a give contract ID, so if you need unrestricted access you basically just need to omit contractId.

    See examples at https://near.dev/ for WalletAccount usage.