-
Notifications
You must be signed in to change notification settings - Fork 86
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
10 changed files
with
121 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,42 +1,62 @@ | ||
import { ByteString, FixedArray, PubKey, Sha256, Sig, SmartContract, assert, method, prop, sha256, toByteString } from "scrypt-ts"; | ||
import { MerklePath, MerkleProof } from 'scrypt-ts-lib'; | ||
import { | ||
ByteString, | ||
FixedArray, | ||
PubKey, | ||
Sha256, | ||
Sig, | ||
SmartContract, | ||
assert, | ||
method, | ||
prop, | ||
sha256, | ||
toByteString, | ||
} from 'scrypt-ts' | ||
import { MerklePath, MerkleProof } from 'scrypt-ts-lib' | ||
|
||
// tree signatures: Merkle tree-based multisig | ||
export class TreeSig extends SmartContract{ | ||
|
||
|
||
export class TreeSig extends SmartContract { | ||
// M out of N multisig | ||
static readonly M : bigint = 3n; | ||
static readonly M: bigint = 3n | ||
|
||
@prop() | ||
readonly merkleRoot : Sha256; | ||
readonly merkleRoot: Sha256 | ||
|
||
constructor(merkleRoot : Sha256){ | ||
constructor(merkleRoot: Sha256) { | ||
super(...arguments) | ||
this.merkleRoot = merkleRoot | ||
} | ||
|
||
@method() | ||
public main(pubKeys : FixedArray<PubKey, 3>, sigs : FixedArray<Sig, 3> , merkleproof : MerkleProof) { | ||
public main( | ||
pubKeys: FixedArray<PubKey, 3>, | ||
sigs: FixedArray<Sig, 3>, | ||
merkleproof: MerkleProof | ||
) { | ||
// validate public keys are from the merkle tree | ||
assert(MerklePath.calcMerkleRoot(TreeSig.pubKeys2Leaf(pubKeys), merkleproof) == this.merkleRoot); | ||
assert( | ||
MerklePath.calcMerkleRoot( | ||
TreeSig.pubKeys2Leaf(pubKeys), | ||
merkleproof, | ||
32 | ||
) == this.merkleRoot | ||
) | ||
|
||
// check if all M signatures are valid | ||
let allMatch :boolean = true; | ||
for (let i = 0; i < 3; i ++) { | ||
allMatch = allMatch && this.checkMultiSig(sigs, pubKeys); | ||
let allMatch: boolean = true | ||
for (let i = 0; i < 3; i++) { | ||
allMatch = allMatch && this.checkMultiSig(sigs, pubKeys) | ||
} | ||
assert(allMatch); | ||
assert(allMatch) | ||
} | ||
|
||
// map public keys to a leaf | ||
@method() | ||
static pubKeys2Leaf(pubKeys : FixedArray<PubKey, 3>) : Sha256 { | ||
let leaf : ByteString = toByteString(''); | ||
static pubKeys2Leaf(pubKeys: FixedArray<PubKey, 3>): Sha256 { | ||
let leaf: ByteString = toByteString('') | ||
|
||
for (let i = 0; i < 3; i ++) { | ||
leaf += pubKeys[i]; | ||
for (let i = 0; i < 3; i++) { | ||
leaf += pubKeys[i] | ||
} | ||
return sha256(leaf); | ||
return sha256(leaf) | ||
} | ||
} |