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

How can I create an Account from a WIF? #3167

Open
lock9 opened this issue Feb 26, 2024 · 8 comments
Open

How can I create an Account from a WIF? #3167

lock9 opened this issue Feb 26, 2024 · 8 comments
Labels
question Used in questions

Comments

@lock9
Copy link
Contributor

lock9 commented Feb 26, 2024

Hi,

I'm creating examples for contract deployments and updates. I'm unable to create an example for Neo using C#.
I can't find how to create an account using a WIF.

Here is the initial code:

using Neo.Network.RPC;
using Neo.Wallets;

// Configure the Network
var client = new RpcClient(new Uri("https://testnet1.neo.coz.io"));

// Configure the Account
var wif = "KzAuju4yBqBhmUzYpfEEppPW8jfxALTsdsUR8hLPv9R3PBD97CUv";
var account = Wallet.GetPrivateKeyFromWIF(wif);

// ???

Here is how to achieve it in Java and Python.

Java:

package coin;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Paths;
import io.neow3j.contract.ContractManagement;
import io.neow3j.contract.NefFile;
import io.neow3j.contract.SmartContract;
import io.neow3j.protocol.Neow3j;
import io.neow3j.protocol.ObjectMapperFactory;
import io.neow3j.protocol.core.response.ContractManifest;
import io.neow3j.protocol.core.response.NeoSendRawTransaction;

import io.neow3j.protocol.http.HttpService;
import io.neow3j.transaction.AccountSigner;
import io.neow3j.transaction.Transaction;
import io.neow3j.types.ContractParameter;
import io.neow3j.types.Hash160;

import io.neow3j.wallet.Account;

public class DeployContractExample {

    public static void main(String[] args) throws Throwable {
        // Configure the Network
        Neow3j neow3j = Neow3j.build(new HttpService("https://testnet1.neo.coz.io"));

        // Configure the Account
        String wif = "KzAuju4yBqBhmUzYpfEEppPW8jfxALTsdsUR8hLPv9R3PBD97CUv";
        Account account = Account.fromWIF(wif);

        // Load the contract's NEF file:
        File contractNefFile = Paths.get("Coin.nef").toFile();
        NefFile nefFile = NefFile.readFromFile(contractNefFile);

        // Load the contract's manifest file:
        File contractManifestFile = Paths.get("coin.manifest.json").toFile();
        ContractManifest manifest;
        try (FileInputStream s = new FileInputStream(contractManifestFile)) {
            manifest = ObjectMapperFactory.getObjectMapper().readValue(s, ContractManifest.class);
        }

        // Convert the NEF file and the manifest to ContractParameters
        ContractParameter nefParameter = ContractParameter.byteArray(nefFile.toArray());
        ContractParameter manifestParameter = ContractParameter
                .byteArray(ObjectMapperFactory.getObjectMapper().writeValueAsBytes(manifest));

        // Contract Management Contract Hash
        Hash160 contractHash = ContractManagement.SCRIPT_HASH;

        // Create the Deployment Transaction
        Transaction deploymentTransaction = new SmartContract(contractHash, neow3j)
                .invokeFunction("deploy", nefParameter, manifestParameter, null)
                .signers(AccountSigner.global(account))
                .sign();

        // Send the Deployment Transaction
        NeoSendRawTransaction response = deploymentTransaction.send();

        System.out.printf("The contract was deployed in transaction %s\n",
                response.getSendRawTransaction().getHash());
    }

}

Python:

import asyncio
from neo3.api.wrappers import GenericContract, ChainFacade
from neo3.api.helpers.signing import sign_insecure_with_account
from neo3.contracts import nef, manifest
from neo3.network.payloads.verification import Signer
from neo3.wallet.account import Account


async def main():
    facade = ChainFacade.node_provider_testnet()

    # Load the contract files
    nef_file = nef.NEF.from_file("Coin.nef")
    manifest_file = manifest.ContractManifest.from_file(
        "Coin.manifest.json"
    )

    # Load the account.
    account_wif = "KzAuju4yBqBhmUzYpfEEppPW8jfxALTsdsUR8hLPv9R3PBD97CUv"
    # Select a password for the account
    account_password = "pass"
    account = Account.from_wif(account_wif, account_password)

    # Set the account as signer
    facade.add_signer(sign_insecure_with_account(
        acc=account, password=account_password),
        Signer(account.script_hash),
    )

    # Create the deployment invocation
    deploy_invocation = GenericContract.deploy(
        nef=nef_file, manifest=manifest_file)

    # Send the invocation to the network
    result = await facade.invoke(deploy_invocation)

    print(result.tx_hash)


if __name__ == "__main__":
    asyncio.run(main())
@lock9 lock9 added the question Used in questions label Feb 26, 2024
@lock9
Copy link
Contributor Author

lock9 commented Feb 26, 2024

Maybe this isn't the correct repository to post this. I'm not sure where to put it. Can someone transfer this issue to the correct repository?

@shargon
Copy link
Member

shargon commented Feb 26, 2024

public abstract WalletAccount CreateAccount(byte[] privateKey);

Call CreateAccount(account) after extract the pk from wif

@lock9
Copy link
Contributor Author

lock9 commented Feb 26, 2024

But that's an abstract method. Where can I find an implementation?

@shargon
Copy link
Member

shargon commented Feb 26, 2024

But that's an abstract method. Where can I find an implementation?

Nep6Wallet for example

@lock9
Copy link
Contributor Author

lock9 commented Feb 26, 2024

That's a NEP-6 wallet. It requires initialization parameters:

public NEP6Wallet(string path, string password, ProtocolSettings settings, string name = null) : base(path, settings)

It's not a static method, we need to instantiate NEP6Wallet first

@superboyiii
Copy link
Member

            var prikey = Wallet.GetPrivateKeyFromWIF(wif);
            KeyPair key = new(prikey);
            var publickey = key.PublicKey;
            var scripthash = Contract.CreateSignatureRedeemScript(publickey).ToScriptHash();
            var address = scripthash.ToAddress(0x35);
            Console.WriteLine(address);

@lock9 Is this you want?

@lock9
Copy link
Contributor Author

lock9 commented Mar 1, 2024

Hi @superboyiii ,

Sort of. I need a wallet or account that can be used to deploy a smart contract, similar to the examples in Java or Python that I've posted above.

@Hecate2
Copy link
Contributor

Hecate2 commented Jun 3, 2024

Hi @superboyiii ,

Sort of. I need a wallet or account that can be used to deploy a smart contract, similar to the examples in Java or Python that I've posted above.

https://github.com/Hecate2/neo-fairy-test/blob/1f3ba9ff5c04d1dbfef9d1ba6e0b341417469fb7/Fairy.Wallet.cs#L147-L163

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Used in questions
Projects
None yet
Development

No branches or pull requests

4 participants