diff --git a/package-lock.json b/package-lock.json index 33aa0c0..6007d75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,13 +9,13 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "@psf/bch-js": "5.3.2", + "@psf/bch-js": "../bch-js", "axios": "0.21.1", "bch-message-lib": "2.1.4", "bcryptjs": "2.4.3", "glob": "7.1.6", "ipfs": "0.60.0", - "ipfs-coord": "7.1.4", + "ipfs-coord": "7.1.6", "ipfs-http-client": "55.0.0", "jsonrpc-lite": "2.2.0", "jsonwebtoken": "8.5.1", @@ -33,7 +33,7 @@ "koa2-ratelimit": "0.9.1", "libp2p": "^0.36.2", "line-reader": "0.4.0", - "minimal-slp-wallet": "4.4.2", + "minimal-slp-wallet": "../minimal-slp-wallet", "mongoose": "5.13.13", "node-fetch": "npm:@achingbrain/node-fetch@2.6.7", "nodemailer": "6.4.17", @@ -67,7 +67,6 @@ "../bch-js": { "name": "@psf/bch-js", "version": "5.0.0", - "extraneous": true, "license": "MIT", "dependencies": { "@chris.troutner/bip32-utils": "1.0.5", @@ -120,7 +119,6 @@ }, "../minimal-slp-wallet": { "version": "4.0.0", - "extraneous": true, "license": "MIT", "dependencies": { "@psf/bch-js": "5.3.2", @@ -1605,62 +1603,8 @@ "license": "BSD-3-Clause" }, "node_modules/@psf/bch-js": { - "version": "5.3.2", - "resolved": "http://94.130.170.209:4873/@psf%2fbch-js/-/bch-js-5.3.2.tgz", - "integrity": "sha512-LzYekwuee+T8AEZ1MKBe5KnZKXzpRZ57SpXRL2S8qsHefokTIN+XnDPX7dsOFkN4VGGgyjYyZ33iN6xhOSHWJw==", - "license": "MIT", - "dependencies": { - "@chris.troutner/bip32-utils": "1.0.5", - "@psf/bip21": "2.0.1", - "@psf/bitcoincash-ops": "2.0.0", - "@psf/bitcoincashjs-lib": "4.0.2", - "@psf/coininfo": "4.0.0", - "@uppy/core": "1.10.4", - "@uppy/tus": "1.5.12", - "axios": "^0.21.4", - "bc-bip68": "1.0.5", - "bchaddrjs-slp": "0.2.5", - "bigi": "1.4.2", - "bignumber.js": "9.0.0", - "bip-schnorr": "0.3.0", - "bip38": "2.0.2", - "bip39": "3.0.2", - "bip66": "1.1.5", - "bitcoinjs-message": "2.0.0", - "bs58": "4.0.1", - "ecashaddrjs": "1.0.7", - "ini": "1.3.8", - "randombytes": "2.0.6", - "safe-buffer": "5.1.2", - "satoshi-bitcoin": "1.0.4", - "slp-mdm": "0.0.6", - "slp-parser": "0.0.4", - "wif": "2.0.6" - } - }, - "node_modules/@psf/bch-js/node_modules/axios": { - "version": "0.21.4", - "resolved": "http://94.130.170.209:4873/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "license": "MIT", - "dependencies": { - "follow-redirects": "^1.14.0" - } - }, - "node_modules/@psf/bch-js/node_modules/randombytes": { - "version": "2.0.6", - "resolved": "http://94.130.170.209:4873/randombytes/-/randombytes-2.0.6.tgz", - "integrity": "sha512-CIQ5OFxf4Jou6uOKe9t1AOgqpeU5fd70A8NPdHSGeYXqXsPe6peOwI0cUl88RWZ6sP1vPMV3avd/R6cZ5/sP1A==", - "license": "MIT", - "dependencies": { - "safe-buffer": "^5.1.0" - } - }, - "node_modules/@psf/bch-js/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "http://94.130.170.209:4873/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "license": "MIT" + "resolved": "../bch-js", + "link": true }, "node_modules/@psf/bip21": { "version": "2.0.1", @@ -8423,9 +8367,10 @@ } }, "node_modules/ipfs-coord": { - "version": "7.1.4", - "resolved": "https://registry.npmjs.org/ipfs-coord/-/ipfs-coord-7.1.4.tgz", - "integrity": "sha512-YsCdrQ30JW03uOLNrpZKqNmWr5zmxF9AyKeXSQA/OTPBoOOqM2nomEYhTPVJjGxee9Qa2dtxCQzw3JwsLDCe4Q==", + "version": "7.1.6", + "resolved": "http://94.130.170.209:4873/ipfs-coord/-/ipfs-coord-7.1.6.tgz", + "integrity": "sha512-wtdJ/PgRMv3r2pOZ1WIc7blTkk3YftMbXTNX9hN9Lu+IblUhNoWUyjq7E4i7J3zBZQVQs7J3TyckRdLCekmc9A==", + "license": "MIT", "dependencies": { "axios": "0.21.4", "bch-encrypt-lib": "2.0.0", @@ -13024,39 +12969,8 @@ } }, "node_modules/minimal-slp-wallet": { - "version": "4.4.2", - "resolved": "http://94.130.170.209:4873/minimal-slp-wallet/-/minimal-slp-wallet-4.4.2.tgz", - "integrity": "sha512-6JNRnH8mQQb/gmfwdd0KgJeHFJycK60EDmeEdjghvm1I2J0z2HVI3gZzrVqjBC/tBDYKtuDuvRtix7q2LqKqhw==", - "license": "MIT", - "dependencies": { - "@psf/bch-js": "5.3.2", - "apidoc": "0.25.0", - "bch-consumer": "1.2.0", - "bch-donation": "1.1.1", - "crypto-js": "4.0.0" - } - }, - "node_modules/minimal-slp-wallet/node_modules/apidoc": { - "version": "0.25.0", - "resolved": "http://94.130.170.209:4873/apidoc/-/apidoc-0.25.0.tgz", - "integrity": "sha512-5g9fp8OffXZOdBTzm4BBvV5Vw54s+NmKnGZIUKuH+gRTqqJuRJpcGN6sz6WnjJ+NcvXhB7rIRp6FhtJahazx2Q==", - "license": "MIT", - "dependencies": { - "apidoc-core": "^0.12.0", - "commander": "^2.20.0", - "fs-extra": "^9.0.1", - "handlebars": "^4.7.6", - "lodash": "^4.17.20", - "markdown-it": "^11.0.0", - "nodemon": "^2.0.4", - "winston": "^3.3.3" - }, - "bin": { - "apidoc": "bin/apidoc" - }, - "engines": { - "node": ">= 0.10.0" - } + "resolved": "../minimal-slp-wallet", + "link": true }, "node_modules/minimalistic-assert": { "version": "1.0.1", @@ -17935,6 +17849,49 @@ "minimal-slp-wallet": "4.4.2" } }, + "node_modules/p2wdb/node_modules/@psf/bch-js": { + "version": "5.3.2", + "resolved": "http://94.130.170.209:4873/@psf%2fbch-js/-/bch-js-5.3.2.tgz", + "integrity": "sha512-LzYekwuee+T8AEZ1MKBe5KnZKXzpRZ57SpXRL2S8qsHefokTIN+XnDPX7dsOFkN4VGGgyjYyZ33iN6xhOSHWJw==", + "license": "MIT", + "dependencies": { + "@chris.troutner/bip32-utils": "1.0.5", + "@psf/bip21": "2.0.1", + "@psf/bitcoincash-ops": "2.0.0", + "@psf/bitcoincashjs-lib": "4.0.2", + "@psf/coininfo": "4.0.0", + "@uppy/core": "1.10.4", + "@uppy/tus": "1.5.12", + "axios": "^0.21.4", + "bc-bip68": "1.0.5", + "bchaddrjs-slp": "0.2.5", + "bigi": "1.4.2", + "bignumber.js": "9.0.0", + "bip-schnorr": "0.3.0", + "bip38": "2.0.2", + "bip39": "3.0.2", + "bip66": "1.1.5", + "bitcoinjs-message": "2.0.0", + "bs58": "4.0.1", + "ecashaddrjs": "1.0.7", + "ini": "1.3.8", + "randombytes": "2.0.6", + "safe-buffer": "5.1.2", + "satoshi-bitcoin": "1.0.4", + "slp-mdm": "0.0.6", + "slp-parser": "0.0.4", + "wif": "2.0.6" + } + }, + "node_modules/p2wdb/node_modules/@psf/bch-js/node_modules/axios": { + "version": "0.21.4", + "resolved": "http://94.130.170.209:4873/axios/-/axios-0.21.4.tgz", + "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.14.0" + } + }, "node_modules/p2wdb/node_modules/apidoc": { "version": "0.25.0", "resolved": "http://94.130.170.209:4873/apidoc/-/apidoc-0.25.0.tgz", @@ -17966,6 +17923,34 @@ "follow-redirects": "^1.14.4" } }, + "node_modules/p2wdb/node_modules/minimal-slp-wallet": { + "version": "4.4.2", + "resolved": "http://94.130.170.209:4873/minimal-slp-wallet/-/minimal-slp-wallet-4.4.2.tgz", + "integrity": "sha512-6JNRnH8mQQb/gmfwdd0KgJeHFJycK60EDmeEdjghvm1I2J0z2HVI3gZzrVqjBC/tBDYKtuDuvRtix7q2LqKqhw==", + "license": "MIT", + "dependencies": { + "@psf/bch-js": "5.3.2", + "apidoc": "0.25.0", + "bch-consumer": "1.2.0", + "bch-donation": "1.1.1", + "crypto-js": "4.0.0" + } + }, + "node_modules/p2wdb/node_modules/randombytes": { + "version": "2.0.6", + "resolved": "http://94.130.170.209:4873/randombytes/-/randombytes-2.0.6.tgz", + "integrity": "sha512-CIQ5OFxf4Jou6uOKe9t1AOgqpeU5fd70A8NPdHSGeYXqXsPe6peOwI0cUl88RWZ6sP1vPMV3avd/R6cZ5/sP1A==", + "license": "MIT", + "dependencies": { + "safe-buffer": "^5.1.0" + } + }, + "node_modules/p2wdb/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "http://94.130.170.209:4873/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "license": "MIT" + }, "node_modules/package-hash": { "version": "4.0.0", "dev": true, @@ -23333,9 +23318,7 @@ "version": "1.1.0" }, "@psf/bch-js": { - "version": "5.3.2", - "resolved": "http://94.130.170.209:4873/@psf%2fbch-js/-/bch-js-5.3.2.tgz", - "integrity": "sha512-LzYekwuee+T8AEZ1MKBe5KnZKXzpRZ57SpXRL2S8qsHefokTIN+XnDPX7dsOFkN4VGGgyjYyZ33iN6xhOSHWJw==", + "version": "file:../bch-js", "requires": { "@chris.troutner/bip32-utils": "1.0.5", "@psf/bip21": "2.0.1", @@ -23344,6 +23327,8 @@ "@psf/coininfo": "4.0.0", "@uppy/core": "1.10.4", "@uppy/tus": "1.5.12", + "apidoc": "0.17.7", + "assert": "2.0.0", "axios": "^0.21.4", "bc-bip68": "1.0.5", "bchaddrjs-slp": "0.2.5", @@ -23355,37 +23340,30 @@ "bip66": "1.1.5", "bitcoinjs-message": "2.0.0", "bs58": "4.0.1", + "chai": "4.1.2", + "coveralls": "3.0.2", "ecashaddrjs": "1.0.7", + "eslint": "7.17.0", + "eslint-config-prettier": "7.1.0", + "eslint-config-standard": "16.0.3", + "eslint-plugin-node": "11.1.0", + "eslint-plugin-prettier": "3.3.1", + "eslint-plugin-standard": "4.0.0", + "husky": "^4.3.8", "ini": "1.3.8", + "lodash.clonedeep": "4.5.0", + "mocha": "9.1.3", + "node-mocks-http": "1.7.0", + "nyc": "15.1.0", "randombytes": "2.0.6", "safe-buffer": "5.1.2", "satoshi-bitcoin": "1.0.4", + "semantic-release": "18.0.0", + "sinon": "9.2.2", "slp-mdm": "0.0.6", "slp-parser": "0.0.4", + "standard": "^16.0.4", "wif": "2.0.6" - }, - "dependencies": { - "axios": { - "version": "0.21.4", - "resolved": "http://94.130.170.209:4873/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "requires": { - "follow-redirects": "^1.14.0" - } - }, - "randombytes": { - "version": "2.0.6", - "resolved": "http://94.130.170.209:4873/randombytes/-/randombytes-2.0.6.tgz", - "integrity": "sha512-CIQ5OFxf4Jou6uOKe9t1AOgqpeU5fd70A8NPdHSGeYXqXsPe6peOwI0cUl88RWZ6sP1vPMV3avd/R6cZ5/sP1A==", - "requires": { - "safe-buffer": "^5.1.0" - } - }, - "safe-buffer": { - "version": "5.1.2", - "resolved": "http://94.130.170.209:4873/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - } } }, "@psf/bip21": { @@ -28099,9 +28077,9 @@ } }, "ipfs-coord": { - "version": "7.1.4", - "resolved": "https://registry.npmjs.org/ipfs-coord/-/ipfs-coord-7.1.4.tgz", - "integrity": "sha512-YsCdrQ30JW03uOLNrpZKqNmWr5zmxF9AyKeXSQA/OTPBoOOqM2nomEYhTPVJjGxee9Qa2dtxCQzw3JwsLDCe4Q==", + "version": "7.1.6", + "resolved": "http://94.130.170.209:4873/ipfs-coord/-/ipfs-coord-7.1.6.tgz", + "integrity": "sha512-wtdJ/PgRMv3r2pOZ1WIc7blTkk3YftMbXTNX9hN9Lu+IblUhNoWUyjq7E4i7J3zBZQVQs7J3TyckRdLCekmc9A==", "requires": { "axios": "0.21.4", "bch-encrypt-lib": "2.0.0", @@ -31617,32 +31595,30 @@ "dev": true }, "minimal-slp-wallet": { - "version": "4.4.2", - "resolved": "http://94.130.170.209:4873/minimal-slp-wallet/-/minimal-slp-wallet-4.4.2.tgz", - "integrity": "sha512-6JNRnH8mQQb/gmfwdd0KgJeHFJycK60EDmeEdjghvm1I2J0z2HVI3gZzrVqjBC/tBDYKtuDuvRtix7q2LqKqhw==", + "version": "file:../minimal-slp-wallet", "requires": { "@psf/bch-js": "5.3.2", "apidoc": "0.25.0", "bch-consumer": "1.2.0", "bch-donation": "1.1.1", - "crypto-js": "4.0.0" - }, - "dependencies": { - "apidoc": { - "version": "0.25.0", - "resolved": "http://94.130.170.209:4873/apidoc/-/apidoc-0.25.0.tgz", - "integrity": "sha512-5g9fp8OffXZOdBTzm4BBvV5Vw54s+NmKnGZIUKuH+gRTqqJuRJpcGN6sz6WnjJ+NcvXhB7rIRp6FhtJahazx2Q==", - "requires": { - "apidoc-core": "^0.12.0", - "commander": "^2.20.0", - "fs-extra": "^9.0.1", - "handlebars": "^4.7.6", - "lodash": "^4.17.20", - "markdown-it": "^11.0.0", - "nodemon": "^2.0.4", - "winston": "^3.3.3" - } - } + "browserify": "17.0.0", + "chai": "4.2.0", + "coveralls": "3.1.0", + "crypto-js": "4.0.0", + "eslint": "7.17.0", + "eslint-config-prettier": "7.1.0", + "eslint-config-standard": "16.0.2", + "eslint-plugin-node": "11.1.0", + "eslint-plugin-prettier": "3.3.1", + "eslint-plugin-standard": "4.0.1", + "husky": "4.3.8", + "lodash.clonedeep": "4.5.0", + "mocha": "8.2.0", + "nyc": "15.1.0", + "semantic-release": "17.4.4", + "sinon": "9.2.0", + "standard": "16.0.4", + "tinyify": "3.0.0" } }, "minimalistic-assert": { @@ -34980,6 +34956,49 @@ "minimal-slp-wallet": "4.4.2" }, "dependencies": { + "@psf/bch-js": { + "version": "5.3.2", + "resolved": "http://94.130.170.209:4873/@psf%2fbch-js/-/bch-js-5.3.2.tgz", + "integrity": "sha512-LzYekwuee+T8AEZ1MKBe5KnZKXzpRZ57SpXRL2S8qsHefokTIN+XnDPX7dsOFkN4VGGgyjYyZ33iN6xhOSHWJw==", + "requires": { + "@chris.troutner/bip32-utils": "1.0.5", + "@psf/bip21": "2.0.1", + "@psf/bitcoincash-ops": "2.0.0", + "@psf/bitcoincashjs-lib": "4.0.2", + "@psf/coininfo": "4.0.0", + "@uppy/core": "1.10.4", + "@uppy/tus": "1.5.12", + "axios": "^0.21.4", + "bc-bip68": "1.0.5", + "bchaddrjs-slp": "0.2.5", + "bigi": "1.4.2", + "bignumber.js": "9.0.0", + "bip-schnorr": "0.3.0", + "bip38": "2.0.2", + "bip39": "3.0.2", + "bip66": "1.1.5", + "bitcoinjs-message": "2.0.0", + "bs58": "4.0.1", + "ecashaddrjs": "1.0.7", + "ini": "1.3.8", + "randombytes": "2.0.6", + "safe-buffer": "5.1.2", + "satoshi-bitcoin": "1.0.4", + "slp-mdm": "0.0.6", + "slp-parser": "0.0.4", + "wif": "2.0.6" + }, + "dependencies": { + "axios": { + "version": "0.21.4", + "resolved": "http://94.130.170.209:4873/axios/-/axios-0.21.4.tgz", + "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", + "requires": { + "follow-redirects": "^1.14.0" + } + } + } + }, "apidoc": { "version": "0.25.0", "resolved": "http://94.130.170.209:4873/apidoc/-/apidoc-0.25.0.tgz", @@ -35002,6 +35021,31 @@ "requires": { "follow-redirects": "^1.14.4" } + }, + "minimal-slp-wallet": { + "version": "4.4.2", + "resolved": "http://94.130.170.209:4873/minimal-slp-wallet/-/minimal-slp-wallet-4.4.2.tgz", + "integrity": "sha512-6JNRnH8mQQb/gmfwdd0KgJeHFJycK60EDmeEdjghvm1I2J0z2HVI3gZzrVqjBC/tBDYKtuDuvRtix7q2LqKqhw==", + "requires": { + "@psf/bch-js": "5.3.2", + "apidoc": "0.25.0", + "bch-consumer": "1.2.0", + "bch-donation": "1.1.1", + "crypto-js": "4.0.0" + } + }, + "randombytes": { + "version": "2.0.6", + "resolved": "http://94.130.170.209:4873/randombytes/-/randombytes-2.0.6.tgz", + "integrity": "sha512-CIQ5OFxf4Jou6uOKe9t1AOgqpeU5fd70A8NPdHSGeYXqXsPe6peOwI0cUl88RWZ6sP1vPMV3avd/R6cZ5/sP1A==", + "requires": { + "safe-buffer": "^5.1.0" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "http://94.130.170.209:4873/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" } } }, diff --git a/package.json b/package.json index 6dfb5fd..41a5d37 100644 --- a/package.json +++ b/package.json @@ -27,13 +27,13 @@ }, "repository": "Permissionless-Software-Foundation/bch-dex", "dependencies": { - "@psf/bch-js": "5.3.2", + "@psf/bch-js": "../bch-js", "axios": "0.21.1", "bch-message-lib": "2.1.4", "bcryptjs": "2.4.3", "glob": "7.1.6", "ipfs": "0.60.0", - "ipfs-coord": "7.1.4", + "ipfs-coord": "7.1.6", "ipfs-http-client": "55.0.0", "jsonrpc-lite": "2.2.0", "jsonwebtoken": "8.5.1", @@ -51,7 +51,7 @@ "koa2-ratelimit": "0.9.1", "libp2p": "^0.36.2", "line-reader": "0.4.0", - "minimal-slp-wallet": "4.4.2", + "minimal-slp-wallet": "../minimal-slp-wallet", "mongoose": "5.13.13", "node-fetch": "npm:@achingbrain/node-fetch@2.6.7", "nodemailer": "6.4.17", diff --git a/src/adapters/wallet.js b/src/adapters/wallet.js index ff226b5..d1a775e 100644 --- a/src/adapters/wallet.js +++ b/src/adapters/wallet.js @@ -180,16 +180,128 @@ class WalletAdapter { // Generate a partial transcation to *take* a 'sell' order. async generatePartialTx (orderInfo) { - // try { - // // const bchjs = this.bchWallet.bchjs - // - // return true - // } catch (err) { - // console.error('Error in wallet.js/generatePartialTx()') - // throw err - // } + try { + console.log(`orderInfo: ${JSON.stringify(orderInfo, null, 2)}`) - return true + const bchjs = this.bchWallet.bchjs + + // instance of transaction builder + const transactionBuilder = new bchjs.TransactionBuilder() + + // Get a payment UTXO. + // TODO: Create a segregated UTXO. + const bchUtxos = this.bchWallet.utxos.utxoStore.bchUtxos + const paymentUtxo = await bchjs.Utxo.findBiggestUtxo(bchUtxos) + console.log('paymentUtxo: ', paymentUtxo) + + // Get token info on the offered UTXO + const txData = await this.bchWallet.getTxData([orderInfo.utxoTxid]) + console.log(`txData: ${JSON.stringify(txData, null, 2)}`) + + // Construct the UTXO being offered for sale. + const offeredUtxo = { + txid: orderInfo.utxoTxid, + vout: orderInfo.utxoVout, + tokenId: orderInfo.tokenId, + decimals: txData[0].tokenDecimals, + tokenQty: orderInfo.numTokens.toString() + } + console.log(`offeredUtxo: ${JSON.stringify(offeredUtxo, null, 2)}`) + + // Build First part of the collaborative Tx a.k.a. Alice + // Generate the OP_RETURN code. + const slpSendObj = bchjs.SLP.TokenType1.generateSendOpReturn( + [offeredUtxo], + orderInfo.numTokens.toString() + ) + const slpData = slpSendObj.script + console.log(`slpOutputs: ${slpSendObj.outputs}`) + + // Currently this app only supports a single SLP token UTXO for exact + // token quantities (no token change). e.g. 1 UTXO representing the + // exact number of 'numTokens' + if (slpSendObj.outputs > 1) { + console.log('WARNING: choose one UTXO with all tokens to exchange') + return + } + + // Calculate sats needed to pay the offer. + const satsNeeded = orderInfo.numTokens * parseInt(orderInfo.rateInSats) + + // Calculate miner fees. + // Get byte count (minimum 2 inputs, 3 outputs) + const opReturnBufLength = slpData.byteLength + 32 // add padding + const byteCount = + bchjs.BitcoinCash.getByteCount({ P2PKH: 2 }, { P2PKH: 4 }) + + opReturnBufLength + const totalSatsNeeded = byteCount + satsNeeded + // console.log(`satoshis needed: ${satsNeeded}`) + + // One last check to ensure the app wallet has enough BCH to complete + // the trade. + if (totalSatsNeeded > paymentUtxo.value) { + console.log(`Selected payment UTXO is not big enough. Sats needed: ${totalSatsNeeded}, UTXO value: ${paymentUtxo.value}`) + } + + // add UTXO for sell(STILL CANNOT SPEND - not signed yet) + transactionBuilder.addInput(orderInfo.utxoTxid, orderInfo.utxoVout) + + // add payment UTXO + transactionBuilder.addInput(paymentUtxo.tx_hash, paymentUtxo.tx_pos) + + const originalAmount = paymentUtxo.value + const dust = 546 + const remainder = originalAmount - satsNeeded - dust // exchange fee + token UTXO dust + + // Add the SLP OP_RETURN data as the first output. + transactionBuilder.addOutput(slpData, 0) + + const buyerAddr = this.bchWallet.walletInfo.legacyAddress + // console.log(`buyAddr: ${JSON.stringify(buyAddr, null, 2)}`) + + // Send dust transaction representing tokens being sent. + transactionBuilder.addOutput( + buyerAddr, + dust + ) + + // Get seller address + // TODO: Seller should explicitly define the address to send to in the + // orderInfo object. Right now it retrieves the address from the UTXO + // for sale, which is not ideal. + const sellerAddr = txData[0].vout[1].scriptPubKey.addresses[0] + + // Send payment to the offer side + transactionBuilder.addOutput(sellerAddr, satsNeeded) + + // Send the BCH change back to the buyer + if (remainder > 550) { + transactionBuilder.addOutput(buyerAddr, remainder) + } + + const buyerECPair = bchjs.ECPair.fromWIF(this.bchWallet.walletInfo.privateKey) + + // Sign the buyers input UTXO for spending. + transactionBuilder.sign( + 1, + buyerECPair, + null, + transactionBuilder.hashTypes.SIGHASH_ALL, + originalAmount + ) + + const tx = transactionBuilder.transaction.buildIncomplete() + + const hex = tx.toHex() + console.log('hex: ', hex) + + return hex + } catch (err) { + console.error('Error in wallet.js/generatePartialTx(): ', err) + throw err + } + + // return true } } diff --git a/src/use-cases/order/index.js b/src/use-cases/order/index.js index d8a1e40..1f0648e 100644 --- a/src/use-cases/order/index.js +++ b/src/use-cases/order/index.js @@ -107,11 +107,11 @@ class OrderUseCases { // Create a partially signed transaction. // https://github.com/Permissionless-Software-Foundation/bch-js-examples/blob/master/bch/applications/collaborate/sell-slp/e2e-exchange/step2-purchase-tx.js#L59 - // const partialTxHex = await this.adapaters.wallet.generatePartialTx(orderInfo) + const partialTxHex = await this.adapters.wallet.generatePartialTx(orderInfo) - // return partialTxHex + return partialTxHex - return true + // return true } catch (err) { console.error('Error in use-cases/order/takeOrder()') throw err