Skip to main content

2.4 On-Chain Contracts

Utilizing the combination of unlock and redeem scripts to define the data area for NOTE allows the incorporation of complex scripts, such as multi-signatures and hash locks, outside the data area.

2.4.1 Multisignature

A contract for multisignature might look like this:

  • Lock script: NOTE OP_2DROP OP_2DROP OP_2DROP <m> <A pubkey> [B pubkey] [C pubkey...] <n> OP_CHECKMULTISIG
  • Unlock script: OP_0 <A sig> [B sig] [C sig...] DATA0 DATA1 DATA2 DATA3 Flag

Multisignature can also be effectively created with the correct redeem script using P2SH/P2WSH/P2TR.

2.4.2 Advanced Contracts

By employing more sophisticated lock scripts, on-chain smart contracts that only allow transactions meeting specific criteria to be added to the blockchain can be developed. Here's an easy example of a hash lock contract that mandates a particular message besides the signature to initiate a transaction, enabling the recording of information on the blockchain.

class HashLock extends SmartContract {
@prop()
static note: ByteString = toByteString('NOTE', true)

@prop()
pubKey: PubKey

@prop()
hash: ByteString

constructor(pubKey: PubKey, hash: Sha256) {
super(...arguments)
this.pubKey = pubKey
this.hash = hash
}

@method()
public unlock(
sig: Sig,
message: ByteString,
data0: ByteString,
data1: ByteString,
data2: ByteString,
data3: ByteString,
data4: ByteString
) {
Scryptdemo.note
assert(sha256(message) == this.hash, 'Hash does not match')
assert(this.checkSig(sig, this.pubKey), 'signature check failed')
}
}

The resulting compiled script is:

044e4f54450000<pubKey><hash>78547a7572537a76537a7577775279755879a8788859795279ac77777777777777777777

ASM stands for:

4e4f5445 0 0 <pubKey> <hash> OP_OVER OP_4 OP_ROLL OP_DROP OP_2SWAP OP_3 OP_ROLL OP_DUP OP_3 OP_ROLL OP_DROP OP_NIP OP_NIP OP_2 OP_PICK OP_DROP OP_8 OP_PICK OP_SHA256 OP_OVER OP_EQUALVERIFY OP_9 OP_PICK OP_2 OP_PICK OP_CHECKSIG OP_NIP OP_NIP OP_NIP OP_NIP OP_NIP OP_NIP OP_NIP OP_NIP OP_NIP OP_NIP

Example contracts are developed with Scrypt.io. From the simplest scripts to the most complex contracts, within transaction scripts that combine unlock and redeem/lock scripts, the data's format and sequence are required to follow the guidelines established by this protocol.