From 08f9f019f794d3f325bbaf1ead1fb76bf44c0c33 Mon Sep 17 00:00:00 2001 From: pedro-at-decenomy Date: Fri, 24 Sep 2021 10:37:22 +0100 Subject: [PATCH] adds classes for Sapphire and Jackpot, and the ability to check addresses. --- index.js | 102 +++++++++++++++++++++++++++++++++++++-------------- package.json | 3 +- sandbox.js | 44 ++++++++++++++++++++++ 3 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 sandbox.js diff --git a/index.js b/index.js index e29f50b..8b76d80 100644 --- a/index.js +++ b/index.js @@ -1,29 +1,75 @@ -const bitcoin = require('bitcoinjs-lib'); - -let network = { - messagePrefix: '\x18DarkNet Signed Message:\n', - bech32: 'bc', // we don't have it, however leave it like in Bitcoin - bip32: { - public: 0x022d2563, // boost::assign::list_of(0x02)(0x2D)(0x25)(0x63) - private: 0x0221312b, // boost::assign::list_of(0x02)(0x21)(0x31)(0x2B) - }, - pubKeyHash: 0x3F, // std::vector(1, 63) - scriptHash: 0x12, // std::vector(1, 18) - wif: 0x19, // std::vector(1, 25) -}; - -let master = bitcoin.bip32.fromBase58( - 'TDt9EWvD5T5T44hAZm3pGYyn3tEa9MtyDUJqTnRbSS4gGmK1RwjkJRNiAXDe29WNQTAYBBWKb6ZK6Wf186SsgNyJ4BgxkEzgLAZbGXo8UF3toxj', - network); - -let child = master.derivePath("m/44'/119'/0'/0'/0'"); // 4pvrsoxZXjKaK93ikcR3qD58ixb7iX1XZQXUNoWyqyqtbbRMgW3y 2021-04-23T14:57:04Z label=Default # addr=SPEvfQMtAtdNFfJT79GfauzodjK5N5eH1r hdkeypath=m/44'/119'/0'/0'/0' - -const { address } = bitcoin.payments.p2pkh({ - pubkey: child.publicKey, - network, -}); - -console.log(address); -if(!child.isNeutered) { - console.log(c.toWIF()); +import bitcoinjs from 'bitcoinjs-lib'; + +/** + * Abstract Class DSW. + * + * @class DSW + */ +class DSW { + + constructor(network) { + if (this.constructor == DSW) { + throw new Error("Abstract classes can't be instantiated."); + } + this._network = network; + } + + checkAddress(address) { + try { + // check the base58 encoding and the checksum value + bitcoinjs.address.fromBase58Check(address); + // check the validity of the network parameters + bitcoinjs.address.toOutputScript(address, this._network); + return true; + } catch { + return false; + } + } +} + +/** + * Sapphire. + * + * @class Sapphire + * @extends {DSW} + */ +export class Sapphire extends DSW { + + constructor() { + super({ + messagePrefix: '\x18DarkNet Signed Message:\n', + bech32: 'bc', // we don't have it, however leave it like in Bitcoin + bip32: { + public: 0x022d2563, // boost::assign::list_of(0x02)(0x2D)(0x25)(0x63) + private: 0x0221312B, // boost::assign::list_of(0x02)(0x21)(0x31)(0x2B) + }, + pubKeyHash: 0x3F, // std::vector(1, 63) + scriptHash: 0x12, // std::vector(1, 18) + wif: 0x19, // std::vector(1, 25) + }); + } + +} + +/** + * Jackpot. + * + * @class Jackpot + * @extends {DSW} + */ + export class Jackpot extends DSW { + + constructor() { + super({ + messagePrefix: '\x18DarkNet Signed Message:\n', + bech32: 'bc', // we don't have it, however leave it like in Bitcoin + bip32: { + public: 0x022D2573, // boost::assign::list_of(0x02)(0x2D)(0x25)(0x73) + private: 0x0221312B, // boost::assign::list_of(0x02)(0x21)(0x31)(0x2B) + }, + pubKeyHash: 0x0F, // std::vector(1, 15) + scriptHash: 0x10, // std::vector(1, 16) + wif: 0x2B, // std::vector(1, 43) + }); + } } \ No newline at end of file diff --git a/package.json b/package.json index ca4bf47..79dffda 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,9 @@ { "name": "dswjs-lib", - "version": "1.0.0", + "version": "0.0.1", "description": "A javascript DSW library for node.js and browsers.", "main": "index.js", + "type": "module", "scripts": { "test": "test" }, diff --git a/sandbox.js b/sandbox.js new file mode 100644 index 0000000..5f70e22 --- /dev/null +++ b/sandbox.js @@ -0,0 +1,44 @@ +import bitcoinjs from 'bitcoinjs-lib'; +import { Sapphire, Jackpot } from './index.js' + +let network = { + messagePrefix: '\x18DarkNet Signed Message:\n', + bech32: 'bc', // we don't have it, however leave it like in Bitcoin + bip32: { + public: 0x022d2563, // boost::assign::list_of(0x02)(0x2D)(0x25)(0x63) + private: 0x0221312b, // boost::assign::list_of(0x02)(0x21)(0x31)(0x2B) + }, + pubKeyHash: 0x3F, // std::vector(1, 63) + scriptHash: 0x12, // std::vector(1, 18) + wif: 0x19, // std::vector(1, 25) +}; + +let master = bitcoinjs.bip32.fromBase58( + 'TDt9EWvD5T5T44hAZm3pGYyn3tEa9MtyDUJqTnRbSS4gGmK1RwjkJRNiAXDe29WNQTAYBBWKb6ZK6Wf186SsgNyJ4BgxkEzgLAZbGXo8UF3toxj', + network); + +let child = master.derivePath("m/44'/119'/0'/0'/0'"); // 4pvrsoxZXjKaK93ikcR3qD58ixb7iX1XZQXUNoWyqyqtbbRMgW3y 2021-04-23T14:57:04Z label=Default # addr=SPEvfQMtAtdNFfJT79GfauzodjK5N5eH1r hdkeypath=m/44'/119'/0'/0'/0' + +const { address } = bitcoinjs.payments.p2pkh({ + pubkey: child.publicKey, + network, +}); + +console.log(address); +if(!child.isNeutered) { + console.log(c.toWIF()); +} + +console.log(bitcoinjs.address.fromBase58Check('SPEvfQMtAtdNFfJT79GfauzodjK5N5eH1r')); + +const sapphire = new Sapphire(); + +console.log(sapphire.checkAddress('SPEvfQMtAtdNFfJT79GfauzodjK5N5eH1r')); // good address +console.log(sapphire.checkAddress('SPEvfQMtAtdNFdJT79GfauzodjK5N5eH1r')); // bad address +console.log(sapphire.checkAddress('bMcbjoJRwrim4bmEByXkpMD1VWmQMKtHPe')); // address from other chain + +const jackpot = new Jackpot(); + +console.log(jackpot.checkAddress('75gbzE5cjtCcEHREeNJh2oFHq9X77Kbkee')); // good address +console.log(jackpot.checkAddress('75gbzE5cjtCcEHrEeNJh2oFHq9X77Kbkee')); // bad address +console.log(jackpot.checkAddress('bMcbjoJRwrim4bmEByXkpMD1VWmQMKtHPe')); // address from other chain \ No newline at end of file