Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base64 Private key conversion suibase <> chrome wallets #75

Open
georgescharlesbrain opened this issue Aug 30, 2023 · 6 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@georgescharlesbrain
Copy link

How do I go from my Suibase required base64 private key to 

TS SDK required pvt key which is the same as the pvt key in the chrome wallets.

const keypair = Ed25519Keypair.fromSecretKey(Buffer.from(privateKey, 'base64'));

The pvt key from the chrome wallets don't work on a regen.

Thx

@mario4tier
Copy link
Member

mario4tier commented Aug 30, 2023

I did a quick test with latest suibase 0.1.5 and it did work for me ... so lets keep digging to find the difference/bug.

===

Please try this test key in your ~/suibase/workdirs/localnet/suibase.yaml:

add_private_keys:
  - 0x4d45fcbfb15f15ca068d87055ee32f130c9ae57cc058c5f79b4d2d2bd36fd4a5

This was taken from latest suiet 0.2.58 on Chrome.

After 'localnet regen' you should see the following public key:
0xc86169fd08606e2edd73f1914945c707763315d335bba6b44d218e607e145906

Works for me on Ubuntu.

In meantime I will be working on adding this as an automated test case and verify on MacOS later today.

@mario4tier mario4tier moved this from 📋 Backlog to 🏗 In progress in ChainMovers Open Tracking Aug 30, 2023
@mario4tier
Copy link
Member

mario4tier commented Aug 30, 2023

It seems there is may be two question/issue in your post.

base64 in suibase.yaml (and sui.keystore) context are for keypair with an extra leading byte to store the type. These are the 33 bytes Mysten Labs specific encoding.

The TS SDK you have here expect a 32 bytes base64.

More info here: https://suibase.io/cookbook/code/keypairs.html

You can go from 33<->32 bytes encoding with the sui keytool.

===

I am thinking to write an utility that you can feed "anything in" and convert to relevant alternatives in a way easy to parse/understand (it is almost all done within Suibase code already, just have to make it a nice standalone script).

Something like:

$ keyinfo 0x4d45fcbfb15f15ca068d87055ee32f130c9ae57cc058c5f79b4d2d2bd36fd4a5
web wallet public key : 0xc86169fd08606e2edd73f1914945c707763315d335bba6b44d218e607e145906
web wallet private key: 0x4d45fcbfb15f15ca068d87055ee32f130c9ae57cc058c5f79b4d2d2bd36fd4a5
sui.keystore keypair  : "AE1F/L+xXxXKBo2HBV7jLxMMmuV8wFjF95tNLSvTb9Sl"
fromsecretkey input   : "..."

$ keyinfo AE1F/L+xXxXKBo2HBV7jLxMMmuV8wFjF95tNLSvTb9Sl
web wallet public key : 0xc86169fd08606e2edd73f1914945c707763315d335bba6b44d218e607e145906
web wallet private key: 0x4d45fcbfb15f15ca068d87055ee32f130c9ae57cc058c5f79b4d2d2bd36fd4a5
sui.keystore keypair  : "AE1F/L+xXxXKBo2HBV7jLxMMmuV8wFjF95tNLSvTb9Sl"
fromsecretkey input   : "..."

$ keyinfo run orient life curious antique major snake amused cruel labor bubble dizzy
web wallet public key : 0xc86169fd08606e2edd73f1914945c707763315d335bba6b44d218e607e145906
web wallet private key: 0x4d45fcbfb15f15ca068d87055ee32f130c9ae57cc058c5f79b4d2d2bd36fd4a5
sui.keystore keypair  : "AE1F/L+xXxXKBo2HBV7jLxMMmuV8wFjF95tNLSvTb9Sl"
fromsecretkey input   : "..."

@georgescharlesbrain
Copy link
Author

Hi,

That tool could be useful. Yes, during my tests, the typescript error mentioned that the function was expecting a 32-byte base64 pvt key.

My challenge is indeed that there are 2 versions of the same base64 private key:
33 bytes used in keystore and suibase
32 bytes used in TS and Ethos

It seems that the private keys in the chrome wallets are:
Ethos: base64 32bytes, HEX missing 0x, UInt8Array
Suiet: HEX missing 0x
Sui: HEX including 0x

@georgescharlesbrain
Copy link
Author

More info here: suibase.io/cookbook/code/keypairs.html

👍

@georgescharlesbrain
Copy link
Author

Maybe useful for in the cookbook:

import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';
import dotenv from 'dotenv';
dotenv.config();

// Ethos chrome wallet: PRIVATE_KEY_BASE64_32BYTES, PRIVATE_KEY_HEX_0X_REMOVED
// Suiet chrome wallet: PRIVATE_KEY_HEX_0X_REMOVED
// Sui chrome wallet: HEX including 0x --> remove the 0x manually to get PRIVATE_KEY_HEX_0X_REMOVED
// keystore file: PRIVATE_KEY_BASE64_33BYTES
// Suibase: PRIVATE_KEY_BASE64_33BYTES or HEX including 0x

const keypair = Ed25519Keypair.fromSecretKey(Buffer.from(process.env.PRIVATE_KEY_BASE64_32BYTES as string, 'base64'));
const keypair = Ed25519Keypair.fromSecretKey(Buffer.from(process.env.PRIVATE_KEY_BASE64_33BYTES as string, 'base64').slice(1,33));
const keypair = Ed25519Keypair.fromSecretKey(Buffer.from(process.env.PRIVATE_KEY_HEX_0X_REMOVED as string, 'hex'));

const publicKey= keypair.getPublicKey().toSuiAddress();

NOT REQUIRED ANYMORE:
import { fromB64 } from "@mysten/bcs";

@mario4tier
Copy link
Member

Great. Please go ahead with this contribution to the cookbook (can conveniently be done with the "edit this page on github" link), and by the way, you are very welcome to edit a lot more if you want to 😄

I assume at this point you are all set with the initial issue (and you can load the private key in suibase).

I will leave the case open so I can attach the following enhancement later:

  • New automated regression test
  • Support mnemonics in suibase.yaml
  • Support 32 bytes variations (hex or base64) in suibase.yaml. That way the user can specify "anything" and it will "just work".
  • New "keyinfo" utility (using same code that process the suibase.yaml).

@mario4tier mario4tier added the enhancement New feature or request label Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: 🏗 In progress
Development

No branches or pull requests

2 participants