|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { ccc } from "../../index.js"; |
| 3 | + |
| 4 | +const client = new ccc.ClientPublicTestnet(); |
| 5 | + |
| 6 | +describe("MultisigCkbWitness", () => { |
| 7 | + it("should encode and decode correctly", () => { |
| 8 | + const witness: ccc.MultisigCkbWitnessLike = { |
| 9 | + publicKeys: [ |
| 10 | + "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ece3d8d3c80fa80", |
| 11 | + "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ece3d8d3c80fa81", |
| 12 | + ], |
| 13 | + threshold: 1, |
| 14 | + mustMatch: 0, |
| 15 | + signatures: [], |
| 16 | + }; |
| 17 | + |
| 18 | + const encoded = ccc.MultisigCkbWitness.from(witness).toBytes(); |
| 19 | + const decoded = ccc.MultisigCkbWitness.decode(encoded); |
| 20 | + |
| 21 | + expect(decoded.threshold).toBe(witness.threshold); |
| 22 | + expect(decoded.mustMatch).toBe(witness.mustMatch); |
| 23 | + expect(decoded.publicKeyHashes.length).toBe(witness.publicKeys.length); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should throw error for invalid threshold", () => { |
| 27 | + expect(() => { |
| 28 | + new ccc.MultisigCkbWitness([], 0, 0, []); |
| 29 | + }).toThrow("threshold should be in range from 1 to public keys length"); |
| 30 | + |
| 31 | + expect(() => { |
| 32 | + new ccc.MultisigCkbWitness([], 1, 0, []); |
| 33 | + }).toThrow("threshold should be in range from 1 to public keys length"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should throw error for invalid mustMatch", () => { |
| 37 | + expect(() => { |
| 38 | + new ccc.MultisigCkbWitness(["0x00"], 1, 2, []); |
| 39 | + }).toThrow( |
| 40 | + "mustMatch should be in range from 0 to min(public keys length, threshold)", |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should calculate scriptArgs correctly", () => { |
| 45 | + const witness: ccc.MultisigCkbWitnessLike = { |
| 46 | + publicKeys: [ |
| 47 | + "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ece3d8d3c80fa80", |
| 48 | + "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ece3d8d3c80fa81", |
| 49 | + ], |
| 50 | + threshold: 1, |
| 51 | + mustMatch: 0, |
| 52 | + }; |
| 53 | + const multisigWitness = ccc.MultisigCkbWitness.from(witness); |
| 54 | + const args = multisigWitness.scriptArgs(); |
| 55 | + expect(args).toBeInstanceOf(Uint8Array); |
| 56 | + expect(ccc.hexFrom(args)).toBe( |
| 57 | + "0x6418f118e94d8dff7d9b0b59a4d837c4e201c5a9", |
| 58 | + ); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("SignerMultisigCkbReadonly", () => { |
| 63 | + it("should initialize correctly", async () => { |
| 64 | + const witness: ccc.MultisigCkbWitnessLike = { |
| 65 | + publicKeys: [ |
| 66 | + "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ece3d8d3c80fa80", |
| 67 | + "0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ece3d8d3c80fa81", |
| 68 | + ], |
| 69 | + threshold: 1, |
| 70 | + mustMatch: 0, |
| 71 | + }; |
| 72 | + |
| 73 | + const signer = new ccc.SignerMultisigCkbReadonly(client, witness); |
| 74 | + |
| 75 | + expect(await signer.getMemberCount()).toBe(2); |
| 76 | + expect(await signer.getMemberThreshold()).toBe(1); |
| 77 | + }); |
| 78 | +}); |
0 commit comments