-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebapp.ts
More file actions
185 lines (136 loc) · 5.21 KB
/
webapp.ts
File metadata and controls
185 lines (136 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Collector, Predicate, TraderApi, TraderStorage, traderApi } from './src/client-api/trader-api';
import { Neighbor, p2pNode, startP2P } from './src/p2p';
import { OracleId, OracleCapability, OfferMsg, Report, PagingDescriptor, Commitment, Fact, FactRequest, HashCashPow, Offer, OfferTerms } from './src/protocol';
import { Api, FacilitatorNode, api as ndapi} from './src/api';
import * as btc from "./src/client-api/contracts/generate-btc-tx";
import { IDBPDatabase, openDB } from 'idb';
import { HashLockProvider, hashLockProvider, matchingEngine } from './src-web/matching';
import { p2pktr } from './src/client-api/contracts/btc/tx';
import { stalkingEngine, StalkingEngine } from './src-web/stalking';
import { browserPeerAPI } from './src/p2p-webrtc';
import { TraderQuery, database, Storage, indexDBstorage } from './src-web/impl/storage';
import { cfg, configureWebMocks, nodeMock, pub1, pub2, pubRandom } from './webcfg';
import { dataProvider } from './src-web/oracle-data-provider';
import { btcDlcContractInterpreter, UTxO } from './src-web/transactions';
import Sandbox from '@nyariv/sandboxjs';
import { Dsl } from './src-web/dsl';
import { MatchingEngine } from './src-web/matching-api';
export interface BtcApi {
generateDlcContract:(params: btc.DlcParams) => Promise<btc.DlcContract>
generateChildDlcContract:(params: btc.ChildDlcParams) => Promise<btc.DlcContract>
generateClosingTransaction: (P: btc.ClosingParams) => Promise<btc.Hex>
generateCetRedemptionTransaction: (p: btc.CetRedemptionParams) => Promise<btc.Hex>
}
declare global {
interface Window {
user: string
traderApi: TraderApi<TraderQuery<OracleId>, TraderQuery<OracleCapability>, boolean>
storage: Storage
btc: BtcApi
matching: MatchingEngine
stalking: StalkingEngine
txfee: number
pool: Api
spec: string
address: string
pubkey: string
privateDB: IDBPDatabase<unknown> //security note: secrts will be shared across pages in origin (subdomain, e.g. dk14.github.io)
webOracleFacts: IDBPDatabase<unknown>
hashLockProvider: HashLockProvider
evalDiscreet: (expression: string, parties: string[], bounds: [number, number][]) => Promise<any>
test: boolean
peerlist: string[]
initWebapp: Promise<void>
progressOffers: () => Promise<void>
profiledb: IDBPDatabase<unknown>
spentUtxos: UTxO[]
spentUtxosMemoize: {[id: string]: UTxO[]}
}
}
window.txfee = 2000
const initWebapp = new Promise<void>(async (resolve) => {
window.spec = await (await fetch("./../mega-peers-spec.yaml")).text()
global.cfg = cfg
//PROFILE
window.profiledb = await openDB('profile', 1, {
upgrade(db) {
db.createObjectStore('xpub');
db.createObjectStore('preferences');
},
});
window.user = "default"
try {
const urlParams = new URLSearchParams(window.location.search);
const param = urlParams.get('user');
if (param) {
window.user = param
}
} catch {
}
let xpub: string = await window.profiledb.get("xpub", window.user)
if (!xpub) {
if (window.user === 'alice') {
xpub = pub1
} else if (window.user === 'bob') {
xpub = pub2
} else {
xpub = pubRandom
}
try {
await window.profiledb.put("xpub", xpub, window.user)
} catch {
}
}
window.address = p2pktr(xpub).address
window.pubkey = xpub
//WALLETT
window.privateDB = await openDB('private', 1, {
upgrade(db) {
db.createObjectStore('secrets');
},
});
//LOCAL ORACLE
window.webOracleFacts = await openDB('web-oracle', 1, {
upgrade(db) {
db.createObjectStore('answers');
},
})
window.pool = ndapi
if (!global.isTest) {
const api = await startP2P(global.cfg, await browserPeerAPI())
setInterval(() => {
window.peerlist = api.peers.map(x => x.addr.server)
})
}
const store = indexDBstorage(await database())
window.traderApi = traderApi(cfg.trader, cfg, ndapi, store, p2pNode)
window.storage = store
window.hashLockProvider = hashLockProvider
window.btc = {
generateClosingTransaction: btc.generateClosingTransaction,
generateCetRedemptionTransaction: btc.generateCetRedemptionTransaction,
generateDlcContract: btc.generateDlcContract,
generateChildDlcContract: btc.generateChildDlcContract
}
window.matching = matchingEngine
window.stalking = stalkingEngine
window.progressOffers = async () => {
await window.stalking.trackIssuedOffers({
"bitcoin-testnet": btcDlcContractInterpreter
}, dataProvider)
}
await configureWebMocks()
window.pool = ndapi
if (!global.isTest && !window.test) {
startP2P(global.cfg, await browserPeerAPI())
}
console.log("WebAPI is ready!")
resolve()
})
global.initWebapp = initWebapp
window.initWebapp = initWebapp
const ev = new CustomEvent<string>('init-webapp',{ bubbles: true, cancelable: false });
try {
window.document.body.dispatchEvent(ev)
} catch {
}