-
Notifications
You must be signed in to change notification settings - Fork 0
feat: nbtc minting #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b1fa0f5
PoC nbtc minting
sczembor 87f9fd1
rename
sczembor 63748ee
fix test
sczembor 5b74e8e
linter
sczembor 5e36945
apply code review suggestions: move sui client and btc serialization …
sczembor 2f7fed6
linter
sczembor 989ed03
add new modules
sczembor 8ad18ea
Apply suggestions from code review
sczembor 8c8a8b6
apply code review suggestions
sczembor e8a4b9b
use interface
sczembor 0a16e17
linter
sczembor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| import { Transaction, TxInput, TxOutput } from "bitcoinjs-lib"; | ||
|
|
||
| export interface SerializedBtcTx { | ||
sczembor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| version: number[]; | ||
| inputCount: number; | ||
| inputs: number[]; | ||
| outputCount: number; | ||
| outputs: number[]; | ||
| lockTime: number[]; | ||
| } | ||
|
|
||
| function serializeU32(n: number): number[] { | ||
| const buffer = Buffer.alloc(4); | ||
| buffer.writeUInt32LE(n, 0); | ||
| return Array.from(buffer); | ||
| } | ||
|
|
||
| function serializeTxInputs(inputs: TxInput[]): number[] { | ||
| const buffers = inputs.map((vin) => { | ||
| const hash = Buffer.from(vin.hash); | ||
| const index = Buffer.alloc(4); | ||
| index.writeUInt32LE(vin.index, 0); | ||
| const scriptLen = Buffer.from([vin.script.length]); | ||
| const sequence = Buffer.alloc(4); | ||
| sequence.writeUInt32LE(vin.sequence, 0); | ||
| return Buffer.concat([hash, index, scriptLen, vin.script, sequence]); | ||
| }); | ||
| return Array.from(Buffer.concat(buffers)); | ||
| } | ||
|
|
||
| function serializeTxOutputs(outputs: TxOutput[]): number[] { | ||
| const buffers = outputs.map((vout) => { | ||
| const value = Buffer.alloc(8); | ||
| value.writeBigUInt64LE(BigInt(vout.value), 0); | ||
| const scriptLen = Buffer.from([vout.script.length]); | ||
| return Buffer.concat([value, scriptLen, vout.script]); | ||
| }); | ||
| return Array.from(Buffer.concat(buffers)); | ||
| } | ||
|
|
||
| export function serializeBtcTx(transaction: Transaction): SerializedBtcTx { | ||
| return { | ||
| version: serializeU32(transaction.version), | ||
| inputCount: transaction.ins.length, | ||
| inputs: serializeTxInputs(transaction.ins), | ||
| outputCount: transaction.outs.length, | ||
| outputs: serializeTxOutputs(transaction.outs), | ||
| lockTime: serializeU32(transaction.locktime), | ||
| }; | ||
| } | ||
This file contains hidden or 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,86 @@ | ||
| import { SuiClient as Client, getFullnodeUrl } from "@mysten/sui/client"; | ||
| import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"; | ||
| import { Transaction as SuiTransaction } from "@mysten/sui/transactions"; | ||
| import { Transaction } from "bitcoinjs-lib"; | ||
| import { serializeBtcTx } from "./btctx-serializer"; | ||
| import { ProofResult } from "./btcindexer"; | ||
|
|
||
| export interface SuiClientConfig { | ||
| suiNetwork: "testnet" | "mainnet" | "devnet"; | ||
| suiPackageId: string; | ||
| suiNbtcObjectId: string; | ||
| suiLightClientObjectId: string; | ||
| suiSignerMnemonic: string; | ||
| } | ||
|
|
||
| export class SuiClient { | ||
| private client: Client; | ||
| private signer: Ed25519Keypair; | ||
| private packageId: string; | ||
| private nbtcObjectId: string; | ||
| private lightClientObjectId: string; | ||
|
|
||
| constructor(config: SuiClientConfig) { | ||
| this.client = new Client({ url: getFullnodeUrl(config.suiNetwork) }); | ||
| this.signer = Ed25519Keypair.deriveKeypair(config.suiSignerMnemonic); | ||
| this.packageId = config.suiPackageId; | ||
| this.nbtcObjectId = config.suiNbtcObjectId; | ||
| this.lightClientObjectId = config.suiLightClientObjectId; | ||
| } | ||
|
|
||
| async mintNbtc( | ||
| transaction: Transaction, | ||
| blockHeight: number, | ||
| txIndex: number, | ||
| proof: ProofResult, | ||
| ): Promise<boolean> { | ||
| try { | ||
| const tx = new SuiTransaction(); | ||
| const target = `${this.packageId}::nbtc::mint` as const; | ||
|
|
||
| const serializedTx = serializeBtcTx(transaction); | ||
sczembor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| tx.moveCall({ | ||
| target: target, | ||
| arguments: [ | ||
| tx.object(this.nbtcObjectId), | ||
| tx.object(this.lightClientObjectId), | ||
| tx.pure.vector("u8", serializedTx.version), | ||
| tx.pure.u32(serializedTx.inputCount), | ||
| tx.pure.vector("u8", serializedTx.inputs), | ||
| tx.pure.u32(serializedTx.outputCount), | ||
| tx.pure.vector("u8", serializedTx.outputs), | ||
| tx.pure.vector("u8", serializedTx.lockTime), | ||
| tx.pure.vector( | ||
| "vector<u8>", | ||
| proof.proofPath.map((p) => Array.from(p)), | ||
| ), | ||
| tx.pure.u64(blockHeight), | ||
| tx.pure.u64(txIndex), | ||
| ], | ||
| }); | ||
|
|
||
| const result = await this.client.signAndExecuteTransaction({ | ||
| signer: this.signer, | ||
| transaction: tx, | ||
| options: { | ||
| showEffects: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (result.effects?.status.status === "success") { | ||
| console.log(`Mint successful. Digest: ${result.digest}`); | ||
| return true; | ||
| } else { | ||
| console.error( | ||
| `Mint failed. Digest: ${result.digest}:`, | ||
| result.effects?.status.error, | ||
| ); | ||
| return false; | ||
| } | ||
sczembor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| console.error(`Error during mint call`, error); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.