Skip to content

Commit

Permalink
Bugfix: Properly decode xpriv/tpriv network bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan February committed Oct 28, 2023
1 parent 31fa906 commit ba687f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/src/encoding/ckdserializer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import 'package:collection/collection.dart';

import '../networks.dart';
import 'package:hex/hex.dart';
import 'dart:convert';
Expand Down Expand Up @@ -38,6 +40,8 @@ abstract class CKDSerializer {

var version = HEX.encode(this._versionBytes.map( (elem) => elem.toUnsigned(8) ).toList());

this.networkType = getXPrivNetworkType(HEX.decode(version));

}

// FIXME: Rewrite using the Buffer class
Expand Down Expand Up @@ -84,6 +88,21 @@ abstract class CKDSerializer {
}
}

Function eq = const ListEquality().equals;

NetworkType getXPrivNetworkType(List<int> versionBytes){

if (eq(versionBytes, MAINNET_PUBLIC) || eq(versionBytes, MAINNET_PRIVATE)){
return NetworkType.MAIN;
}

if (eq(versionBytes, TESTNET_PUBLIC) || eq(versionBytes, TESTNET_PRIVATE)){
return NetworkType.TEST;
}

return NetworkType.TEST;
}

set chainCode(List<int> bytes) {
this._chainCode = bytes;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/src/hdprivatekey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class HDPrivateKey extends CKDSerializer{
/// Reconstruct a private key from a standard `xpriv` string.
///
HDPrivateKey.fromXpriv(String vector){
this.networkType = NetworkType.MAIN;



this.keyType = KeyType.PRIVATE;

this.deserialize(vector);
Expand Down
1 change: 0 additions & 1 deletion lib/src/hdpublickey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class HDPublicKey extends CKDSerializer {

/// Constructs a new public key from it's `xpub`-encoded representation
HDPublicKey.fromXpub(String vector){
this.networkType = NetworkType.MAIN;
this.keyType = KeyType.PUBLIC;
deserialize(vector);
}
Expand Down

0 comments on commit ba687f3

Please sign in to comment.