UNPKG

flocore-lib

Version:

A pure and powerful JavaScript Florincoin library.

149 lines (137 loc) 5.92 kB
'use strict'; /* jshint unused: false */ var should = require('chai').should(); var expect = require('chai').expect; var _ = require('lodash'); var flocore = require('../../..'); var Transaction = flocore.Transaction; var PrivateKey = flocore.PrivateKey; var Address = flocore.Address; var Script = flocore.Script; var Signature = flocore.crypto.Signature; var MultiSigScriptHashInput = flocore.Transaction.Input.MultiSigScriptHash; describe('MultiSigScriptHashInput', function() { var privateKey1 = new PrivateKey('cNSvQov1Qrh2sqGaCRWzs1ppPKKEDfKQbS95E7Y4Da7c4fuSx92R'); var privateKey2 = new PrivateKey('cSCT2jteFyKSJiSpCmEtCz88zkCmxvyjzF8wDkCDiBak2fhULoMo'); var privateKey3 = new PrivateKey('cTKLF5W5SLaGTTMs7bWQ73nR4Zs118W1HZA57CWzW16A7KPqoZTv'); var public1 = privateKey1.publicKey; var public2 = privateKey2.publicKey; var public3 = privateKey3.publicKey; // var address = new Address([public1, public2, public3], 2); var address = new Address("QeJdou13jd18ghcr3F2ktDff1nvbjJnGrS"); var output = { address: address, txId: '0fa147b287dacf753fd5f0e9aaf342464555b78960352ec043b9f7289e82e60f', outputIndex: 0, script: new Script(address), satoshis: 1000000 }; it('can count missing signatures', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000); var input = transaction.inputs[0]; input.countSignatures().should.equal(0); transaction.sign(privateKey1); input.countSignatures().should.equal(1); input.countMissingSignatures().should.equal(1); input.isFullySigned().should.equal(false); transaction.sign(privateKey2); input.countSignatures().should.equal(2); input.countMissingSignatures().should.equal(0); input.isFullySigned().should.equal(true); }); it('returns a list of public keys with missing signatures', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000); var input = transaction.inputs[0]; _.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) { var serialized = publicKeyMissing.toString(); return serialized === public1.toString() || serialized === public2.toString() || serialized === public3.toString(); }).should.equal(true); transaction.sign(privateKey1); _.every(input.publicKeysWithoutSignature(), function(publicKeyMissing) { var serialized = publicKeyMissing.toString(); return serialized === public2.toString() || serialized === public3.toString(); }).should.equal(true); }); it('can clear all signatures', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000) .sign(privateKey1) .sign(privateKey2); var input = transaction.inputs[0]; input.isFullySigned().should.equal(true); input.clearSignatures(); input.isFullySigned().should.equal(false); }); it('can estimate how heavy is the output going to be', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000); var input = transaction.inputs[0]; input._estimateSize().should.equal(257); }); it('uses SIGHASH_ALL by default', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000); var input = transaction.inputs[0]; var sigs = input.getSignatures(transaction, privateKey1, 0); sigs[0].sigtype.should.equal(Signature.SIGHASH_ALL); }); it('roundtrips to/from object', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000) .sign(privateKey1); var input = transaction.inputs[0]; var roundtrip = new MultiSigScriptHashInput(input.toObject()); roundtrip.toObject().should.deep.equal(input.toObject()); }); it('roundtrips to/from object when not signed', function() { var transaction = new Transaction() .from(output, [public1, public2, public3], 2) .to(address, 1000000); var input = transaction.inputs[0]; var roundtrip = new MultiSigScriptHashInput(input.toObject()); roundtrip.toObject().should.deep.equal(input.toObject()); }); it('will get the scriptCode for nested witness', function() { var address = Address.createMultisig([public1, public2, public3], 2, 'testnet', true); var utxo = { address: address.toString(), txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140', outputIndex: 0, script: new Script(address), satoshis: 1000000 }; var transaction = new Transaction() .from(utxo, [public1, public2, public3], 2, true) .to(address, 1000000); var input = transaction.inputs[0]; var scriptCode = input.getScriptCode(); scriptCode.toString('hex').should.equal('6952210235f6364aca36f01702b8dab94060acf431feaf94e24668c350c747c24ea57d1421025d5fdce77bdbff09d3139d5716bfb4d1a269cf642c34b2049f6c5558b59006e4210288b7a3ce0e26fd4e98c2befc2df440ea733d848491fd1f11a4b3b0722d7f29a853ae'); }); it('will get the satoshis buffer for nested witness', function() { var address = Address.createMultisig([public1, public2, public3], 2, 'testnet', true); var utxo = { address: address.toString(), txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140', outputIndex: 0, script: new Script(address), satoshis: 1000000 }; var transaction = new Transaction() .from(utxo, [public1, public2, public3], 2, true) .to(address, 1000000); var input = transaction.inputs[0]; var satoshisBuffer = input.getSatoshisBuffer(); satoshisBuffer.toString('hex').should.equal('40420f0000000000'); }); });