-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { assert } from 'console' | ||
import { | ||
Addr, | ||
FixedArray, | ||
hash256, | ||
method, | ||
prop, | ||
PubKey, | ||
Sig, | ||
SmartContract, | ||
Utils, | ||
} from 'scrypt-ts' | ||
|
||
export class CourtCase extends SmartContract { | ||
static readonly JURY_THRESHOLD = 6 | ||
|
||
@prop() | ||
jury: FixedArray<PubKey, 12> | ||
|
||
@prop(true) | ||
judge: PubKey | ||
|
||
@prop() | ||
defendant: Addr | ||
|
||
@prop() | ||
dismissalDeadline: bigint | ||
|
||
constructor( | ||
jury: FixedArray<PubKey, 12>, | ||
judge: PubKey, | ||
defendant: Addr, | ||
stateTreasury: Addr, | ||
dismissalDeadline: bigint | ||
) { | ||
super(...arguments) | ||
this.jury = jury | ||
this.judge = judge | ||
this.defendant = defendant | ||
this.dismissalDeadline = dismissalDeadline | ||
} | ||
|
||
@method() | ||
public resolve( | ||
sigsJury: FixedArray<Sig, typeof CourtCase.JURY_THRESHOLD>, | ||
sigJudge: Sig | ||
) { | ||
// Check jury signatures. | ||
assert(this.checkMultiSig(sigsJury, this.jury), 'jury invalid multisig') | ||
|
||
// Check judge signature. | ||
assert(this.checkSig(sigJudge, this.judge), 'judge invalid sig') | ||
|
||
// Release funds to defendant. | ||
let outputs = Utils.buildAddressOutput( | ||
this.defendant, | ||
this.ctx.utxo.value | ||
) | ||
outputs += this.buildChangeOutput() | ||
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch') | ||
} | ||
|
||
@method() | ||
public dismiss(sigJudge: Sig) { | ||
// Check judge signature. | ||
assert(this.checkSig(sigJudge, this.judge), 'judge invalid sig') | ||
|
||
// Check deadline. | ||
assert( | ||
this.timeLock(this.dismissalDeadline), | ||
'dismissal deadline not reached' | ||
) | ||
|
||
// Release funds to defendant. | ||
let outputs = Utils.buildAddressOutput( | ||
this.defendant, | ||
this.ctx.utxo.value | ||
) | ||
outputs += this.buildChangeOutput() | ||
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch') | ||
} | ||
|
||
@method() | ||
public appeal(sigJudge: Sig, appellateJudge: PubKey) { | ||
// Check judge signature. | ||
assert(this.checkSig(sigJudge, this.judge), 'judge invalid sig') | ||
|
||
// Set appellate judge as the new judge. | ||
this.judge = appellateJudge | ||
|
||
// Propagate contract. | ||
let outputs = this.buildStateOutput(this.ctx.utxo.value) | ||
outputs += this.buildChangeOutput() | ||
assert(hash256(outputs) == this.ctx.hashOutputs, 'hashOutputs mismatch') | ||
} | ||
} |