paypro: example - add privkey option. refactor.

This commit is contained in:
Christopher Jeffrey 2014-08-18 14:00:43 -07:00
parent e93e3d83f4
commit 253d66994c
2 changed files with 27 additions and 9 deletions

View File

@ -50,7 +50,8 @@ Customer: Payment sent successfully.
If you want to alter the address or public key the testnet coins get sent to by
the payment server, you can pass in the `--pubkey` or `--address` options.
`address` has to be a testnet address, whereas `pubkey` is a hex encoded public
key.
key. The `--privkey` option is also available in the standard bitcoind privkey
format.
## Other Options

View File

@ -15,6 +15,7 @@ var fs = require('fs');
var path = require('path');
var qs = require('querystring');
var crypto = require('crypto');
var assert = require('assert');
// Disable strictSSL
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
@ -71,7 +72,7 @@ app.use(function(req, res, next) {
};
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
res.setHeader('Access-Control-Allow-Headers', [
'Host',
'Connection',
@ -138,19 +139,34 @@ app.get('/-/request', function(req, res, next) {
[2000, 1000, 10000].forEach(function(value) {
var po = new PayPro();
po = po.makeOutput();
// number of satoshis to be paid
po.set('amount', value);
// a TxOut script where the payment should be sent. similar to OP_CHECKSIG
if (argv.pubkey || argv.address) {
// Instead of creating it ourselves:
// if (!argv.pubkey && !argv.privkey && !argv.address) {
// argv.pubkey = '3730febcba04bad0cd476cfb820f9c37d7466fd9';
// }
if (argv.pubkey || argv.privkey || argv.address) {
var pubKey;
if (argv.address) {
pubKey = bitcore.Base58Check.decode(new Buffer(argv.address));
} else {
if (argv.pubkey) {
pubKey = new Buffer(argv.pubkey, 'hex');
// If it were possible:
// pubKey = bitcore.Script.fromCompressedPubKey(new Buffer(argv.pubkey)).toCompressedPubKey();
} else if (argv.privkey) {
pubKey = bitcore.Key.recoverPubKey(new Buffer(argv.privkey)).toCompressedPubKey();
} else if (argv.address) {
pubKey = bitcore.Base58Check.decode(new Buffer(argv.address));
// pubKey = bitcore.Script.fromUncompressedPubKey(pubKey).toCompressedPubKey();
}
var pubKeyHash = bitcore.util.sha256ripe160(pubKey);
var address = new bitcore.Address(pubKeyHash, 'testnet');
var scriptPubKey = addr.getScriptPubKey();
// var pubKeyHash = bitcore.util.sha256ripe160(pubKey);
// var address = new bitcore.Address(111, pubKeyHash);
var address = bitcore.Address.fromPubKey(pubKey, 'testnet');
var scriptPubKey = address.getScriptPubKey();
assert.equal(scriptPubKey.isPubkeyHash(), true);
po.set('script', scriptPubKey.getBuffer());
} else {
po.set('script', new Buffer([
@ -182,6 +198,7 @@ app.get('/-/request', function(req, res, next) {
172 // OP_CHECKSIG
]));
}
outputs.push(po.message);
});