escrow-bitcoin
Version:
A service to make possible escrow transactions using BTC (Bitcoin)
45 lines (42 loc) • 1.78 kB
JavaScript
const Address = require("./address")
const Transaction = require("./transaction")
const bitcore = require("bitcore-lib");
/**
* Make Escrow wallet send bitcoin
*
* @param {Address} escrowAddress - Address to withdraw
* @param {PrivateKey} escrowPrivateKey - PrivateKey from Address to withdraw to sign transaction
* @param {Address} addressTo - Address to send
* @param {Number} amount - Amount (in satoshis)
* @param {Networks} [network=bitcore.Networks.testnet] - Network to make transaction
* @returns string (transaction id)
*/
function Escrow(escrowAddress, escrowPrivateKey, addressTo, amount, network = bitcore.Networks.testnet) {
return new Promise(function (resolve, reject) {
new Transaction(escrowAddress, escrowPrivateKey, addressTo, amount, network).then(function (transactionId) {
resolve(transactionId)
}).catch(reject)
});
}
/* ... */
/**
* Create a Escrow Wallet
*
* @param {Number} amount - Amount to escrow (in satoshis)
* @param {Networks} [network=bitcore.Networks.testnet] - Network to make transaction
* @returns {Object} (with privateKey, adress and URI to request payment)
*/
Escrow.createEscrowAddress = function createEscrowAddress(amount, network = bitcore.Networks.testnet) {
return new Promise(function (resolve, reject) {
const privateKey = Address.createPrivateKey(undefined, network);
new Address(privateKey, network).then(function (escrowAddress) {
resolve({
privateKey: privateKey,
address: escrowAddress,
URI: Address.requestPayment(escrowAddress, amount)
})
}).catch(reject)
})
}
Escrow.Networks = bitcore.Networks;
module.exports = Escrow;