|
| 1 | +import * as t from 'io-ts'; |
| 2 | +import { isRight } from 'fp-ts/Either'; |
| 3 | +import { PathReporter } from 'io-ts/lib/PathReporter'; |
| 4 | +import waitFor from './waitFor'; |
| 5 | + |
| 6 | +const processResponse = async <T>(response: Response, decoder: t.Decoder<unknown, T>) : Promise<t.Validation<T>> => { |
| 7 | + if(!response.ok) { |
| 8 | + throw new Error(`Got response with status ${response.status}`) |
| 9 | + } |
| 10 | + |
| 11 | + const rawData = await response.json() |
| 12 | + return decoder.decode(rawData) |
| 13 | +} |
| 14 | + |
| 15 | +export type BroadcastInputs = { |
| 16 | + signature: string, |
| 17 | + recoveryId: number, |
| 18 | + message: string |
| 19 | +} |
| 20 | + |
| 21 | +const broadcastResult = t.type({ |
| 22 | + txhash: t.string |
| 23 | +}) |
| 24 | + |
| 25 | +export type BroadcastResult = t.TypeOf<typeof broadcastResult> |
| 26 | + |
| 27 | +export const broadcast = async (base: string, { message, recoveryId, signature }: BroadcastInputs): Promise<BroadcastResult> => { |
| 28 | + const response = await fetch(`${base}/broadcast`, { |
| 29 | + method: 'PUT', |
| 30 | + headers: { |
| 31 | + 'Content-Type': 'application/json', |
| 32 | + }, |
| 33 | + body: JSON.stringify({ |
| 34 | + message, |
| 35 | + signature, |
| 36 | + recovery_id: recoveryId |
| 37 | + }), |
| 38 | + }); |
| 39 | + |
| 40 | + const parsedData = await processResponse(response, broadcastResult) |
| 41 | + |
| 42 | + if(isRight(parsedData)) { |
| 43 | + return parsedData.right |
| 44 | + } else { |
| 45 | + throw new Error(`${PathReporter.report(parsedData)}`) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const nextNonce = t.type({ |
| 50 | + next_nonce: t.number, |
| 51 | +}) |
| 52 | + |
| 53 | +export const getNextNonce = async (base: string, kolmePublicKey: string) : Promise<number> => { |
| 54 | + const response = await fetch(`${base}/get-next-nonce?pubkey=${kolmePublicKey}`); |
| 55 | + |
| 56 | + const parsedData = await processResponse(response, nextNonce) |
| 57 | + |
| 58 | + if(isRight(parsedData)) { |
| 59 | + return parsedData.right.next_nonce |
| 60 | + } else { |
| 61 | + throw new Error(`${PathReporter.report(parsedData)}`) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function taggedJson<C extends t.Mixed>(inner: C) { |
| 66 | + return new t.Type< |
| 67 | + t.TypeOf<C>, // the decoded output |
| 68 | + string, // the encoded input (we accept a JSON string) |
| 69 | + unknown // overall unknown input |
| 70 | + >( |
| 71 | + `TaggedJson<${inner.name}>`, |
| 72 | + inner.is, |
| 73 | + (u, c) => { |
| 74 | + if (typeof u !== 'string') { |
| 75 | + return t.failure(u, c, 'expected a JSON‐string'); |
| 76 | + } |
| 77 | + let parsed: unknown; |
| 78 | + try { |
| 79 | + parsed = JSON.parse(u); |
| 80 | + } catch (_e) { |
| 81 | + return t.failure(u, c, 'invalid JSON string'); |
| 82 | + } |
| 83 | + // now validate the parsed object against `inner` |
| 84 | + return inner.validate(parsed, c); |
| 85 | + }, |
| 86 | + // when encoding back out, we simply JSON.stringify |
| 87 | + (a) => JSON.stringify(a), |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +const signedTaggedJson = <C extends t.Mixed>(inner: C) => |
| 92 | + t.type({ |
| 93 | + message: taggedJson(inner), |
| 94 | + signature: t.string, |
| 95 | + recovery_id: t.number, |
| 96 | + }); |
| 97 | + |
| 98 | +export const transactionCodec = t.type({ |
| 99 | + pubkey: t.string, |
| 100 | + nonce: t.number, |
| 101 | + created: t.string, |
| 102 | + messages: t.array(t.any), |
| 103 | + max_height: t.union([t.number, t.null, t.undefined]), |
| 104 | +}); |
| 105 | + |
| 106 | +const blockCodec = t.type({ |
| 107 | + tx: signedTaggedJson(transactionCodec), |
| 108 | + timestamp: t.string, |
| 109 | + processor: t.string, |
| 110 | + height: t.number, |
| 111 | + parent: t.string, |
| 112 | + framework_state: t.string, |
| 113 | + app_state: t.string, |
| 114 | + logs: t.string, |
| 115 | + loads: t.array(t.type({ request: t.string, response: t.string })), |
| 116 | +}); |
| 117 | + |
| 118 | +const block = t.type({ |
| 119 | + blockhash: t.string, |
| 120 | + txhash: t.string, |
| 121 | + block: signedTaggedJson(blockCodec), |
| 122 | + logs: t.array(t.array(t.string)) |
| 123 | +}) |
| 124 | + |
| 125 | +export type Block = t.TypeOf<typeof block> |
| 126 | + |
| 127 | +export const loadBlock = async (base: string, txHash: string): Promise<Block> => { |
| 128 | + const response = await fetch(`${base}/block-by-tx-hash/${txHash}`); |
| 129 | + |
| 130 | + const parsedData = await processResponse(response, block) |
| 131 | + |
| 132 | + if(isRight(parsedData)) { |
| 133 | + return parsedData.right |
| 134 | + } else { |
| 135 | + throw new Error(`${PathReporter.report(parsedData)}`) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export const waitForTx = async(base: string, txHash: string) : Promise<Block> => { |
| 140 | + let attempt = 1 |
| 141 | + const before = Date.now() |
| 142 | + |
| 143 | + while(attempt < 100) { |
| 144 | + try { |
| 145 | + return await loadBlock(base, txHash) |
| 146 | + } catch { |
| 147 | + const nextWaitTime = 1000 + (Math.floor(attempt / 10) * 1000) |
| 148 | + await waitFor(nextWaitTime) |
| 149 | + } |
| 150 | + |
| 151 | + attempt += 1 |
| 152 | + } |
| 153 | + |
| 154 | + const after = Date.now() |
| 155 | + |
| 156 | + throw new Error(`Kolme block with tx ${txHash} did not appear on the side chain after ${(after - before) / 1000} seconds`) |
| 157 | +} |
| 158 | + |
| 159 | +export class WithBase { |
| 160 | + base: string; |
| 161 | + |
| 162 | + constructor(base: string) { |
| 163 | + this.base = base; |
| 164 | + } |
| 165 | + |
| 166 | + async getNextNonce(kolmePublicKey: string): Promise<number> { |
| 167 | + return getNextNonce(this.base, kolmePublicKey) |
| 168 | + } |
| 169 | + |
| 170 | + async broadcast(inputs: BroadcastInputs): Promise<BroadcastResult> { |
| 171 | + return broadcast(this.base, inputs) |
| 172 | + } |
| 173 | + |
| 174 | + async loadBlock(txHash: string): Promise<Block> { |
| 175 | + return loadBlock(this.base, txHash) |
| 176 | + } |
| 177 | + |
| 178 | + async waitForTx(txHash: string): Promise<Block> { |
| 179 | + return waitForTx(this.base, txHash) |
| 180 | + } |
| 181 | +} |
0 commit comments