-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codec): molecule pack works with BytesLike
- Loading branch information
Showing
4 changed files
with
81 additions
and
18 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import test from "ava"; | ||
import { blockchain, molecule, number } from "../src"; | ||
import { randomBytes } from "crypto"; | ||
import { concat, hexify } from "../src/bytes"; | ||
import { BI } from "@ckb-lumos/bi"; | ||
|
||
const { struct } = molecule; | ||
const { Uint32 } = number; | ||
const { Byte32 } = blockchain; | ||
|
||
test("pack with BytesLike and BIish", (t) => { | ||
const OutPoint = struct({ tx_hash: Byte32, index: Uint32 }, [ | ||
"tx_hash", | ||
"index", | ||
]); | ||
|
||
const txHash = randomBytes(32); | ||
|
||
const packed1 = OutPoint.pack({ tx_hash: txHash, index: 0 }); | ||
const packed2 = OutPoint.pack({ tx_hash: hexify(txHash), index: "0x0" }); | ||
const packed3 = OutPoint.pack({ tx_hash: txHash, index: "0x0" }); | ||
const packed4 = OutPoint.pack({ tx_hash: txHash, index: BI.from(0) }); | ||
|
||
t.deepEqual(packed1, concat(txHash, "0x00000000")); | ||
t.deepEqual(packed1, packed2); | ||
t.deepEqual(packed2, packed3); | ||
t.deepEqual(packed3, packed4); | ||
}); | ||
|
||
test("unpack with BytesLike", (t) => { | ||
const OutPoint = struct({ tx_hash: Byte32, index: Uint32 }, [ | ||
"tx_hash", | ||
"index", | ||
]); | ||
|
||
const txHash = randomBytes(32); | ||
const index = "0x00000000"; | ||
|
||
const unpacked1 = OutPoint.unpack(concat(txHash, index)); | ||
const unpacked2 = OutPoint.unpack(hexify(concat(txHash, index))); | ||
const unpacked3 = OutPoint.unpack(Array.from(concat(txHash, index))); | ||
|
||
t.deepEqual(unpacked1, { tx_hash: hexify(txHash), index: 0 }); | ||
t.deepEqual(unpacked1, unpacked2); | ||
t.deepEqual(unpacked2, unpacked3); | ||
}); |