Skip to content

Instantly share code, notes, and snippets.

@cpacia
Last active June 8, 2019 16:17
Show Gist options
  • Save cpacia/97de7a487d0876aea3147b4d9a22763a to your computer and use it in GitHub Desktop.
Save cpacia/97de7a487d0876aea3147b4d9a22763a to your computer and use it in GitHub Desktop.
SLP redeem script
var prevRedeemScript = readFromStack()

var preimage = readFromStack()
validatePreimage(preimage)

var outputTransfers = readFromStack()

var siganture = readFromStack()

var tokenMetadata

// If this is the genesis transaction allow the script to create
// and save all the token metadata.
if tokenMetadata == nil {
	tokenMetadata.tokenID = preimage.prevout
	tokenMetadata.numCoins = 2000000000
	tokenMetadata.inputValue = 2000000000
	// etc
} else {
	// Otherwise validate prevRedeemScript
	if makeOutputScript(hash160(prevRedeemScript)) != preimage.scriptCode {
		fail()
	}

	tokenMetadata = prevRedeemScript.metadata
}

checkSig(redeemScript.pubkey, signature)

// For each requested output construct a new redeemScript/outputScript which
// contains the exact same token metadata with the exception of a new value and
// a new public key.
//
// Validate that the covenant sends to these output scripts.
var totalOut = 0
for i in range len(outputTransfers) {
	totalOut += outputTransfers[i].value

	// We are including the input's prevOutpoint in the new redeem script to ensure that the redeemScript is unique
	// for this input. This prevents other inputs in the transaction from claiming the same outputs as satisfying their
	// covenant. If they tried, they wouldn't produce a redeemScript that satisfies the covenant. 
	newRedeemScript = makeNewRedeemScript(outputTransfers[i].value, outputTransfers[i].pubkey, preimage.prevout)
	outputScript = makeOutputScript(hash160(redeemScript))

	if preimage.outputs[i].script != outputScript {
		fail()
	}
}

// Validate that the total out == total in.
if totalOut != tokenMetadata.inputValue {
	fail()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment