psf-multisig-approval 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.editorconfig ADDED
@@ -0,0 +1,30 @@
1
+ # http://editorconfig.org
2
+
3
+ # A special property that should be specified at the top of the file outside of
4
+ # any sections. Set to true to stop .editor config file search on current file
5
+ root = true
6
+
7
+ [*]
8
+ # Indentation style
9
+ # Possible values - tab, space
10
+ indent_style = space
11
+
12
+ # Indentation size in single-spaced characters
13
+ # Possible values - an integer, tab
14
+ indent_size = 2
15
+
16
+ # Line ending file format
17
+ # Possible values - lf, crlf, cr
18
+ end_of_line = lf
19
+
20
+ # File character encoding
21
+ # Possible values - latin1, utf-8, utf-16be, utf-16le
22
+ charset = utf-8
23
+
24
+ # Denotes whether to trim whitespace at the end of lines
25
+ # Possible values - true, false
26
+ trim_trailing_whitespace = true
27
+
28
+ # Denotes whether file should end with a newline
29
+ # Possible values - true, false
30
+ insert_final_newline = true
package/.eslintrc.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "standard",
3
+ "env": {
4
+ "node": true,
5
+ "mocha": true
6
+ },
7
+ "parserOptions": {
8
+ "ecmaVersion": 8
9
+ }
10
+ }
package/.on-save.json ADDED
@@ -0,0 +1,8 @@
1
+ [
2
+ {
3
+ "srcDir": "",
4
+ "destDir": "",
5
+ "files": "**/*.js",
6
+ "command": "npm run lint"
7
+ }
8
+ ]
package/.travis.yml ADDED
@@ -0,0 +1,33 @@
1
+ # This is a node.js v8+ JavaScript project
2
+ language: node_js
3
+ node_js:
4
+ - "10"
5
+
6
+ # Build on Ubuntu Trusty (14.04)
7
+ # https://docs.travis-ci.com/user/reference/trusty/#javascript-and-nodejs-images
8
+ dist: xenial
9
+ sudo: required
10
+
11
+ # Use Docker
12
+ services:
13
+ - docker
14
+
15
+ before_install:
16
+ #- npm install -g mocha
17
+
18
+ # https://github.com/greenkeeperio/greenkeeper-lockfile/issues/156
19
+ #install: case $TRAVIS_BRANCH in greenkeeper*) npm i;; *) npm ci;; esac;
20
+ install:
21
+ - npm install
22
+
23
+ script: "npm run test"
24
+
25
+ # Send coverage data to Coveralls
26
+ after_success:
27
+ - npm run coverage
28
+
29
+ deploy:
30
+ provider: script
31
+ skip_cleanup: true
32
+ script:
33
+ - npx semantic-release
package/LICENSE.md ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2021 Chris Troutner <chris.troutner@gmail.com>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/PEDIGREE.md ADDED
@@ -0,0 +1,3 @@
1
+ # Pedigree
2
+
3
+ This repository was originally forked from [npm-lib-boilerplate](https://github.com/christroutner/npm-lib-boilerplate). This is a boilerplate used to start JavaScript libraries which are intended to be published to npm.
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # psf-multisig-approval
2
+
3
+ This is an npm library for node.js. It implements the [PS009 specification for multisignature approval](https://github.com/Permissionless-Software-Foundation/specifications/blob/master/ps009-multisig-approval.md).
4
+
5
+ # License
6
+ [MIT](LICENSE.md)
package/apidoc.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "sampleUrl": null
3
+ }
@@ -0,0 +1,23 @@
1
+ /*
2
+ An example app for creating a wallet using this library.
3
+ */
4
+
5
+ const BchWallet = require('../index')
6
+
7
+ async function createWallet () {
8
+ try {
9
+ // Instantiate the wallet library.
10
+ const bchWallet = new BchWallet()
11
+
12
+ // Wait for the wallet to be created.
13
+ await bchWallet.walletInfoPromise
14
+
15
+ // Print out the wallet information.
16
+ console.log(
17
+ `Wallet information: ${JSON.stringify(bchWallet.walletInfo, null, 2)}`
18
+ )
19
+ } catch (err) {
20
+ console.error('Error: ', err)
21
+ }
22
+ }
23
+ createWallet()
@@ -0,0 +1,26 @@
1
+ /*
2
+ An example for listing the tokens and token balances of the wallet. a
3
+ */
4
+
5
+ const SlpWallet = require('../index')
6
+
7
+ async function listTokens () {
8
+ try {
9
+ // Replace the values for the constants below to customize for your use.
10
+ const MNEMONIC =
11
+ 'essence appear intact casino neck scatter search post cube fit door margin'
12
+
13
+ // Instantiate the wallet library.
14
+ const slpWallet = new SlpWallet(MNEMONIC)
15
+
16
+ // Wait for the wallet to be created.
17
+ await slpWallet.walletInfoPromise
18
+
19
+ // Get the token summary
20
+ const tokenInfo = await slpWallet.listTokens()
21
+ console.log(`tokenInfo: ${JSON.stringify(tokenInfo, null, 2)}`)
22
+ } catch (err) {
23
+ console.error('Error: ', err)
24
+ }
25
+ }
26
+ listTokens()
@@ -0,0 +1,60 @@
1
+ /*
2
+ An example for sending BCH with this library.
3
+ */
4
+
5
+ const SlpWallet = require('../index')
6
+
7
+ async function sendBch () {
8
+ try {
9
+ // Replace the values for the constants below to customize for your use.
10
+ const MNEMONIC =
11
+ 'essence appear intact casino neck scatter search post cube fit door margin'
12
+ const RECIEVER = ''
13
+ const SATS_TO_SEND = 1000
14
+
15
+ // Instantiate the wallet library.
16
+ const slpWallet = new SlpWallet(MNEMONIC)
17
+
18
+ // Wait for the wallet to be created.
19
+ await slpWallet.walletInfoPromise
20
+
21
+ // Get the balance of the wallet.
22
+ const balance = await slpWallet.getBalance()
23
+ console.log(`balance: ${balance} satoshis`)
24
+
25
+ // Exit if the wallet has no balance.
26
+ if (balance === 0) {
27
+ console.log(
28
+ `The balance of your wallet is zero. Send BCH to ${
29
+ slpWallet.walletInfo.address
30
+ } to run this example.`
31
+ )
32
+ return
33
+ }
34
+
35
+ // Create the outputs array.
36
+ const outputs = []
37
+
38
+ // If reciever is not specified, send the funds back to the wallet.
39
+ if (RECIEVER === '') {
40
+ outputs.push({
41
+ address: slpWallet.walletInfo.address,
42
+ amountSat: SATS_TO_SEND
43
+ })
44
+ //
45
+ // Send the funds to the reciever.
46
+ } else {
47
+ outputs.push({
48
+ address: RECIEVER,
49
+ amountSat: SATS_TO_SEND
50
+ })
51
+ }
52
+
53
+ const txid = await slpWallet.send(outputs)
54
+
55
+ console.log(`Success! BCH sent with TXID: ${txid}`)
56
+ } catch (err) {
57
+ console.error('Error: ', err)
58
+ }
59
+ }
60
+ sendBch()
@@ -0,0 +1,66 @@
1
+ /*
2
+ An example for sending tokens with this library.
3
+ */
4
+
5
+ const SlpWallet = require('../index')
6
+
7
+ async function sendBch () {
8
+ try {
9
+ // Replace the values for the constants below to customize for your use.
10
+ const MNEMONIC =
11
+ 'essence appear intact casino neck scatter search post cube fit door margin'
12
+ const RECIEVER = ''
13
+ const TOKENID =
14
+ 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2'
15
+ const TOKENS_TO_SEND = 1
16
+
17
+ // Instantiate the wallet library.
18
+ const slpWallet = new SlpWallet(MNEMONIC)
19
+
20
+ // Wait for the wallet to be created.
21
+ await slpWallet.walletInfoPromise
22
+
23
+ // console.log('slpWallet.utxos.utxoStore: ', slpWallet.utxos.utxoStore)
24
+
25
+ // Get the balance of the wallet.
26
+ const balance = await slpWallet.getBalance()
27
+ console.log(`balance: ${balance} satoshis`)
28
+
29
+ // Exit if the wallet has no balance.
30
+ if (balance === 0) {
31
+ console.log(
32
+ `The balance of your wallet is zero. Send BCH to ${
33
+ slpWallet.walletInfo.address
34
+ } to run this example.`
35
+ )
36
+ return
37
+ }
38
+
39
+ // Create the outputs array.
40
+ let output = {}
41
+
42
+ // If reciever is not specified, send the funds back to the wallet.
43
+ if (RECIEVER === '') {
44
+ output = {
45
+ address: slpWallet.walletInfo.address,
46
+ tokenId: TOKENID,
47
+ qty: TOKENS_TO_SEND
48
+ }
49
+ //
50
+ // Send the funds to the reciever.
51
+ } else {
52
+ output = {
53
+ address: RECIEVER,
54
+ tokenId: TOKENID,
55
+ qty: TOKENS_TO_SEND
56
+ }
57
+ }
58
+
59
+ const txid = await slpWallet.sendTokens(output)
60
+
61
+ console.log(`Success! Tokens sent with TXID: ${txid}`)
62
+ } catch (err) {
63
+ console.error('Error: ', err)
64
+ }
65
+ }
66
+ sendBch()
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ /*
2
+ This library implement PS009 specification:
3
+ https://github.com/Permissionless-Software-Foundation/specifications/blob/master/ps009-multisig-approval.md
4
+ */
5
+
6
+ /* eslint-disable no-async-promise-executor */
7
+
8
+ 'use strict'
9
+
10
+ const BCHJS = require('@psf/bch-js')
11
+
12
+ const Util = require('./lib/util')
13
+ const util = new Util()
14
+
15
+ let _this // local global for 'this'.
16
+
17
+ class BoilplateLib {
18
+ constructor () {
19
+ _this = this
20
+
21
+ _this.bchjs = new BCHJS()
22
+ _this.util = util
23
+ }
24
+ }
25
+
26
+ module.exports = BoilplateLib
package/lib/util.js ADDED
@@ -0,0 +1,50 @@
1
+ /*
2
+ An example of a typical utility library. Things to notice:
3
+ - This library is exported as a Class.
4
+ - External dependencies are embedded into the class 'this' object: this.bitbox
5
+ - `_this` maintains top-level context for `this`.
6
+ */
7
+
8
+ 'use strict'
9
+
10
+ // npm libraries
11
+ const BCHJS = require('@psf/bch-js')
12
+
13
+ // Locally global variables.
14
+ let _this
15
+ const bchjs = new BCHJS()
16
+
17
+ class UtilLib {
18
+ constructor () {
19
+ // Embed external libraries into the class, for easy mocking.
20
+ this.bchjs = bchjs
21
+
22
+ _this = this
23
+ }
24
+
25
+ async getBchData (addr) {
26
+ try {
27
+ // Validate Input
28
+ if (typeof addr !== 'string') throw new Error('Address must be a string')
29
+
30
+ const balance = await _this.bchjs.Electrumx.balance(addr)
31
+
32
+ const utxos = await _this.bchjs.Electrumx.utxo(addr)
33
+
34
+ const bchData = {
35
+ balance: balance.balance,
36
+ utxos: utxos.utxos
37
+ }
38
+ // console.log(`bchData: ${JSON.stringify(bchData, null, 2)}`)
39
+
40
+ return bchData
41
+ } catch (err) {
42
+ // Optional log to indicate the source of the error. This would normally
43
+ // be written with a logging app like Winston.
44
+ console.log('Error in util.js/getBalance()')
45
+ throw err
46
+ }
47
+ }
48
+ }
49
+
50
+ module.exports = UtilLib
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "psf-multisig-approval",
3
+ "version": "1.0.3",
4
+ "description": "Utility library implementing the PS009 specification",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "node index.js",
8
+ "test": "npm run lint && TEST=unit nyc mocha test/unit/",
9
+ "test:integration": "mocha --timeout 25000 test/integration/",
10
+ "lint": "standard --env mocha --fix",
11
+ "docs": "./node_modules/.bin/apidoc -i src/ -o docs",
12
+ "coverage:report": "nyc --reporter=html mocha test/unit/ --exit"
13
+ },
14
+ "keywords": [
15
+ "bitcoin",
16
+ "bitcoin cash",
17
+ "wallet",
18
+ "javascript",
19
+ "cryptocurrency",
20
+ "react",
21
+ "front end",
22
+ "client",
23
+ "apidoc",
24
+ "slp",
25
+ "tokens"
26
+ ],
27
+ "author": "Chris Troutner <chris.troutner@gmail.com>",
28
+ "license": "MIT",
29
+ "apidoc": {
30
+ "title": "@psf/multisig-approval",
31
+ "url": "localhost:5000"
32
+ },
33
+ "repository": "Permissionless-Software-Foundation/psf-multisig-approval",
34
+ "dependencies": {
35
+ "@psf/bch-js": "6.7.1"
36
+ },
37
+ "devDependencies": {
38
+ "apidoc": "0.54.0",
39
+ "chai": "4.3.7",
40
+ "husky": "8.0.3",
41
+ "lodash.clonedeep": "4.5.0",
42
+ "mocha": "10.2.0",
43
+ "nyc": "15.1.0",
44
+ "semantic-release": "19.0.5",
45
+ "sinon": "15.0.3",
46
+ "standard": "17.0.0"
47
+ },
48
+ "release": {
49
+ "publish": [
50
+ {
51
+ "path": "@semantic-release/npm",
52
+ "npmPublish": true
53
+ }
54
+ ]
55
+ },
56
+ "husky": {
57
+ "hooks": {
58
+ "pre-commit": "npm run lint"
59
+ }
60
+ }
61
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ Integration tests for the util.js utility library.
3
+ */
4
+
5
+ // npm libraries
6
+ const chai = require('chai')
7
+
8
+ // Locally global variables.
9
+ const assert = chai.assert
10
+
11
+ // Unit under test
12
+ const UtilLib = require('../../lib/util')
13
+ const uut = new UtilLib()
14
+
15
+ describe('#util.js', () => {
16
+ describe('#getBchData', () => {
17
+ it('should get BCH data on an address', async () => {
18
+ const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
19
+
20
+ const bchData = await uut.getBchData(addr)
21
+
22
+ // Assert that top-level properties exist.
23
+ assert.property(bchData, 'balance')
24
+ assert.property(bchData, 'utxos')
25
+
26
+ // Assert essential UTXOs properties exist.
27
+ assert.isArray(bchData.utxos)
28
+ assert.property(bchData.utxos[0], 'tx_pos')
29
+ assert.property(bchData.utxos[0], 'tx_hash')
30
+ assert.property(bchData.utxos[0], 'value')
31
+ })
32
+ })
33
+ })
@@ -0,0 +1,32 @@
1
+ /*
2
+ A mocking library for util.js unit tests.
3
+ A mocking library contains data to use in place of the data that would come
4
+ from an external dependency.
5
+ */
6
+
7
+ 'use strict'
8
+
9
+ const mockBalance = {
10
+ success: true,
11
+ balance: {
12
+ confirmed: 1000,
13
+ unconfirmed: 0
14
+ }
15
+ }
16
+
17
+ const mockUtxos = {
18
+ success: true,
19
+ utxos: [
20
+ {
21
+ height: 601861,
22
+ tx_hash: '6181c669614fa18039a19b23eb06806bfece1f7514ab457c3bb82a40fe171a6d',
23
+ tx_pos: 0,
24
+ value: 1000
25
+ }
26
+ ]
27
+ }
28
+
29
+ module.exports = {
30
+ mockBalance,
31
+ mockUtxos
32
+ }
@@ -0,0 +1,70 @@
1
+ /*
2
+ Unit tests for the util.js utility library.
3
+ */
4
+
5
+ // npm libraries
6
+ const chai = require('chai')
7
+ const sinon = require('sinon')
8
+ const cloneDeep = require('lodash.clonedeep')
9
+
10
+ // Locally global variables.
11
+ const assert = chai.assert
12
+
13
+ // Mocking data libraries.
14
+ const mockDataLib = require('./mocks/util-mocks')
15
+
16
+ // Unit under test
17
+ const UtilLib = require('../../lib/util')
18
+ const uut = new UtilLib()
19
+
20
+ describe('#util.js', () => {
21
+ let sandbox
22
+ let mockData
23
+
24
+ beforeEach(() => {
25
+ // Restore the sandbox before each test.
26
+ sandbox = sinon.createSandbox()
27
+
28
+ // Clone the mock data.
29
+ mockData = cloneDeep(mockDataLib)
30
+ })
31
+
32
+ afterEach(() => sandbox.restore())
33
+
34
+ describe('#getBchData', () => {
35
+ it('should throw error if address is not a string', async () => {
36
+ try {
37
+ const addr = 1234
38
+
39
+ await uut.getBchData(addr)
40
+
41
+ assert.equal(true, false, 'unexpected result')
42
+ } catch (err) {
43
+ assert.include(err.message, 'Address must be a string')
44
+ }
45
+ })
46
+
47
+ it('should get BCH data on an address', async () => {
48
+ // Mock external dependencies.
49
+ sandbox
50
+ .stub(uut.bchjs.Electrumx, 'balance')
51
+ .resolves(mockData.mockBalance)
52
+ sandbox.stub(uut.bchjs.Electrumx, 'utxo').resolves(mockData.mockUtxos)
53
+
54
+ const addr = 'bitcoincash:qp3sn6vlwz28ntmf3wmyra7jqttfx7z6zgtkygjhc7'
55
+
56
+ const bchData = await uut.getBchData(addr)
57
+ // console.log(`bchData: ${JSON.stringify(bchData, null, 2)}`)
58
+
59
+ // Assert that top-level properties exist.
60
+ assert.property(bchData, 'balance')
61
+ assert.property(bchData, 'utxos')
62
+
63
+ // Assert essential UTXOs properties exist.
64
+ assert.isArray(bchData.utxos)
65
+ assert.property(bchData.utxos[0], 'tx_pos')
66
+ assert.property(bchData.utxos[0], 'tx_hash')
67
+ assert.property(bchData.utxos[0], 'value')
68
+ })
69
+ })
70
+ })