bmresearch / Solnet

Solana's .NET SDK and integration library.
https://blockmountain.io/Solnet
MIT License
319 stars 128 forks source link

How to create an Account based on the private key of Base58? #418

Closed JavinYang closed 6 months ago

JavinYang commented 1 year ago

I can use github.com/gagliardetto/solan-go to create an Account (Wallet) through private key (base58)

// Wallet is a wrapper around a PrivateKey
type Wallet struct {
    PrivateKey PrivateKey
}

func NewWallet() *Wallet {
    privateKey, err := NewRandomPrivateKey()
    if err != nil {
        panic(fmt.Sprintf("failed to generate private key: %s", err))
    }
    return &Wallet{
        PrivateKey: privateKey,
    }
}

func WalletFromPrivateKeyBase58(privateKey string) (*Wallet, error) {
    k, err := PrivateKeyFromBase58(privateKey)
    if err != nil {
        return nil, fmt.Errorf("account from private key: private key from b58: %w", err)
    }
    return &Wallet{
        PrivateKey: k,
    }, nil
}

func (a *Wallet) PublicKey() PublicKey {
    return a.PrivateKey.PublicKey()
}

...

But SolNet needs PrivateKey and PublicKey to create an account

/// <summary>
        /// Initialize an account with the passed private and public keys.
        /// </summary>
        /// <param name="privateKey">The private key.</param>
        /// <param name="publicKey">The public key.</param>
        public Account(string privateKey, string publicKey)
        {
            PrivateKey = new PrivateKey(privateKey);
            PublicKey = new PublicKey(publicKey);
        }

How can I create an account using only PrivateKey?

BifrostTitan commented 6 months ago

https://github.com/bmresearch/Solnet/pull/419/commits/a2161f97ccd02f9a2cf853ff35319f02a59517ec