-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from obsidiansystems/repro
Reproducing bugs
- Loading branch information
Showing
5 changed files
with
1,157 additions
and
21 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
examples/ckb-sdk-js-example/packages/main/bin/change.cjs
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,150 @@ | ||
#!/usr/bin/env node | ||
|
||
const path = require('path') | ||
const os = require('os') | ||
const CKB = require('@nervosnetwork/ckb-sdk-core').default | ||
const { Indexer, CellCollector } = require('@ckb-lumos/indexer') | ||
|
||
const LUMOS_DB = process.env.LUMOS_DB || path.join(os.tmpdir(), 'lumos_db') | ||
const CKB_URL = process.env.CKB_URL || 'http://localhost:8114'; | ||
|
||
const Transport = require("@ledgerhq/hw-transport-node-hid").default; | ||
|
||
const LedgerCkb = require("hw-app-ckb").default; | ||
|
||
const ckbPath = `44'/309'/0'`; | ||
|
||
const indexer = new Indexer(CKB_URL, LUMOS_DB) | ||
|
||
const startSync = async () => { | ||
indexer.startForever() | ||
await new Promise((resolve) => setTimeout(resolve, 200000)) | ||
} | ||
|
||
const stopSync = () => { | ||
indexer.stop() | ||
} | ||
|
||
const bootstrap = async () => { | ||
const nodeUrl = process.env.NODE_URL || CKB_URL // example node url | ||
const blockAssemblerCodeHash = '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8' // transcribe the block_assembler.code_hash in the ckb.toml from the ckb projec | ||
|
||
const ckb = new CKB(nodeUrl) // instantiate the JS SDK with provided node url | ||
|
||
const { secp256k1Dep } = await ckb.loadDeps() | ||
|
||
let transport = await Transport.open(); | ||
|
||
const lckb = new LedgerCkb(transport); | ||
|
||
const keydata = await lckb.getWalletPublicKey(ckbPath, true) | ||
const address = keydata.address | ||
const addresses = { testnetAddress: address } | ||
const publicKeyHash = "0x" + keydata.lockArg | ||
|
||
/** | ||
* to see the addresses | ||
*/ | ||
console.log('addresses.testnetAddress', addresses.testnetAddress) | ||
console.log('publicKeyHash', publicKeyHash) | ||
console.log('addresses', addresses) | ||
|
||
/** | ||
* calculate the lockHash by the address publicKeyHash | ||
* 1. the publicKeyHash of the address is required in the args field of lock script | ||
* 2. compose the lock script with the code hash(as a miner, we use blockAssemblerCodeHash here), and args | ||
* 3. calculate the hash of lock script via ckb.utils.scriptToHash method | ||
*/ | ||
|
||
const lockScript = { | ||
hashType: "type", | ||
codeHash: blockAssemblerCodeHash, | ||
args: publicKeyHash, | ||
} | ||
|
||
// method to fetch all unspent cells by lock hash | ||
const locks = [ | ||
{ lockHash: ckb.utils.scriptToHash(lockScript) }, | ||
//lockScript, | ||
//{...secp256k1Dep, args: publicKeyHash } | ||
] | ||
console.log('locks', locks) | ||
|
||
const cells = await Promise.all( | ||
locks.map(lock => ckb.loadCells(lock /*{ indexer, CellCollector, lock }*/)) | ||
) | ||
|
||
const unspentCells = cells.flat() | ||
console.log('unspentCells', unspentCells) | ||
|
||
const rawTransaction = ckb.generateRawTransaction({ | ||
fromAddress: addresses.testnetAddress, | ||
toAddress: 'ckt1qyq9t9n5qj58wrnanafe6862t6wxeeaww3csvdfg44', | ||
// capacity: BigInt(9200000000), | ||
capacity: BigInt(40000000000), | ||
fee: BigInt(100000), | ||
safeMode: true, | ||
cells: unspentCells, | ||
deps: ckb.config.secp256k1Dep, | ||
}) | ||
|
||
console.log('rawTransaction', rawTransaction) | ||
console.log('rawTransaction.inputs', rawTransaction.inputs) | ||
|
||
// const rawTransaction = ckb.generateDaoDepositTransaction({ | ||
// fromAddress: addresses.testnetAddress, | ||
// capacity: BigInt(10400000000), | ||
// fee: BigInt(100000), | ||
// // cells: unspentCells | ||
// }) | ||
|
||
rawTransaction.witnesses = rawTransaction.inputs.map(() => '0x') | ||
rawTransaction.witnesses[0] = ckb.utils.serializeWitnessArgs({ | ||
lock: '', | ||
inputType: '', | ||
outputType: '' | ||
}) | ||
rawTransaction.witnesses[1] = ckb.utils.serializeWitnessArgs({ | ||
lock: '', | ||
inputType: '', | ||
outputType: '' | ||
}) | ||
|
||
|
||
// fetch all the context transactions | ||
ctxds = (await Promise.all(rawTransaction.inputs.map(a=>ckb.rpc.getTransaction(a.previousOutput.txHash)))).map(a=>a.transaction) | ||
|
||
for (ctdx in ctxds) { | ||
console.log('ctdx', ctdx) | ||
console.log('ctdx.inputs', ctdx.inputs) | ||
} | ||
|
||
const formatted = ckb.rpc.paramsFormatter.toRawTransaction(rawTransaction) | ||
const formattedCtxd = ctxds.map(ckb.rpc.paramsFormatter.toRawTransaction) | ||
//console.log(formatted); | ||
//console.log(formattedCtxd); | ||
|
||
const signature1 = await lckb.signTransaction(ckbPath, formatted, [formatted.witnesses[0]], formattedCtxd, "44'/309'/0'/1/0") | ||
const signature2 = await lckb.signTransaction("44'/309'/0'/0/1", formatted, [formatted.witnesses[1]], formattedCtxd, "44'/309'/0'/1/0") | ||
|
||
rawTransaction.witnesses[0] = ckb.utils.serializeWitnessArgs( { lock: "0x"+signature1, inputType: '', outputType: '' }); | ||
rawTransaction.witnesses[1] = ckb.utils.serializeWitnessArgs( { lock: "0x"+signature2, inputType: '', outputType: '' }); | ||
//console.log('rawTransaction.witnesses', rawTransaction.witnesses) | ||
const realTxHash = await ckb.rpc.sendTransaction(rawTransaction).catch(err=>err) | ||
|
||
/** | ||
* to see the real transaction hash | ||
*/ | ||
console.log(`The real transaction hash is: ${realTxHash}`) | ||
} | ||
|
||
(async () => { | ||
try { | ||
stopSync() | ||
await bootstrap() | ||
startSync() | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
process.exit(0) | ||
})() |
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,125 @@ | ||
#!/usr/bin/env node | ||
|
||
const CKB = require("@nervosnetwork/ckb-sdk-core").default; | ||
|
||
const CKB_URL = process.env.CKB_URL || 'http://localhost:8114'; | ||
|
||
const LedgerCkb = require("hw-app-ckb").default; | ||
|
||
const ckbPath = `44'/309'/0'`; | ||
|
||
// Whether to connect to a running instance of the Speculos simulator for | ||
// ledger apps or a real physical ledger | ||
const useSpeculos = false | ||
|
||
let Transport = null | ||
if ( useSpeculos ) { | ||
// For speculos: | ||
Transport = require("@ledgerhq/hw-transport-node-speculos").default; | ||
} else { | ||
// For a real ledger: | ||
Transport = require("@ledgerhq/hw-transport-node-hid").default; | ||
} | ||
console.log(Transport) | ||
|
||
const bootstrap = async () => { | ||
const ckb = new CKB(CKB_URL) | ||
|
||
let transport = null | ||
if ( useSpeculos ) { | ||
// To connect to a speculos instance: | ||
apduPort = 9999; | ||
transport = await Transport.open( { apduPort } ); | ||
} else { | ||
transport = await Transport.open(); | ||
} | ||
|
||
const lckb = new LedgerCkb(transport); | ||
|
||
const keydata = await lckb.getWalletPublicKey(ckbPath, true); | ||
console.log('keydata', keydata); | ||
|
||
const publicKeyHash = "0x" + keydata.lockArg; | ||
const address = keydata.address; | ||
const addresses = { testnetAddress: address }; | ||
const loadCells = async () => { | ||
await ckb.loadDeps(); | ||
const lockHash = ckb.generateLockHash(publicKeyHash); | ||
return await ckb.loadCells({ | ||
lockHash, | ||
start: BigInt(0), | ||
end: BigInt(500000), | ||
save: true, | ||
}); | ||
}; | ||
|
||
const cells = await loadCells(); | ||
|
||
const rawTransaction = ckb.generateDaoDepositTransaction({ | ||
fromAddress: addresses.testnetAddress, | ||
capacity: BigInt(10400000000), | ||
fee: BigInt(100000), | ||
cells, | ||
}); | ||
|
||
console.log('rawTransaction', rawTransaction); | ||
|
||
rawTransaction.witnesses = rawTransaction.inputs.map(() => "0x"); | ||
rawTransaction.witnesses[0] = ckb.utils.serializeWitnessArgs({ | ||
lock: "", | ||
inputType: "", | ||
outputType: "", | ||
}); | ||
|
||
console.log("rawTransaction:", JSON.stringify(rawTransaction)); | ||
|
||
const ctxds = ( | ||
await Promise.all( | ||
rawTransaction.inputs.map((a) => | ||
ckb.rpc.getTransaction(a.previousOutput.txHash) | ||
) | ||
) | ||
).map((a) => a.transaction); | ||
|
||
const formatted = ckb.rpc.paramsFormatter.toRawTransaction(rawTransaction); | ||
const formattedCtxd = ctxds.map(ckb.rpc.paramsFormatter.toRawTransaction); | ||
|
||
try { | ||
console.log('annotatedTransaction', JSON.stringify(lckb.buildAnnotatedTransaction( | ||
ckbPath, | ||
formatted, | ||
formatted.witnesses, | ||
formattedCtxd, | ||
ckbPath | ||
))); | ||
const signature = await lckb.signTransaction( | ||
ckbPath, | ||
formatted, | ||
formatted.witnesses, | ||
formattedCtxd, | ||
ckbPath | ||
); | ||
rawTransaction.witnesses[0] = ckb.utils.serializeWitnessArgs({ | ||
lock: "0x" + signature, | ||
inputType: "", | ||
outputType: "", | ||
}); | ||
|
||
const realTxHash = await ckb.rpc | ||
.sendTransaction(rawTransaction) | ||
.catch((err) => err); | ||
|
||
/** | ||
* to see the real transaction hash | ||
*/ | ||
console.log(`The real transaction hash is: ${realTxHash}`); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
|
||
try { | ||
bootstrap(); | ||
} catch (error) { | ||
console.log(error) | ||
} |
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.