Skip to content

Commit

Permalink
Merge pull request #1 from obsidiansystems/repro
Browse files Browse the repository at this point in the history
Reproducing bugs
  • Loading branch information
Ericson2314 committed Sep 29, 2020
2 parents 5e04cad + 9871090 commit 32a236d
Show file tree
Hide file tree
Showing 5 changed files with 1,157 additions and 21 deletions.
150 changes: 150 additions & 0 deletions examples/ckb-sdk-js-example/packages/main/bin/change.cjs
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)
})()
125 changes: 125 additions & 0 deletions examples/ckb-sdk-js-example/packages/main/bin/dao.cjs
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)
}
4 changes: 2 additions & 2 deletions examples/ckb-sdk-js-example/packages/main/bin/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ const bootstrap = async () => {
}

const lckb = new LedgerCkb(transport)

const keydata = await lckb.getWalletPublicKey("44'/309'/0'/0/0", true)
const publicKeyHash = "0x" + keydata.lockArg
const address = keydata.address
addresses = { testnetAddress: address }

/**
* to see the addresses
*/
Expand Down
9 changes: 6 additions & 3 deletions examples/ckb-sdk-js-example/packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
"main": "bin/index.cjs",
"type": "commonjs",
"bin": {
"main": "bin/index.cjs"
"main": "bin/index.cjs",
"change": "bin/change.cjs"
},
"dependencies": {
"util": "*",
"hw-app-ckb": "*",
"@ledgerhq/hw-transport": "^5.9.0",
"@nervosnetwork/ckb-sdk-core": "*",
"@nervosnetwork/ckb-sdk-core": "0.35.0",
"@nervosnetwork/ckb-sdk-rpc": "*",
"@ledgerhq/hw-transport-node-speculos": "*",
"@ledgerhq/hw-transport-node-hid": "*"
"@ledgerhq/hw-transport-node-hid": "*",
"@ckb-lumos/base": "0.12.0",
"@ckb-lumos/indexer": "0.12.0"
},
"devDependencies": {
}
Expand Down
Loading

0 comments on commit 32a236d

Please sign in to comment.