Skip to content

P2PK

P2PK (Pay-to-Public-Key) locks an Output to a Public Key itself, rather than to its hash: <pubkey> OP_CHECKSIG. Spending it takes only a Signature. It appears in early coinbase Outputs, and in some protocols.

The examples use the keys and the fundingTransaction helper described in About the examples.

final locker = P2PKLockBuilder(aliceKey.publicKey);
locker.getScriptPubkey().toString(type: 'asm'); // <33-byte public key> OP_CHECKSIG

A Signature-only Unlocking Script builder takes only a few lines (writing your own builders is covered in Custom script builders):

/// Unlocking Script for P2PK: the Signature alone.
class SignatureOnlyUnlocker extends UnlockingScriptBuilder {
@override
SVScript getScriptSig() {
if (signatures.isEmpty) return SVScript(); // not signed yet
return ScriptBuilder()
.addData(Uint8List.fromList(HEX.decode(signatures[0].toTxFormat())))
.build();
}
@override
void parse(SVScript script) {}
}

We use it like any other unlocker:

final signer = DefaultTransactionSigner(sighashAll, aliceKey);
final tx = TransactionBuilder()
.spendFromTxnWithSigner(signer, fundingTx, 0, TransactionInput.MAX_SEQ_NUMBER, SignatureOnlyUnlocker())
.spendToPKH(aliceKey.toAddress(networkType: NetworkType.TEST), BigInt.from(9000))
.build(false);