PotcoinJS Documentation & Tutorials
PotcoinJS is a modified version of BitcoinJS 1.x. PotcoinJS is a javascript library that can handle most of the low-level functionality necessary to build a Potcoin app. It contains routines to parse, convert, encode and decode various formats like base58check and wif, build transaction, and wrangle scripts. Most significantly, it does all the nasty ECDSA-cryptography heavy lifting necessary to sign and verify transactions.
However, PocoinJS does not talk to potcoin nodes or handle the blockchain in any way. That means, in order to create a wallet, you will need to talk to some third party service from which your app can retrieve unspent transaction outputs, propogate transactions and verify them.
Quick Examples
var key = potcoin.ECKey.makeRandom();
var wif_key = key.toWIF();
var address = key.pub.getAddress();
Reference
potcoin.Address
var addr = new potcoin.Address(hash_buf, version)
var addr = potcoin.Address.fromBase58Check(base58check_string)
// extract address from pay-to-pubkeyhash or pay-to-script
var addr = potcoin.fromOutputScript(script, network)
var base58check_string = addr.toBase58Check()
var base58check_string = addr.toString() // same thing
// create a pay-to-pubkeyhash or pay-to-script
var script = addr.toOutputScript()
var hash_buf = add.hash
var version = add.version
potcoin.Block
var blk = potcoin.Block.fromBuffer(buf)
var blk = potcoin.Block.fromHex(hex_string)
var buf = blk.getHash()
var hex_string = blk.getId()
var date = blk.getUTCDate()
var buf = blk.toBuffer()
var buf = blk.toHex()
blk.version // integer
blk.prevHash // buffer
blk.merkleRoot // buffer
blk.timestamp // integer
blk.bits // integer
blk.nonce // integer
potcoin.Crypto
var checksum_buf = potcoin.crypto.ripemd160(buf)
var checksum_buf = potcoin.crypto.sha1(buf)
var checksum_buf = potcoin.crypto.sha256(buf)
var checksum_buf = potcoin.crypto.hash160(buf)
var checksum_buf = potcoin.crypto.hash256(buf)
var checksum_buf = potcoin.crypto.HmacSHA256(buf)
var checksum_buf = potcoin.crypto.HmacSHA512(buf)
potcoin.ECKey
var eckey = potcoin.ECKey.makeRandom()
var ec_signature = key.sign(hash_buf)
var wif_livenet = key.toWIF(potcoin.networks.potcoin)
var wif_testnet = key.toWIF(potcoin.networks.potcoin)
var ecpubkey = eckey.pub // the derived public key
potcoin.ECPubKey
var ecpubkey = potcoin.ECPubKey.fromHex(hex_string)
var ecpubkey = potcoin.ECPubKey.fromBuffer(buf)
var ecpubkey = eckey.pub
var buf = ecpubkey.toBuffer()
var hex_string = ecpubkey.toHex()
var addr_livenet = ecpubkey.getAddress() // default to livenet
var addr_livenet = ecpubkey.getAddress(potcoin.networks.potcoin)
potcoin.Script
var script = potcoin.Script.fromASM(opcode_string)
var script = potcoin.Script.fromBuffer(buf)
var script = potcoin.Script.fromChunks(array_of_bufs)
var script = potcoin.Script.fromHex(hex_string)
var hash_buf = script.getHash()
var asm_string = script.toASM()
var script_buf = script.toBuffer()
var hex_string = script.toHex()
potcoin.Scripts
Utility functions for creating and decoding potcoin.Script
objects.
// Does the buffer contain a valid pubkey?
potcoin.scripts.isCanonicalPubKey(buf)
// Does the buffer contain a DER encoded ECDSA signature?
potcoin.scripts.isCanonicalSignature(buf)
// Check if script is <signature> <pubkey>, the signature script for pay-to-pubkey-hash outputs
potcoin.scripts.isPubKeyHashInput(script)
// Check if script is OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG, the standard pay-to-pubkey-hash output script
potcoin.scripts.isPubKeyHashOutput(script)
// Check if script is <signature>, the signature script for pay-to-pubkey outputs
potcoin.scripts.isPubKeyInput(script)
// Check if script is <pubkey> OP_CHECKSIG, the pay-to-pubkey output script, which are rare these days
potcoin.scripts.isPubKeyOutput(script)
// Check if script is OP_HASH160 <hash160> OP_EQUAL, the standard pay-to-script-hash output
potcoin.scripts.isScriptHashOutput(script)
// Check if script is OP_0 <sig> <sig> ... <sig>, the signature scipt for multisig outputs (if allowIncomplete is true, then allow any <sig> to be OP_0, meaning it hasn't been signed with that sig yet)
potcoin.scripts.isMultisigInput(script, allowIncomplete)
// Check if script is m <pubkey> <pubkey> ... <pubkey> m, which means that it needs m of n of those signatures to be spent
potcoin.scripts.isMultisigOutput(script)
// Check if script starts with OP_RETURN
potcoin.scripts.isNullDataOutput(script)
// Return 'pubkeyhash', 'scripthash', 'multisig', 'pubkey', 'nulldata', or 'nonstandard'
potcoin.scripts.classifyOutput(script)
// Return 'pubkeyhash', 'scripthash', 'multisig', 'pubkey', or 'nonstandard'
potcoin.scripts.classifyInput
// Generate a pay-to-pubkey script <pubKey> OP_CHECKSIG (rare these days)
potcoin.scripts.pubKeyOutput(pubkey)
// Generate a pay-to-pubkey-hash script OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG, the most common kind
potcoin.scripts.pubKeyHashOutput(hash_buf)
// Generate a pay-to-script-hash script OP_HASH160 <scriptHash> OP_EQUAL
potcoin.scripts.scriptHashOutput(hash_buf)
// Generate a m-of-n multisignature output script m <pubkey1> <pubkey2> .. <pubkeyn> n
potcoin.scripts.multisigOutput(num_needed, pubkeys)
// Generate the redemption script for a pay-to-pubkey <signature>
potcoin.scripts.pubKeyInput(sig_buf)
// Generate the redemption script for a pay-to-pubkey-hash <signature> <pubKey>
potcoin.scripts.pubKeyHashInput(sig_buf, ecpubkey)
// Generate input script for redeeming multisig, OP_0 <sig1> <sig2> ... <sign> (if multisig_output_script is given, it does sanity checks to ensure there is the right number of signatures are given)
potcoin.scripts.multisigInput(array_of_sig_bufs, multisig_output_script)
// Generate a "null" output OP_RETURN <anything>... everything after OP_RETURN is ignored so this is how you spam the blockchain (don't do that on livenet)
potcoin.scripts.nullDataOutput(anything_buf)
potcoin.Transaction
var tx = potcoin.Transaction.fromHex("")
var tx = potcoin.Transaction.fromBuffer(buf)
tx.addInput(hash, index, [sequence, [script]])
tx.addOutput(scriptPubKey, value)
var tx = tx.clone()
var buf = tx.toBuffer()
var id = tx.getId()
var hash = tx.getHash()
var len = tx.byteLength()
var str = tx.toHex()
// Static method
isCoinbaseHash(buffer)
tx.version
tx.locktime
tx.ins // array of hash { hash, index, script, sequence }
tx.outs // array of hash { script, value }
potcoin.Transaction.DEFAULT_SEQUENCE // 4294967295
potcoin.Transaction.SIGHASH_ALL // 1
potcoin.Transaction.SIGHASH_NONE // 2
potcoin.Transaction.SIGHASH_SINGLE // 3
potcoin.Transaction.SIGHASH_ANYONECANPAY // 128
potcoin.TransactionBuilder
var txb = new potcoin.TransactionBuilder()
var txb = potcoin.TransactionBuilder.fromTransaction(tx)
// tx_hash can be buffer, a hex-string or transaction object.
// tx_out_index, integer, is index of output in the referenced tx
// sequence defaults to 2^32-1,
// prevOutScript is the output script of the referenced tx
txb.addInput(tx_hash, tx_out_index, [sequence, [prevOutScript]])
// script_or_address can be a script, an Address object, or a base58check address string
txb.addOutput(script_or_address, value)
// redeem_script_of_p2sh must be provided when sigining a script-hash signature
// hash_type is one of potcoin.Transaction.SIGHASH_*
txb.sign(index, eckey, [redeem_script_of_p2sh, [hash_type]])
var tx = txb.build()
var tx = txb.buildIncomplete()
OP CODES
// push value
OP_FALSE: 0,
OP_0: 0,
OP_PUSHDATA1: 76,
OP_PUSHDATA2: 77,
OP_PUSHDATA4: 78,
OP_1NEGATE: 79,
OP_RESERVED: 80,
OP_1: 81,
OP_TRUE: 81,
OP_2: 82,
OP_3: 83,
OP_4: 84,
OP_5: 85,
OP_6: 86,
OP_7: 87,
OP_8: 88,
OP_9: 89,
OP_10: 90,
OP_11: 91,
OP_12: 92,
OP_13: 93,
OP_14: 94,
OP_15: 95,
OP_16: 96,
// control
OP_NOP: 97,
OP_VER: 98,
OP_IF: 99,
OP_NOTIF: 100,
OP_VERIF: 101,
OP_VERNOTIF: 102,
OP_ELSE: 103,
OP_ENDIF: 104,
OP_VERIFY: 105,
OP_RETURN: 106,
// stack ops
OP_TOALTSTACK: 107,
OP_FROMALTSTACK: 108,
OP_2DROP: 109,
OP_2DUP: 110,
OP_3DUP: 111,
OP_2OVER: 112,
OP_2ROT: 113,
OP_2SWAP: 114,
OP_IFDUP: 115,
OP_DEPTH: 116,
OP_DROP: 117,
OP_DUP: 118,
OP_NIP: 119,
OP_OVER: 120,
OP_PICK: 121,
OP_ROLL: 122,
OP_ROT: 123,
OP_SWAP: 124,
OP_TUCK: 125,
// splice ops
OP_CAT: 126,
OP_SUBSTR: 127,
OP_LEFT: 128,
OP_RIGHT: 129,
OP_SIZE: 130,
// bit logic
OP_INVERT: 131,
OP_AND: 132,
OP_OR: 133,
OP_XOR: 134,
OP_EQUAL: 135,
OP_EQUALVERIFY: 136,
OP_RESERVED1: 137,
OP_RESERVED2: 138,
// numeric
OP_1ADD: 139,
OP_1SUB: 140,
OP_2MUL: 141,
OP_2DIV: 142,
OP_NEGATE: 143,
OP_ABS: 144,
OP_NOT: 145,
OP_0NOTEQUAL: 146,
OP_ADD: 147,
OP_SUB: 148,
OP_MUL: 149,
OP_DIV: 150,
OP_MOD: 151,
OP_LSHIFT: 152,
OP_RSHIFT: 153,
OP_BOOLAND: 154,
OP_BOOLOR: 155,
OP_NUMEQUAL: 156,
OP_NUMEQUALVERIFY: 157,
OP_NUMNOTEQUAL: 158,
OP_LESSTHAN: 159,
OP_GREATERTHAN: 160,
OP_LESSTHANOREQUAL: 161,
OP_GREATERTHANOREQUAL: 162,
OP_MIN: 163,
OP_MAX: 164,
OP_WITHIN: 165,
// crypto
OP_RIPEMD160: 166,
OP_SHA1: 167,
OP_SHA256: 168,
OP_HASH160: 169,
OP_HASH256: 170,
OP_CODESEPARATOR: 171,
OP_CHECKSIG: 172,
OP_CHECKSIGVERIFY: 173,
OP_CHECKMULTISIG: 174,
OP_CHECKMULTISIGVERIFY: 175,
// expansion
OP_NOP1: 176,
OP_NOP2: 177,
OP_NOP3: 178,
OP_NOP4: 179,
OP_NOP5: 180,
OP_NOP6: 181,
OP_NOP7: 182,
OP_NOP8: 183,
OP_NOP9: 184,
OP_NOP10: 185,
// template matching params
OP_PUBKEYHASH: 253,
OP_PUBKEY: 254,
OP_INVALIDOPCODE: 255
potcoin.Message
var hash = potcoin.message.magicHash(message, potcoin.network.potcoin)
var signed = potcoin.message.sign(privKey, message, potcoin.network.potcoin)
var verified = potcoin.message.verify(address, signature, message, potcoin.network.potcoin)
potcoin.HDNode
// chainCode is a Buffer variable
// K is a key instanceof BigInteger, ECKey or ECPubKey
HDNode (K, chainCode, potcoin.network.potcoin)
// Seed should be at least 128 bits and no more than 512 bits
var hdNode = potcoin.HDNode.fromSeedBuffer(seed, potcoin.network.potcoin)
var address = potcoin.HDNode.fromSeedBuffer(seed, potcoin.network.potcoin)
var address = potcoin.HDNode.fromSeedHex(hex, potcoin.network.potcoin)
var address = potcoin.HDNode.fromBase58(string, potcoin.network.potcoin)
var hexBuffer = potcoin.HDNode.fromHex(hex, potcoin.network.potcoin)
var pubKey = potcoin.HDNode.getIdentifier()
var fourByteIdentifier = potcoin.HDNode.getFingerprint()
var pubKey = potcoin.HDNode.getAddress()
var hexBuffer = potcoin.HDNode.toHex(isPrivate)
var hardenedPrvKey = potcoin.HDNode.derive(index)
var hardenedPrvKey = potcoin.HDNode.deriveHardened(index)
var base58 = potcoin.HDNode.toString()