Git Product home page Git Product logo

farukterzioglu / hdwallet Goto Github PK

View Code? Open in Web Editor NEW
73.0 4.0 34.0 575 KB

A generic HD wallet for Elliptic Curve (Secp256k1) and Edwards-curve (Ed25519) based crypto currencies like Bitcoin, Ethereum, Cosmos, Tezos, Tron, Cardano, Polkadot, Avalanche, FileCoin and more.

License: MIT License

C# 99.88% Dockerfile 0.12%
hd-wallet crypto-currency bip32 secp256k1 ed25519 tron cardano polkadot avalanche tezos

hdwallet's Introduction

NuGet Build status

HD Wallet

A generic HD wallet for Elliptic Curves (Secp256k1/Secp256r1) and Edwards-curve (Ed25519) based crypto currencies.

Supported blockchains

  • Avalanche
  • Bitcoin
  • Cardano
  • Ethereum
  • FileCoin
  • Neo3
  • Polkadot
  • Solana
  • Stacks
  • Terra
  • Tezos
  • Tron
  • Bitcoin-like (Litecoin, Dash, Doge, ...) (WIP)

HD wallets can be generated from mnemonic (w/ or w/o passphrase) or from extended private key (xprv) and non-hd wallets can be generated directly from private key.

HD wallets can derive sub accounts, and from that accounts external (deposit) wallets or internal (change) wallets can be derived. Using generated wallets, addresses can be retrived by implementing address generators for related crypto currency.

SDK (.Net Core)

An SDK for wallet creation in a hierarchical and deterministic way. From one mnemonic, multiple wallets can be created for multiple blockchains. You don't need to store private keys for multiple crypto addresses. They can be re-created deterministically. .Net developers can use this SDK to create HD wallet within their programs. This SDK doesn't store secrets or manage wallet. Mnemonic (or seed/private key) need to be given for wallet creation.
Also regular wallet can be created from private keys.

SDK for new blockchains (.Net Core)

By using HDWallet.Secp256k1 project, any Elliptic Curve (Secp256k1) based crypto currency wallet can be generated by defining the purpose (e.g. BIP44) and the coin type (e.g. 195 for Tron).
HDWallet.Tron project is already ready to go HD wallet for Tron (TRX) which uses HDWallet.Secp256k1 project.

By using HDWallet.Ed25519 project, any Edwards-curve (Ed25519) based crypto currency wallet can be generated by defining purpose (e.g. BIP44) and coin type (e.g. 1852 for Cardano).

API

An API can be hosted as Docker container or run as .Net Core app and will receive requests for address generation. Generated addresses (or private keys) won't be stored and will be re-generated when ever queried.

Sign server (WIP)

If activated within config, a sign server with receive messages to be signed and in reponse, signature and public key that signed the message will be returned.

How To

How to use the SDK

Sample for generating an HD Wallet for Bitcoin (purpose: BIP84, coin type: 0) from mnemonic and getting the first account's first deposit wallet;

IHDWallet<BitcoinWallet> bitcoinHDWallet = new BitcoinHDWallet("conduct stadium ask orange vast impose depend assume income sail chunk tomorrow life grape dutch", "");
var depositWallet0 = bitcoinHDWallet.GetAccount(0).GetExternalWallet(0);        
var address = depositWallet0.Address;

Sample for generating HD Wallet (m/44'/0'/0') from extended private key;

var accountExtendedPrivateKey = "xprv9xyvwx1jBEBKwjZtXYogBwDyfXTyTa3Af6urV2dU843CyBxLu9J5GLQL4vMWvaW4q3skqAtarUvdGmBoWQZnU2RBLnmJdCM4FnbMa72xWNy";
IAccount<BitcoinWallet> accountWallet = new Account<BitcoinWallet>(accountExtendedPrivateKey);

var depositWallet0 = accountWallet.GetExternalWallet(0);

How to use the API

For multiple accounts

curl 'https://localhost:5001/bitcoin/0/external/0' 
curl 'https://localhost:5001/bitcoin/0/external/1'
curl 'https://localhost:5001/bitcoin/0/internal/0'
curl 'https://localhost:5001/bitcoin/0/internal/1'

For single account

curl 'https://localhost:5001/bitcoin/account/external/0'
curl 'https://localhost:5001/bitcoin/account/internal/0'

Signing (WIP)

curl -X POST 'https://localhost:8080/api/v1/tron/account/0/address/0/sign' --header 'Content-Type: application/json' --data-raw '{
    "message":"MESSAGE-TO-SIGN"
}'

{
  "signature" : "[SIGNATURE]",
  "publickey" : "[PUBLICKEY]"
}

How to Run

With Docker [WIP]

~ docker run -e mnemonic="conduct stadium ask orange vast impose depend assume income sail chunk tomorrow life grape dutch" -e passphrase=P@55PHR@S3 -p 8080:80 hdwallet-api 

➜  ~ docker run -e accounthdkey=xprv9xyvwx1jBEBKwjZtXYogBwDyfXTyTa3Af6urV2dU843CyBxLu9J5GLQL4vMWvaW4q3skqAtarUvdGmBoWQZnU2RBLnmJdCM4FnbMa72xWNy -p 8080:80 hdwallet-api 

From terminal

~ dotnet hdwallet-api.dll

Authentication

Cookie based [WIP]
Cookie based authentication activated by default. As the application starts, a cookie file created at ~/.hdwallet/.cookie (can be configured w/ --cookiefile flag). Api user can authenticate with Basic Authentication using credentials in cookie file.

If one wants to activate sign server feature, a flag is needed as --signserver

Configuration

from appSettings.json

{
  "mnemonic" : "conduct stadium ask orange vast impose depend assume income sail chunk tomorrow life grape dutch",
  "passphrase" : "P@55PHR@S3"
}

{
  "accounthdkey" : "xprv9xyvwx1jBEBKwjZtXYogBwDyfXTyTa3Af6urV2dU843CyBxLu9J5GLQL4vMWvaW4q3skqAtarUvdGmBoWQZnU2RBLnmJdCM4FnbMa72xWNy"
}

from Environment

~ export MNEMONIC="conduct stadium ask orange vast impose depend assume income sail chunk tomorrow life grape dutch"~ export PASSPHRASE=P@55PHR@S3
➜  ~ export ACCOUNTHDKEY=xprv9xyvwx1jBEBKwjZtXYogBwDyfXTyTa3Af6urV2dU843CyBxLu9J5GLQL4vMWvaW4q3skqAtarUvdGmBoWQZnU2RBLnmJdCM4FnbMa72xWNy

➜  ~ dotnet hdwallet-api.dll

from parameters

~ dotnet hdwallet-api.dll --mnemonic "conduct stadium ask orange vast impose depend assume income sail chunk tomorrow life grape dutch" --passphrase P@55PHR@S3

➜  ~ dotnet hdwallet-api.dll --accounthdkey xprv9xyvwx1jBEBKwjZtXYogBwDyfXTyTa3Af6urV2dU843CyBxLu9J5GLQL4vMWvaW4q3skqAtarUvdGmBoWQZnU2RBLnmJdCM4FnbMa72xWNy

Sample for signing messages with generated wallets;

...
var signature = depositWallet0.Sign(messageBytes);

Build the api

docker build -f src/HDWallet.Api/Dockerfile -t hdwallet-api .

hdwallet's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hdwallet's Issues

Need Offline build transaction and sign

The way to create transactions is not completely offline, which is a serious problem. When signing, you cannot guarantee whether the contents of the trading partner are not tampered with.

Balance and transactions

Hello. Thank you for sharing this project. I have some questions

After creating hd wallet of tron and generate some address on it how to get the balance and transactions of that address?

May you please help me hot to get a webhook data when new deposit on network for the generated address take place?

Add post actions to return wallet details

Add relevant methods to return address-private key pairs
e.g.

[HttpPost("/Avalanche/{account}/external/{index}")]
public ActionResult<KeyPair> GetDepositWallet(uint account, uint index)

For only apps that allowed this by configuration exposePrivateKey

should return a response like;

{
 "address": "X-avax1....",
 "privateKey" : "adf54djmr...."
}

Tron/Ethereum HD Wallet

Is there a way using this library I can use the public keys only of a tron/eth HDWallet to get the deterministic wallet addresses? Ive seen this done using the Nethereum library but wondered if it was possible with this library for both Tron and Ethereum?

Some Cryptos (Terra, Avalanche ...) Do Not Work With NBitcoin 7.0.5

The HDWallet project is referring to NBitcoin version 5 currently.

When I'm referring NBitcoin version 7 together with HDWallet, I have error when generating HDWallet.Terra and HDWallet.Avalanche.

No implementation:
at NBitcoin.IBitcoinSerializable.ReadWrite(BitcoinStream stream)
at NBitcoin.BitcoinSerializableExtensions.ToBytes(IBitcoinSerializable serializable, Nullable1 version) at HDWallet.Secp256.AccountSecpBase1.GetWallet(UInt32 addressIndex, Boolean isInternal)
at HDWallet.Secp256.AccountSecpBase`1.HDWallet.Core.IAccount.GetExternalWallet(UInt32 addressIndex)

Will there be future support for the NBitcoin 7+?

Stacks addresses incorrect

Hi, i found incorrect line in your method for the stacks coin, address generator
var publicKey = pubKey.Decompress();
Why you decompress key? I replace this line on
var publicKey = pubKey;
And it's good working, addresses equals for the hiro wallet and other for the Stacks.

Cleanup non issue

When my account was compromised a spam issue was created in this repo. I sincerely apologize. Cleaning up such issues via script.

Aggregate values

My problem is that the wallet addresses are made in HD, but unfortunately the amounts are not available after depositing in index 0.

This is my step.

1.I create HD and external wallet addresses:

string words = "mmm ..."
IHDWallet<TronWallet> wallet = new TronHDWallet(words);
var account0 = wallet.GetAccount(0).GetExternalWallet(0);
var account2 = wallet.GetAccount(0).GetExternalWallet(1);
var account3 = wallet.GetAccount(0).GetExternalWallet(2);

2.Then I transfer some TRX to account 2 & account 3.

3.Then I add the default wallet address based on the words in the tronlink.

Unfortunately I do not have access to the amount through a single wallet!

Tron Signature

In the documentation of the Tron it is written: Note: The size of the signature result is 65 bytes.
https://tronprotocol.github.io/documentation-en/mechanism-algorithm/account/#signature
The sign method returns a 68-byte signature to you, if you use your signature method to send a regular transfer, everything will work correctly, but if you use it for TRC 20 TriggerSmartContract when you broadcast, we will get a "SIGERROR"
@farukterzioglu Could you fix this? This is the only library I know on C # that allows you to sign a transaction offline, it would be great if we could use it to sign TriggerSmartContract

Generating Deterministic Polkadot Addresses

With my implementation, anyone can generate Polkadot addresses based on Bip32-Ed25519.

My implementation has the same results with test vectors here;
https://github.com/satoshilabs/slips/blob/master/slip-0010.md#test-vector-1-for-ed25519

I can also confirm that addresses I generated is same with generate from subkey with private key if I investigate it.

Results from my code;

Address: 5G6tXDeie2KaUgGrwkBfzApKSCm9sE5QvBm1bi1vmBZ4gcCN
Public key: b29b533725c02f6e69d8774c92d8a5a98506c2f09e13a1adbe4db367fbfa512a
Private key: 8bd78fe8b30abf91d3e9474c8927d9874fabc7e31ce2d866cf795378161f954a
Expanded private key: 8bd78fe8b30abf91d3e9474c8927d9874fabc7e31ce2d866cf795378161f954ab29b533725c02f6e69d8774c92d8a5a98506c2f09e13a1adbe4db367fbfa512a
Result from Subkey

subkey inspect 0x8bd78fe8b30abf91d3e9474c8927d9874fabc7e31ce2d866cf795378161f954a --scheme Ed25519
Secret Key URI 0x8bd78fe8b30abf91d3e9474c8927d9874fabc7e31ce2d866cf795378161f954a is account:
Secret seed: 0x8bd78fe8b30abf91d3e9474c8927d9874fabc7e31ce2d866cf795378161f954a
Public key (hex): 0xb29b533725c02f6e69d8774c92d8a5a98506c2f09e13a1adbe4db367fbfa512a
Account ID: 0xb29b533725c02f6e69d8774c92d8a5a98506c2f09e13a1adbe4db367fbfa512a
SS58 Address: 5G6tXDeie2KaUgGrwkBfzApKSCm9sE5QvBm1bi1vmBZ4gcCN
When I investigate the private key from my hd wallet implementation, Subkey prints the same address as I generated.

The problem here that, my implementation is not suitable with official hd wallet implementation which uses path like P//hard//soft (where P is mnemonic)
I rather use a path as m/44/354/0/0/0, to define 0. account's 0. deposit wallet.

The gotcha here is that, from same mnemonic I can't produce the same set of addresses with other available wallets.

Deposit USDT TRC20

Hello. I have created 3 address from account 0 of Tron HD Wallet. Now the question is can I deposit usdt trc20 to these 3 addresses? or first must deposit some trx to every address? I want to get every user in my website one address to deposit usdt trc20 without need to deposit trx first. Can I do this?

Hardened deriviation path

Hi. I looked the code, and found there it:

public ExtKey Derive(uint index)
{
    (var childkey, var childcc) = this.Key.Derivate(this.ChainCode, index + hardenedOffset);
    return new ExtKey(childkey, childcc);
}

in HDWallet.BIP32.Ed25519/ExtKey.cs

Why all childs always hardened?

Internal Address

Hello. What is internal address in hd wallet of tron for? What is the diference between address 0 of account 0 of an mnemonic in deposit and internal mode?

Scan 100,000 wallets and get notified after receiving coinsReceive when receiving coins

I have a question.
I have 100,000 wallet addresses and I want to be notified immediately if a token is deposited into it, like a webhook / event listener or something like that.
Is there a way that is basic and we can do it? Like wallet applications, in the past I used to regularly query and compare transactions, but this is not good at all.

Example:
https://api.trongrid.io/v1/accounts/{address}/transactions/trc20?only_confirmed=true&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t&only_to=true&limit=200

for each address

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.