GithubHelp home page GithubHelp logo

bip32-dart's Introduction

bip32

A BIP32 compatible library for Flutter writing in Dart.

Inspired by bitcoinjs

Example

import 'package:bip32/bip32.dart' as bip32;
import 'package:hex/hex.dart';

main() {
  bip32.BIP32 node = bip32.BIP32.fromBase58('xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi');

  print(HEX.encode(node.privateKey));
  // => e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35

  bip32.BIP32 nodeNeutered = node.neutered();
  print(nodeNeutered.isNeutered());
  // => true

  print(HEX.encode(nodeNeutered.publicKey));
  // => 0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2

  print(nodeNeutered.toBase58());
  // => xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8

  bip32.BIP32 child = node.derivePath('m/0/0');
  print(child.toBase58());
  // => xprv9ww7sMFLzJMzur2oEQDB642fbsMS4q6JRraMVTrM9bTWBq7NDS8ZpmsKVB4YF3mZecqax1fjnsPF19xnsJNfRp4RSyexacULXMKowSACTRc

  print(HEX.encode(child.privateKey));
  // => f26cf12f89ab91aeeb8d7324a22e8ba080829db15c9245414b073a8c342322aa

  bip32.BIP32 childNeutered = child.neutered();
  print(childNeutered.isNeutered());
  // => true

  print(HEX.encode(childNeutered.publicKey));
  // => 02756de182c5dd4b717ea87e693006da62dbb3cddaa4a5cad2ed1f5bbab755f0f5

  print(childNeutered.toBase58());
  // => xpub6AvUGrnEpfvJ8L7GLRkBTByQ9uBvUHp9o5VxHrFxhvzV4dSWkySpNaBoLR9FpbnwRmTa69yLHF3QfcaxbWT7gWdwws5k4dpmJvqpEuMWwnj

  bip32.BIP32 nodeFromSeed = bip32.BIP32.fromSeed(HEX.decode("000102030405060708090a0b0c0d0e0f"));
  print(nodeFromSeed.toBase58());
  // => xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi

  bip32.BIP32 nodeFromPub = bip32.BIP32.fromBase58("xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8");
  print(nodeFromPub.toBase58());
  // => xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8

  var message = HEX.decode("0202020202020202020202020202020202020202020202020202020202020202");
  var signature = nodeFromSeed.sign(message);
  print(signature);
  // => [63, 219, 20, 114, 95, 184, 192, 55, 216, 206, 126, 121, 17, 71, 64, 70, 163, 82, 247, 73, 243, 95, 30, 137, 177, 155, 100, 225, 177, 203, 217, 147, 122, 64, 208, 129, 54, 133, 113, 41, 216, 160, 191, 15, 136, 98, 235, 25, 219, 178, 70, 222, 127, 151, 135, 242, 25, 192, 161, 187, 187, 84, 81, 215]

  print(HEX.encode(signature));
  // => 3fdb14725fb8c037d8ce7e7911474046a352f749f35f1e89b19b64e1b1cbd9937a40d08136857129d8a0bf0f8862eb19dbb246de7f9787f219c0a1bbbb5451d7

  print(nodeFromSeed.verify(message, signature));
  // => true
}

Contributor

bip32-dart's People

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

bip32-dart's Issues

Update to pointycastle 2.0 broke things

Hi,

I have the following code:

final mnemonic = bip39.generateMnemonic();
final seed = bip39.mnemonicToSeed(mnemonic);
final root = bip32.BIP32.fromSeed(seed); // error: Private key not in range [1, n] since pointycastle 2.0

Line three throws an error Private key not in range [1, n]

The same code seems to work well when overriding dependencies with an old pointycastle version:

dependency_overrides:
  pointycastle: ^1.0.2

Thank you.

Upgrade pointycastle version

I want to use bip32 and the newest version of the pointycastle library.
But since bip32 depends on an older version of pointycastle, it would be awesome if you could update the used version.

Error when using ecc.sign(hash,x)

I‘ve got an error when using function sign(hash, x) which is import 'package:bip32/src/utils/ecurve.dart' as ecc;

var key = '8c39fb3d889b6be22850254dc7ce3247c559d9f968785d88251ee4457633e335';
var msg = '6ad99bc30926fd79cf5a977044088fd298a59728c5b0c1040713d3a8e1c1e69b';

var sig = sign(Uint8List.fromList(HEX.decode(msg)), Uint8List.fromList(HEX.decode(key)));

The expected sig is 00349f1a2df670db4ec676674489f12684afb0ca8225e02a1299c01b8c9c99804837d43fce2267e2200526f0ad304360a5371f859698c2a1c042004c85f1c985

But it throw an error:

Unhandled exception:
Bad state: Too few elements
#0      _TypedIntListMixin.setRange (dart:typed_data-patch/typed_data_patch.dart:408:7)
#1      sign (package:bip32/src/utils/ecurve.dart:134:10)
#2      sign (file:///D:/intchains/TEST/wallet/sign.dart:104:10)
#3      main (file:///D:/intchains/TEST/wallet/sign.dart:94:13)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

I just found it’s because the length of encodeBigInt(sig.r) is 31, but buffer.setRange(0, 32, list) need 32.
If the result encodeBigInt(sig.r) can be length 32 with 0 fixed in the front of the list, it will be right.

I hope this problem can be fixed.

Publish the latest updates on pub.dev

Hello, would it be possible to publish on pub.dev the changes made with this pr #3?
We have developed a package based on these libraries, but the address derivation is wrong because there is no pull request fix.
Thanks

negativeFlag bug

Hi

into codes used negativeFlag, In the event that not implement or declare for that !!!!

bip32-1.0.10/lib/src/utils/ecurve.dart:236:74: Error: Getter not found: 'negativeFlag'.
needsPaddingByte = ((number >> (rawSize - 1) * 8) & negativeFlag) == negativeFlag ? 1 : 0;
...................................................................................................... ^^^^^^^^^^^^

NetworkType

why don't work NetworkType

into codes used NetworkType that can as argument gave to this code 'bip32.BIP32.fromSeed(seed)' and with not giving this argument, bitcoin network is default but at all no matter and dont work!!!

invalid derivation result for specific path

seed:xprv9s21ZrQH143K3Jpuz63XbuGs9CH9xG4sniVBBRVm6AJR57D9arxWz6FkXF3JSxSK7jUmVA11AdWa6ZsUtwGztE4QT5i8Y457RRPvMCc39rY
path: m/1'/199007533'/627785449'/1521366139'/1'
result: xprvA39a1i4ieYqGXFQSJ1HtoUVPqzUXU1JgUW4RMgzhv8HuPvkjWBKpMCxFGU4tJKfH83m6QmiWUe2w1zfQBxRkoAoSVtYE6ZbA9RDBjZiPU7M
expected: xprvA39a1i4ieYqGUQ7G1KGnaGzGwm7v3emjms3QN4jZ3HPeubXjshA3XjD5XFaiNgWFvoyC2NV5jN4eFcsVhkrWkvwR4qjdPbue3kpt6Ur3JRf

expected value verified both using bitcoinjs/bip32 as well as bip32.org.

Looks like the derivation is skipping to the next index due to the check in bip_base.dart#109

Deriving m/1'/199007533'/627785449'/1521366139' produces the correct result so only the last segment causes the issue.

Test code:

  test('bug', () {
    final hd = BIP32.fromBase58('xprv9s21ZrQH143K3Jpuz63XbuGs9CH9xG4sniVBBRVm6AJR57D9arxWz6FkXF3JSxSK7jUmVA11AdWa6ZsUtwGztE4QT5i8Y457RRPvMCc39rY');
    final d = hd.derivePath("m/1'/199007533'/627785449'/1521366139'/1'");
    expect(d.toBase58(), 'xprvA39a1i4ieYqGUQ7G1KGnaGzGwm7v3emjms3QN4jZ3HPeubXjshA3XjD5XFaiNgWFvoyC2NV5jN4eFcsVhkrWkvwR4qjdPbue3kpt6Ur3JRf');
  });

Can't derive xpub

When deriving an xpub I have the error Missing private key for hardened child key. Aren't you supposed to be able to derive with only the public key, for watch only wallets for example ?

import 'package:bip32/bip32.dart' as bip32;

void main(List<String> arguments) {
  final base58 = 'xpub661MyMwAqRbcFxtSX21UXfaDwmdN5zmJ1C8t1cjviNkkW6gXCd7AcfciXRuhPerjhnpziEXb2fsWkAVsNEJhgM5BPgnenuDDXVPbJSadrUG';
  final node = bip32.BIP32.fromBase58(base58);
  final derived = node.derivePath("m/44'/0'/0'/0/0");
  }

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.