|
1 |
| -export const Blockchain = { |
| 1 | +import { NetworkModule } from "../types" |
| 2 | + |
| 3 | +interface BlockIdentifier { |
| 4 | + hash: ArrayBuffer, |
| 5 | + height: number, |
| 6 | +} |
| 7 | + |
| 8 | +// One or the other, not both |
| 9 | +interface SingleBlockQuery { |
| 10 | + hash?: ArrayBuffer, |
| 11 | + height?: number, |
| 12 | +} |
| 13 | + |
| 14 | +// One or the other, not both |
| 15 | +interface SingleTransactionQuery { |
| 16 | + hash?: ArrayBuffer, |
| 17 | + blockTxIdx?: [SingleBlockQuery, number], |
| 18 | +} |
| 19 | + |
| 20 | +interface TransactionIdentifier { |
| 21 | + hash: ArrayBuffer, |
| 22 | +} |
| 23 | + |
| 24 | +export interface BlockchainTransaction { |
| 25 | + transactionIdentifier: TransactionIdentifier, |
| 26 | + transactionRequest?: ArrayBuffer, |
| 27 | + transactionResponse?: ArrayBuffer, |
| 28 | + [key: string]: any, |
| 29 | +} |
| 30 | + |
| 31 | +export interface BlockchainBlock { |
| 32 | + blockIdentifier: BlockIdentifier, |
| 33 | + parentBlockIdentifier?: BlockIdentifier, |
| 34 | + appHash?: ArrayBuffer, |
| 35 | + timestamp?: number, |
| 36 | + totalTxs: number, |
| 37 | + transactions: BlockchainTransaction[], |
| 38 | + // 6 => [ * bstr ], NOT IMPLEMENTED |
| 39 | + [key: string]: any, |
| 40 | +} |
| 41 | + |
| 42 | +export interface BlockchainInfo { |
| 43 | + blockIdentifier: BlockIdentifier, |
| 44 | + serverStateHash?: ArrayBuffer, |
| 45 | + earliestQueryableHeight?: number, |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +interface Blockchain extends NetworkModule { |
| 50 | + _namespace_: string |
| 51 | + info: () => Promise<BlockchainInfo> |
| 52 | + block: (params: SingleBlockQuery) => Promise<BlockchainBlock> |
| 53 | + transaction: (params: SingleTransactionQuery) => Promise<BlockchainTransaction> |
| 54 | + request: (params: SingleTransactionQuery) => Promise<ArrayBuffer> |
| 55 | + response: (params: SingleTransactionQuery) => Promise<ArrayBuffer> |
| 56 | +} |
| 57 | + |
| 58 | +export const Blockchain: Blockchain = { |
2 | 59 | _namespace_: "blockchain",
|
3 |
| - async info(): Promise<unknown> { |
4 |
| - // @ts-ignore |
5 |
| - return await this.call("blockchain.info") |
| 60 | + |
| 61 | + // Tested to work |
| 62 | + async info(): Promise<BlockchainInfo> { |
| 63 | + const message = await this.call("blockchain.info") |
| 64 | + const payload = message.getPayload() |
| 65 | + return getBlockchainInfo(payload) |
| 66 | + }, |
| 67 | + |
| 68 | + // Tested to work |
| 69 | + async block(params: SingleBlockQuery): Promise<BlockchainBlock> { |
| 70 | + const cborParams = makeBlockParam(params) |
| 71 | + const message = await this.call("blockchain.block", cborParams) |
| 72 | + const payload = message.getPayload() |
| 73 | + return getBlockchainBlock(payload) |
6 | 74 | },
|
7 | 75 |
|
8 |
| - async block(): Promise<unknown> { |
9 |
| - // @ts-ignore |
10 |
| - return this.call("blockchain.block", {}) |
| 76 | + // Untested |
| 77 | + async transaction(params: SingleTransactionQuery): Promise<BlockchainTransaction> { |
| 78 | + const cborParams = makeTransactionParam(params) |
| 79 | + const message = await this.call("blockchain.transaction", cborParams) |
| 80 | + const payload = message.getPayload() |
| 81 | + return getBlockchainTransaction(payload) |
11 | 82 | },
|
12 | 83 |
|
13 |
| - async transaction(): Promise<unknown> { |
14 |
| - // @ts-ignore |
15 |
| - return await this.call("blockchain.transaction", new Map([[1, symbols]])) |
| 84 | + async request(_params: SingleTransactionQuery): Promise<ArrayBuffer> { |
| 85 | + throw new Error("Not implemented") |
16 | 86 | },
|
| 87 | + |
| 88 | + async response(_params: SingleTransactionQuery): Promise<ArrayBuffer> { |
| 89 | + throw new Error("Not implemented") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function getBlockchainInfo(data: Map<number, any>): BlockchainInfo { |
| 94 | + return { |
| 95 | + blockIdentifier: { |
| 96 | + hash: data.get(0)?.get(0), |
| 97 | + height: data.get(0)?.get(1), |
| 98 | + }, |
| 99 | + serverStateHash: data.get(1), |
| 100 | + earliestQueryableHeight: data.get(2), |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function getBlockchainBlock(data: Map<number, any>): BlockchainBlock { |
| 105 | + return { |
| 106 | + blockIdentifier: { |
| 107 | + hash: data.get(0)?.get(0)?.get(0), |
| 108 | + height: data.get(0)?.get(0)?.get(1), |
| 109 | + }, |
| 110 | + parentBlockIdentifier: { |
| 111 | + hash: data.get(0)?.get(1)?.get(0), |
| 112 | + height: data.get(0)?.get(1)?.get(1), |
| 113 | + }, |
| 114 | + appHash: data.get(0)?.get(2), |
| 115 | + timestamp: data.get(0)?.get(3), |
| 116 | + totalTxs: data.get(0)?.get(4), |
| 117 | + transactions: data.get(0)?.get(5).map((tx: Map<number, any>) => { |
| 118 | + return { |
| 119 | + transactionIdentifier: { |
| 120 | + hash: tx.get(0)?.get(0), |
| 121 | + }, |
| 122 | + transactionRequest: tx.get(1), |
| 123 | + transactionResponse: tx.get(2), |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function getBlockchainTransaction(data: Map<number, any>): BlockchainTransaction { |
| 130 | + return { |
| 131 | + transactionIdentifier: { |
| 132 | + hash: data.get(0)?.get(0), |
| 133 | + }, |
| 134 | + transactionRequest: data.get(1), |
| 135 | + transactionResponse: data.get(2), |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +function makeBlockParam(params: SingleBlockQuery) { |
| 140 | + const query = new Map<number, any>() |
| 141 | + if (params.hash) { |
| 142 | + query.set(0, params.hash) |
| 143 | + } else if (params.height) { |
| 144 | + query.set(1, params.height) |
| 145 | + } |
| 146 | + return new Map([[0, query]]) |
| 147 | +} |
| 148 | + |
| 149 | +function makeTransactionParam(params: SingleTransactionQuery) { |
| 150 | + const query = new Map<number, any>() |
| 151 | + if (params.hash) { |
| 152 | + query.set(0, params.hash) |
| 153 | + } else if (params.blockTxIdx) { |
| 154 | + query.set(1, [makeBlockParam(params.blockTxIdx[0]), params.blockTxIdx[1]]) |
| 155 | + } |
| 156 | + return new Map([[0, query]]) |
17 | 157 | }
|
0 commit comments