Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #49 from Vesta-wallet/p2sh-segwit
Browse files Browse the repository at this point in the history
Simplify P2WPKH and P2PKH classes. Add P2SH-P2PKH address generation.
  • Loading branch information
willyfromtheblock authored Dec 21, 2022
2 parents 1db66b7 + 9478236 commit 15d233c
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 886 deletions.
4 changes: 3 additions & 1 deletion lib/coinslib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export 'src/transaction.dart';
export 'src/address.dart';
export 'src/transaction_builder.dart';
export 'src/ecpair.dart';
export 'src/payments/multisig.dart';
export 'src/payments/p2pkh.dart';
export 'src/payments/p2wpkh.dart';
export 'src/payments/index.dart';
export 'src/payments/p2sh.dart';
export 'src/payments/p2wsh.dart';
export 'src/bip32_base.dart';
9 changes: 2 additions & 7 deletions lib/src/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:coinslib/src/payments/p2wsh.dart';
import 'models/networks.dart';
import 'package:bs58check/bs58check.dart' as bs58check;
import 'package:coinslib/bech32/bech32.dart';
import 'payments/index.dart' show PaymentData;
import 'payments/p2pkh.dart';
import 'payments/p2wpkh.dart';
import 'payments/p2sh.dart';
Expand Down Expand Up @@ -35,9 +34,7 @@ class Address {
final data = decodeBase58.sublist(1);

if (prefix == network.pubKeyHash) {
P2PKH p2pkh =
P2PKH(data: PaymentData(address: address), network: network);
return p2pkh.data.output!;
return P2PKH.fromPublicKeyHash(data).outputScript;
}

if (prefix == network.scriptHash) {
Expand All @@ -64,9 +61,7 @@ class Address {
final progLen = program.length;

if (progLen == 20) {
P2WPKH p2wpkh =
P2WPKH(data: PaymentData(address: address), network: network);
return p2wpkh.data.output!;
return P2WPKH.fromPublicKeyHash(program).outputScript;
}

if (progLen == 32) {
Expand Down
85 changes: 36 additions & 49 deletions lib/src/coinslib_base.dart
Original file line number Diff line number Diff line change
@@ -1,79 +1,74 @@
// TODO: Put public facing types in this file.
import 'dart:typed_data';
import 'package:coinslib/src/utils/magic_hash.dart';
import 'package:hex/hex.dart';
import 'bip32_base.dart' as bip32;
import 'models/networks.dart';
import 'payments/index.dart' show PaymentData;
import 'payments/p2pkh.dart';
import 'ecpair.dart';

/// Checks if you are awesome. Spoiler: you are.
class HDWallet {
bip32.BIP32? _bip32;
P2PKH? _p2pkh;
bip32.BIP32 _bip32;
P2PKH _p2pkh;
String? seed;
NetworkType network;

String? get privKey {
if (_bip32 == null) return null;
try {
return HEX.encode(_bip32!.privateKey!);
return HEX.encode(_bip32.privateKey!);
} catch (_) {
return null;
}
}

Uint8List? get pubKeyBytes => _bip32?.publicKey;
String? get pubKey => _bip32 != null ? HEX.encode(_bip32!.publicKey) : null;
Uint8List get pubKeyBytes => _bip32.publicKey;
String get pubKey => HEX.encode(_bip32.publicKey);

String? get base58Priv {
if (_bip32 == null) return null;
try {
return _bip32!.toBase58();
return _bip32.toBase58();
} catch (_) {
return null;
}
}

String? get base58 => _bip32 != null ? _bip32!.neutered().toBase58() : null;
String get base58 => _bip32.neutered().toBase58();

String? get wif {
if (_bip32 == null) return null;
try {
return _bip32!.toWIF();
return _bip32.toWIF();
} catch (_) {
return null;
}
}

String? get address => _p2pkh != null ? _p2pkh!.data.address : null;
String get address => _p2pkh.address(network);

HDWallet({required bip32, required p2pkh, required this.network, this.seed}) {
_bip32 = bip32;
_p2pkh = p2pkh;
}
HDWallet({
required bip32,
required p2pkh,
required this.network,
this.seed,
}) : _bip32 = bip32,
_p2pkh = p2pkh;

HDWallet derivePath(String path) {
final bip32 = _bip32!.derivePath(path);
final p2pkh =
P2PKH(data: PaymentData(pubkey: bip32.publicKey), network: network);
final bip32 = _bip32.derivePath(path);
final p2pkh = P2PKH.fromPublicKey(bip32.publicKey);
return HDWallet(bip32: bip32, p2pkh: p2pkh, network: network);
}

HDWallet derive(int index) {
final bip32 = _bip32!.derive(index);
final p2pkh =
P2PKH(data: PaymentData(pubkey: bip32.publicKey), network: network);
final bip32 = _bip32.derive(index);
final p2pkh = P2PKH.fromPublicKey(bip32.publicKey);
return HDWallet(bip32: bip32, p2pkh: p2pkh, network: network);
}

factory HDWallet.fromSeed(Uint8List seed, {NetworkType? network}) {
network = network ?? bitcoin;
final seedHex = HEX.encode(seed);
final wallet = bip32.BIP32.fromSeed(seed, network);
final p2pkh =
P2PKH(data: PaymentData(pubkey: wallet.publicKey), network: network);
final p2pkh = P2PKH.fromPublicKey(wallet.publicKey);
return HDWallet(
bip32: wallet,
p2pkh: p2pkh,
Expand All @@ -85,62 +80,54 @@ class HDWallet {
factory HDWallet.fromBase58(String xpub, {NetworkType? network}) {
network = network ?? bitcoin;
final wallet = bip32.BIP32.fromBase58(xpub, network);
final p2pkh =
P2PKH(data: PaymentData(pubkey: wallet.publicKey), network: network);
final p2pkh = P2PKH.fromPublicKey(wallet.publicKey);
return HDWallet(bip32: wallet, p2pkh: p2pkh, network: network, seed: null);
}

Uint8List sign(String message) {
Uint8List messageHash = magicHash(message, network);
return _bip32!.signRecoverable(messageHash);
return _bip32.signRecoverable(messageHash);
}

bool verify({required String message, required Uint8List signature}) {
Uint8List messageHash = magicHash(message);
return _bip32!.verify(messageHash, signature);
return _bip32.verify(messageHash, signature);
}
}

class Wallet {
ECPair? _keyPair;
P2PKH? _p2pkh;

String? get privKey =>
_keyPair != null ? HEX.encode(_keyPair!.privateKey!) : null;

String? get pubKey =>
_keyPair != null ? HEX.encode(_keyPair!.publicKey!) : null;

String? get wif => _keyPair != null ? _keyPair!.toWIF() : null;

String? get address => _p2pkh != null ? _p2pkh!.data.address : null;
ECPair _keyPair;
P2PKH _p2pkh;
NetworkType network;

NetworkType? network;
String get privKey => HEX.encode(_keyPair.privateKey!);
String get pubKey => HEX.encode(_keyPair.publicKey!);
String get wif => _keyPair.toWIF();
String get address => _p2pkh.address(network);

Wallet(this._keyPair, this._p2pkh, this.network);

factory Wallet.random([NetworkType? network]) {
network ??= bitcoin;
final keyPair = ECPair.makeRandom(network: network);
final p2pkh =
P2PKH(data: PaymentData(pubkey: keyPair.publicKey), network: network);
final p2pkh = P2PKH.fromPublicKey(keyPair.publicKey!);
return Wallet(keyPair, p2pkh, network);
}

factory Wallet.fromWIF(String wif, [NetworkType? network]) {
network = network ?? bitcoin;
final keyPair = ECPair.fromWIF(wif, network: network);
final p2pkh =
P2PKH(data: PaymentData(pubkey: keyPair.publicKey), network: network);
final p2pkh = P2PKH.fromPublicKey(keyPair.publicKey!);
return Wallet(keyPair, p2pkh, network);
}

Uint8List sign(String message) {
Uint8List messageHash = magicHash(message, network);
return _keyPair!.signRecoverable(messageHash);
return _keyPair.signRecoverable(messageHash);
}

bool verify({required String message, required Uint8List signature}) {
Uint8List messageHash = magicHash(message, network);
return _keyPair!.verify(messageHash, signature);
return _keyPair.verify(messageHash, signature);
}
}
26 changes: 0 additions & 26 deletions lib/src/payments/index.dart

This file was deleted.

29 changes: 0 additions & 29 deletions lib/src/payments/p2pk.dart

This file was deleted.

Loading

0 comments on commit 15d233c

Please sign in to comment.