Skip to content
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

Draft: WIP Ergopay bridge #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"blakejs": "^1.1.0",
"bs58": "^4.0.1",
"crypto-js": "^4.0.0",
"ergo-lib-wasm-browser": "0.14.0",
"ergo-lib-wasm-nodejs": "0.14.0",
"ergo-lib-wasm-browser": "^0.15.0-alpha-c308476",
"ergo-lib-wasm-nodejs": "^0.15.0-alpha-c308476",
"json-bigint": "^1.0.0",
"mathjs": "^9.4.4",
"ramda": "0.27.1"
Expand Down
26 changes: 26 additions & 0 deletions src/entities/blockHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type BlockPowSolutions = {
pk: string
w: string
n: string
d: number
}

export type BlockHeader = {
timestamp: bigint
height: number
nBits: number
version: number
extensionId: string
difficulty: string
votes: string
size: string
stateRoot: string
id: string
adProofsRoot: string
transactionsRoot: string
extensionHash: string
powSolutions: BlockPowSolutions
adProofsId: string
transactionsId: string
parentId: string
}
14 changes: 14 additions & 0 deletions src/entities/blockSummary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export type BlockSummary = {
id: string
height: number
epoch: number
version: number
timestamp: bigint
transactionsCount: number
miner: {
address: string
name: string
}
size: number
difficulty: bigint
}
28 changes: 27 additions & 1 deletion src/ergoWasmInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import {
ErgoBoxCandidate as WasmErgoBoxCandidate,
ErgoBox as WasmErgoBox,
ErgoTree as WasmErgoTree,
Token as WasmToken
Token as WasmToken,
ErgoStateContext,
PreHeader,
ReducedTransaction
} from "ergo-lib-wasm-browser"
import {BlockHeader} from "./entities/blockHeader"

export function txRequestToWasmTransaction(req: TxRequest, ctx: NetworkContext): UnsignedTransaction {
const inputs = boxSelectionToWasm(req.inputs)
Expand All @@ -29,6 +33,28 @@ export function txRequestToWasmTransaction(req: TxRequest, ctx: NetworkContext):
return txb.build()
}

export function blockHeadersToErgoStateContext(blockHeaders: BlockHeader[]): ErgoStateContext {
const block_headers = RustModule.SigmaRust.BlockHeaders.from_json(blockHeaders.slice(0, 10))
const pre_header = PreHeader.from_block_header(block_headers.get(0))
return new RustModule.SigmaRust.ErgoStateContext(pre_header, block_headers)
}

export function txRequestToWasmReducedTx(
req: TxRequest,
ergoStateCtx: ErgoStateContext,
ctx: NetworkContext
): ReducedTransaction {
const unsignedTx = txRequestToWasmTransaction(req, ctx)
const unspent_boxes = RustModule.SigmaRust.ErgoBoxes.from_boxes_json(req.inputs.inputs)
const tx_data_inputs = RustModule.SigmaRust.ErgoBoxes.from_boxes_json(req.dataInputs)
return RustModule.SigmaRust.ReducedTransaction.from_unsigned_tx(
unsignedTx,
unspent_boxes,
tx_data_inputs,
ergoStateCtx
)
}

export function boxSelectionToWasm(inputs: BoxSelection): WasmBoxSelection {
const boxes = new RustModule.SigmaRust.ErgoBoxes(boxToWasm(inputs.inputs[0]))
const tokens = new RustModule.SigmaRust.Tokens()
Expand Down
39 changes: 38 additions & 1 deletion src/network/ergoNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ import {
ExplorerBalance,
explorerBalanceToWallet,
ExplorerErgoUTx,
explorerUtxToErgoTx
explorerUtxToErgoTx,
ExplorerBlockHeader,
explorerBlockSummariesToBlockSummaries,
ExplorerBlockSummary,
Items
} from "./models"
import {Sorting} from "./sorting"
import {JSONBI} from "../utils/json"
import {TokenSymbol} from "../types"
import {Balance} from "../wallet/entities/balance"
import {BlockHeader} from "../entities/blockHeader"

export interface ErgoNetwork {
/** Get confirmed transaction by id.
Expand Down Expand Up @@ -119,6 +124,28 @@ export class Explorer implements ErgoNetwork {
.then(res => explorerToErgoBox(res.data))
}

async getBlockHeaderByHeaderId(headerId: string): Promise<BlockHeader | undefined> {
return await this.backend
.request<ExplorerBlockHeader>({
url: `/api/v1/blocks/${headerId}`,
transformResponse: data => JSONBI.parse(data)
})
.then(res => network.explorerBlockHeaderToBlockHeader(res.data))
}

async getBlockHeaders(): Promise<Array<BlockHeader | undefined>> {
const blockSummaries = await this.backend
.request<Items<ExplorerBlockSummary>>({
url: `/api/v1/blocks`,
transformResponse: data => JSONBI.parse(data)
})
.then(res => explorerBlockSummariesToBlockSummaries(res.data))

if (!blockSummaries) return []

return Promise.all(blockSummaries.map(({id}) => this.getBlockHeaderByHeaderId(id)))
}

async getBalanceByAddress(address: Address): Promise<Balance | undefined> {
return this.backend
.request<ExplorerBalance>({
Expand All @@ -138,6 +165,16 @@ export class Explorer implements ErgoNetwork {
.then(res => [res.data.items.map(tx => explorerToErgoTx(tx)), res.data.total])
}

async getUnspentBoxesByAddress(address: Address, paging: Paging): Promise<AugErgoBox[]> {
return this.backend
.request<network.Items<network.ExplorerErgoBox>>({
url: `/api/v1/boxes/unspent/byAddress/${address}`,
params: paging,
transformResponse: data => JSONBI.parse(data)
})
.then(res => res.data.items.map(b => network.explorerToErgoBox(b)))
}

async getUTxsByAddress(address: Address, paging: Paging): Promise<[AugErgoTx[], number]> {
return this.backend
.request<network.Items<ExplorerErgoUTx>>({
Expand Down
84 changes: 84 additions & 0 deletions src/network/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
TokenId,
TxId
} from "../"
import {BlockHeader} from "../entities/blockHeader"
import {BlockSummary} from "../entities/blockSummary"
import {DataInput} from "../entities/dataInput"
import {parseRegisterId} from "../entities/registers"
import {Balance} from "../wallet/entities/balance"
Expand Down Expand Up @@ -80,6 +82,88 @@ export function explorerBalanceToWallet(b: ExplorerBalance): Balance {
)
}

export type ExplorerUnspentErgoBox = {
id: string
txId: string
mainChain: boolean
value: bigint
index: number
creationHeight: number
ergoTree: string
address: string
assets: BoxAsset[]
additionalRegisters: {[key: string]: BoxRegister}
spentTransactionId?: string | null
}

// const errr = {"id":"962b8bb807115545482f8af97ba34e89bbc970b7978699ababb3a1650d94688a","height":673747,"epoch":657,"version":2,"timestamp":1643366279734,"transactionsCount":5,"miner":{"address":"88dhgzEuTXaRxf1rbqBRZ6Zbw9iigdB4PCdjyFKLrk22gnmjKcxZBe53vqJVetRa4tTNF9oowQWPp2c6","name":"wQWPp2c6"},"size":27215,"difficulty":1859476026032128,"minerReward":66000000000}
// https://api.ergoplatform.com/api/v1/blocks
export type ExplorerBlockSummary = {
id: string
height: number
epoch: number
version: number
timestamp: bigint
transactionsCount: number
miner: {
address: string
name: string
}
size: number
difficulty: bigint
}

export function explorerBlockSummaryToBlockSummary(summary: ExplorerBlockSummary): BlockSummary {
return {
...summary
}
}

export function explorerBlockSummariesToBlockSummaries(
summaryItems: Items<ExplorerBlockSummary>
): BlockSummary[] {
return summaryItems.items.map(explorerBlockSummaryToBlockSummary)
}

export type ExplorerBlockHeader = {
timestamp: bigint
height: number
nBits: number
version: number
epoch: number
difficulty: bigint
size: number
votes: number[]
extensionId: string
stateRoot: string
id: string
adProofsRoot: string
transactionsRoot: string
extensionHash: string
powSolutions: {
pk: string
w: string
n: string
d: string
}
adProofsId: string
transactionsId: string
parentId: string
}

export function explorerBlockHeaderToBlockHeader(blockHeader: ExplorerBlockHeader): BlockHeader {
return {
...blockHeader,
difficulty: blockHeader.difficulty.toString(),
votes: blockHeader.votes.map(voteByte => voteByte.toString(16).padStart(2, "0")).join(""),
size: blockHeader.size.toString(),
powSolutions: {
...blockHeader.powSolutions,
d: parseInt(blockHeader.powSolutions.d, 10)
}
}
}

export type ExplorerErgoTx = {
readonly id: TxId
readonly inputs: ExplorerInput[]
Expand Down
Loading