forked from ckb-js/lumos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common-scripts): integrete omnilock p2pkh (ckb-js#405)
* chore(lumos): add pr template * feat: support omnilock * test: add test cases for omnilock * Revert "chore(lumos): add pr template" This reverts commit 2e78b59. * chore: fix CI * chore: changes to the comments * chore: use sealTransaction to put signature to witnesses * fix: can't resolve stream in browser
- Loading branch information
1 parent
73af0bc
commit 09bd023
Showing
14 changed files
with
706 additions
and
139 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"parcel": "^2.2.1" | ||
}, | ||
"alias": { | ||
"process": false | ||
"process": false, | ||
"stream": false | ||
} | ||
} |
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,83 @@ | ||
import { bytes } from "@ckb-lumos/codec"; | ||
import { | ||
TransactionSkeleton, | ||
encodeToAddress, | ||
sealTransaction, | ||
} from "@ckb-lumos/helpers"; | ||
import { key } from "@ckb-lumos/hd"; | ||
import { initializeConfig, predefined } from "@ckb-lumos/config-manager/lib"; | ||
import { common, omnilock } from "@ckb-lumos/common-scripts"; | ||
import { Script } from "@ckb-lumos/base"; | ||
import { Indexer } from "@ckb-lumos/ckb-indexer"; | ||
import { RPC } from "@ckb-lumos/rpc"; | ||
|
||
const CKB_RPC_URL = "https://testnet.ckb.dev/rpc"; | ||
const CKB_INDEXER_URL = "https://testnet.ckb.dev/indexer"; | ||
const rpc = new RPC(CKB_RPC_URL); | ||
const indexer = new Indexer(CKB_INDEXER_URL, CKB_RPC_URL); | ||
|
||
async function main() { | ||
let txSkeleton = TransactionSkeleton({ cellProvider: indexer }); | ||
const ckbConfig = predefined.AGGRON4; | ||
initializeConfig(ckbConfig); | ||
const ALICE_PRIVKEY = | ||
"0x1234567812345678123456781234567812345678123456781234567812345678"; | ||
const aliceArgs = key.privateKeyToBlake160(ALICE_PRIVKEY); | ||
console.log("alice args is:", aliceArgs); | ||
|
||
const aliceOmnilock: Script = omnilock.createOmnilockScript({ | ||
auth: { | ||
flag: "SECP256K1_BLAKE160", | ||
content: aliceArgs, | ||
}, | ||
}); | ||
const aliceSecplock: Script = { | ||
codeHash: ckbConfig.SCRIPTS.SECP256K1_BLAKE160.CODE_HASH, | ||
hashType: ckbConfig.SCRIPTS.SECP256K1_BLAKE160.HASH_TYPE, | ||
args: aliceArgs, | ||
}; | ||
|
||
console.log("aliceOmnilock is:", aliceOmnilock); | ||
const aliceOmnilockAddress = encodeToAddress(aliceOmnilock, { | ||
config: ckbConfig, | ||
}); | ||
const aliceSecplockAddress = encodeToAddress(aliceSecplock, { | ||
config: ckbConfig, | ||
}); | ||
|
||
console.log("aliceOmnilockAddress is:", aliceOmnilockAddress); | ||
|
||
txSkeleton = await common.transfer( | ||
txSkeleton, | ||
[aliceOmnilockAddress], | ||
aliceSecplockAddress, | ||
BigInt(70 * 10 ** 8), | ||
undefined, | ||
undefined, | ||
{ config: ckbConfig } | ||
); | ||
|
||
txSkeleton = await common.payFee( | ||
txSkeleton, | ||
[aliceOmnilockAddress], | ||
1000, | ||
undefined, | ||
{ config: ckbConfig } | ||
); | ||
|
||
txSkeleton = common.prepareSigningEntries(txSkeleton, { config: ckbConfig }); | ||
|
||
const message = txSkeleton.get("signingEntries").get(0)!.message; | ||
|
||
const sig = key.signRecoverable(message, ALICE_PRIVKEY); | ||
const omnilockSig = bytes.hexify( | ||
omnilock.OmnilockWitnessLock.pack({ signature: sig }) | ||
); | ||
|
||
const tx = sealTransaction(txSkeleton, [omnilockSig]); | ||
const hash = await rpc.sendTransaction(tx, "passthrough"); | ||
console.log("tx is:", tx); | ||
console.log("The transaction hash is", hash); | ||
return hash; | ||
} | ||
main(); |
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
Oops, something went wrong.