-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
44 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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { helpers } from '@ckb-lumos/lumos'; | ||
import { ccc } from '@ckb-ccc/core'; | ||
|
||
/** | ||
* Sign a Lumos transaction with the specified signer. | ||
* | ||
* @param skeleton The Lumos transaction skeleton. | ||
* @param signer An abstract signer from ccc to sign the transaction. | ||
* @param send Whether to send the transaction after signing. Default is false. | ||
* @returns The signed transaction and the transaction hash if `send` is set to True. | ||
*/ | ||
export async function signTransaction(props: { | ||
skeleton: helpers.TransactionSkeletonType; | ||
signer: ccc.Signer; | ||
send?: boolean; | ||
}): Promise<{ | ||
cccTx: ccc.Transaction; | ||
txHash?: ccc.Hex; | ||
}> { | ||
const send = props.send ?? false; | ||
const signer = props.signer; | ||
const tx = ccc.Transaction.fromLumosSkeleton(props.skeleton); | ||
const signedTx = await signer.signTransaction(tx); | ||
if (send) { | ||
const txHash = await signer.sendTransaction(signedTx); | ||
return { cccTx: signedTx, txHash }; | ||
} else { | ||
return { cccTx: signedTx }; | ||
} | ||
} |
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,63 @@ | ||
import { ccc } from '@ckb-ccc/core'; | ||
import { Eip6963 } from '@ckb-ccc/eip6963'; | ||
import { ProviderDetail } from '@ckb-ccc/eip6963/src/eip6963.advanced'; | ||
import { helpers } from '@ckb-lumos/lumos'; | ||
import { signTransaction } from './abstract'; | ||
|
||
let mainnetEip6963Signer: Eip6963.Signer | undefined = undefined; | ||
let testnetEip6963Signer: Eip6963.Signer | undefined = undefined; | ||
|
||
/** | ||
* Get the raw Eip6963 signer according to the network type. | ||
* | ||
* @param network The network type, either "mainnet" or "testnet". | ||
* @returns The Eip6963 signer. | ||
*/ | ||
export function getEip6963Signer(network: 'mainnet' | 'testnet'): Eip6963.Signer { | ||
const windowRef = window as { ethereum?: ProviderDetail }; | ||
|
||
if (!windowRef.ethereum) { | ||
throw new Error('Eip6963 compatible wallet is not loaded'); | ||
} | ||
|
||
if (network === 'mainnet') { | ||
if (mainnetEip6963Signer) { | ||
return mainnetEip6963Signer; | ||
} | ||
const client = new ccc.ClientPublicMainnet(); | ||
mainnetEip6963Signer = new Eip6963.Signer(client, windowRef.ethereum); | ||
return mainnetEip6963Signer; | ||
} else { | ||
if (testnetEip6963Signer) { | ||
return testnetEip6963Signer; | ||
} | ||
const client = new ccc.ClientPublicTestnet(); | ||
testnetEip6963Signer = new Eip6963.Signer(client, windowRef.ethereum); | ||
return testnetEip6963Signer; | ||
} | ||
} | ||
|
||
/** | ||
* Sign a Lumos transaction with Eip6963 compatible wallet. | ||
* | ||
* @param skeleton The Lumos transaction skeleton. | ||
* @param network The network type, either "mainnet" or "testnet". | ||
* @param send Whether to send the transaction after signing. Default is false. | ||
* @returns The signed transaction and the transaction hash if `send` is set to True. | ||
*/ | ||
export async function signTransactionWithEip6963CompatibleWallet(props: { | ||
skeleton: helpers.TransactionSkeletonType; | ||
network: 'mainnet' | 'testnet'; | ||
privkey: ccc.Hex; | ||
send?: boolean; | ||
}): Promise<{ | ||
cccTx: ccc.Transaction; | ||
txHash?: ccc.Hex; | ||
}> { | ||
const send = props.send ?? false; | ||
const signer = getEip6963Signer(props.network); | ||
if (!signer.isConnected()) { | ||
await signer.connect(); | ||
} | ||
return await signTransaction({ skeleton: props.skeleton, signer, send }); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.