import * as assert from 'assert'; import BIP32Factory from 'bip32'; import * as ecc from 'tiny-secp256k1'; import * as bip39 from 'bip39'; import { describe, it } from 'mocha'; import * as bitcoin from '../..'; const bip32 = BIP32Factory(ecc); function getAddress(node: any, network ? : any): string { return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address!; } describe('bitcoinjs-lib (BIP32)', () => { it('can import a BIP32 testnet xpriv and export to WIF', () => { const xpriv = 'tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK'; const node = bip32.fromBase58(xpriv, bitcoin.networks.testnet); assert.strictEqual( node.toWIF(), 'cQfoY67cetFNunmBUX5wJiw3VNoYx3gG9U9CAofKE6BfiV1fSRw7', ); }); it('can export a BIP32 xpriv, then import it', () => { const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'; const seed = bip39.mnemonicToSeedSync(mnemonic); const node = bip32.fromSeed(seed); const strng = node.toBase58(); const restored = bip32.fromBase58(strng); assert.strictEqual(getAddress(node), getAddress(restored)); // same public key assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key }); it('can export a BIP32 xpub', () => { const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'; const seed = bip39.mnemonicToSeedSync(mnemonic); const node = bip32.fromSeed(seed); const strng = node.neutered().toBase58(); assert.strictEqual( strng, 'xpub661MyMwAqRbcGhVeaVfEBA25e3cP9DsJQZoE8iep5fZSxy3TnPBNBgWnMZx56oreNc48ZoTkQfatNJ9VWnQ7ZcLZcVStpaXLTeG8bGrzX3n', ); }); it('can create a BIP32, bitcoin, account 0, external address', () => { const path = "m/0'/0/0"; const root = bip32.fromSeed( Buffer.from( 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex', ), ); const child1 = root.derivePath(path); // option 2, manually const child1b = root.deriveHardened(0).derive(0).derive(0); assert.strictEqual( getAddress(child1), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7', ); assert.strictEqual( getAddress(child1b), '1JHyB1oPXufr4FXkfitsjgNB5yRY9jAaa7', ); }); it('can create a BIP44, bitcoin, account 0, external address', () => { const root = bip32.fromSeed( Buffer.from( 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', 'hex', ), ); const child1 = root.derivePath("m/44'/0'/0'/0/0"); // option 2, manually const child1b = root .deriveHardened(44) .deriveHardened(0) .deriveHardened(0) .derive(0) .derive(0); assert.strictEqual( getAddress(child1), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au', ); assert.strictEqual( getAddress(child1b), '12Tyvr1U8A3ped6zwMEU5M8cx3G38sP5Au', ); }); it('can create a BIP49, bitcoin testnet, account 0, external address', () => { const mnemonic = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; const seed = bip39.mnemonicToSeedSync(mnemonic); const root = bip32.fromSeed(seed); const path = "m/49'/1'/0'/0/0"; const child = root.derivePath(path); const { address } = bitcoin.payments.p2sh({ redeem: bitcoin.payments.p2wpkh({ pubkey: child.publicKey, network: bitcoin.networks.testnet, }), network: bitcoin.networks.testnet, }); assert.strictEqual(address, '2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2'); }); it('can use BIP39 to generate BIP32 addresses', () => { // var mnemonic = bip39.generateMnemonic() const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'; assert(bip39.validateMnemonic(mnemonic)); const seed = bip39.mnemonicToSeedSync(mnemonic); const root = bip32.fromSeed(seed); // receive addresses assert.strictEqual( getAddress(root.derivePath("m/0'/0/0")), '1AVQHbGuES57wD68AJi7Gcobc3RZrfYWTC', ); assert.strictEqual( getAddress(root.derivePath("m/0'/0/1")), '1Ad6nsmqDzbQo5a822C9bkvAfrYv9mc1JL', ); // change addresses assert.strictEqual( getAddress(root.derivePath("m/0'/1/0")), '1349KVc5NgedaK7DvuD4xDFxL86QN1Hvdn', ); assert.strictEqual( getAddress(root.derivePath("m/0'/1/1")), '1EAvj4edpsWcSer3duybAd4KiR4bCJW5J6', ); }); });
Write, Run & Share Ruby code online using OneCompiler's Ruby online compiler for free. It's one of the robust, feature-rich online compilers for Ruby language, running on the latest version 2.3.1. Getting started with the OneCompiler's Ruby compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Ruby
and start coding.
OneCompiler's Ruby online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Ruby program which takes name as input and prints hello message with your name.
name = gets.chomp
print "Hello #{name}.\n"
Ruby is a general purpose object oriented programming language developed by Yukihiro Matsumoto.
Data type | Description | Usage |
---|---|---|
Fixnum | Represents normal numbers | x = 10 |
Bignum | Represents big numbers | x =9999999999 |
Float | Represents decimal numbers | x = 3.14 |
Complex | Represents imaginary numbers | x = 1 + 2i |
Rational | Represents fractional numbers | x = 1/4 |
BigDecimal | Represents precision decimal numbers | x=1.0 |
Hash | Represents key value pairs | {"Website"=>"onecompiler","message" => "Happy learning"} |
In Ruby, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically and a prefix is needed to indicate the following variable types.
Variable type | Description |
---|---|
Local variables | Scope is limited to the block of the variable initialization. Variable name must start with either _ or lowercase letter. |
Class variables | Class variables belongs to whole class and can be accessible anywhere inside the class. Variable name must start with @@ . They must be initialized before use. |
Instance variables | Instance variables belongs to a instance of the class. They can be accessed from any instance of the class within a method. Variable name must start @ |
Global variables | Scope is global and hence they can be accessible anywhere in the program. Variable name must start with $ |
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression)
#code
end
if(conditional-expression)
#code if condition is true
else
#code if condition is false
end
if(condition-expression1)
#code if above condition is true
elsif(condition-expression2)
#code if above condition is true
elsif(condition-expression3)
#code if above condition is true
...
else
#code if all the conditions are false
end
Case is similar to Switch statement, where it is used to execute one set of a statement from multiple conditions.
case expression
[when expression [, expression ...] [then]
# code ]...
[else
# code ]
end
For loop is used to iterate a set of statements based on a condition.
for variable in expression do
# code
end
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition do
# code
end
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
loop do
#code
break if conditional-expression
end