|
1 | 1 | import http from "http";
|
2 | 2 | import type { AddressInfo } from "net";
|
3 |
| -import test, { Test } from "tape-promise/tape"; |
| 3 | +import "jest-extended"; |
4 | 4 | import { v4 as uuidv4 } from "uuid";
|
5 | 5 | import express from "express";
|
6 | 6 | import bodyParser from "body-parser";
|
@@ -39,186 +39,173 @@ const connectorId = uuidv4();
|
39 | 39 | const logLevel: LogLevelDesc = "INFO";
|
40 | 40 | const testCase = "Test get invalid single status";
|
41 | 41 |
|
42 |
| -test("BEFORE " + testCase, async (t: Test) => { |
43 |
| - const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
44 |
| - await t.doesNotReject(pruning, "Pruning did not throw OK"); |
45 |
| - t.end(); |
46 |
| -}); |
47 |
| - |
48 |
| -test(testCase, async (t: Test) => { |
49 |
| - t.comment("Starting Besu Test Ledger"); |
| 42 | +describe(testCase, () => { |
50 | 43 | const besuTestLedger = new BesuTestLedger({ logLevel });
|
51 | 44 |
|
52 |
| - test.onFinish(async () => { |
| 45 | + const expressApp = express(); |
| 46 | + expressApp.use(bodyParser.json({ limit: "250mb" })); |
| 47 | + const server = http.createServer(expressApp); |
| 48 | + const listenOptions: IListenOptions = { |
| 49 | + hostname: "0.0.0.0", |
| 50 | + port: 0, |
| 51 | + server, |
| 52 | + }; |
| 53 | + |
| 54 | + beforeAll(async () => { |
| 55 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 56 | + await expect(pruning).resolves.toBeTruthy(); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(async () => await Servers.shutdown(server)); |
| 60 | + |
| 61 | + afterAll(async () => { |
53 | 62 | await besuTestLedger.stop();
|
54 | 63 | await besuTestLedger.destroy();
|
55 | 64 | await pruneDockerAllIfGithubAction({ logLevel });
|
56 | 65 | });
|
57 | 66 |
|
58 |
| - await besuTestLedger.start(); |
59 |
| - |
60 |
| - const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); |
61 |
| - const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); |
62 |
| - const firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey(); |
63 |
| - const privateKey = besuTestLedger.getGenesisAccountPrivKey(); |
64 |
| - const web3SigningCredential: Web3SigningCredential = { |
65 |
| - ethAccount: firstHighNetWorthAccount, |
66 |
| - secret: privateKey, |
67 |
| - type: Web3SigningCredentialType.PrivateKeyHex, |
68 |
| - } as Web3SigningCredential; |
69 |
| - |
70 |
| - const fakeWeb3SigningCredential: Web3SigningCredential = { |
71 |
| - ethAccount: "fakeAccount", |
72 |
| - secret: privateKey, |
73 |
| - type: Web3SigningCredentialType.PrivateKeyHex, |
74 |
| - } as Web3SigningCredential; |
75 |
| - const keychainId = uuidv4(); |
76 |
| - const keychainPlugin = new PluginKeychainMemory({ |
77 |
| - instanceId: uuidv4(), |
78 |
| - keychainId, |
79 |
| - // pre-provision keychain with mock backend holding the private key of the |
80 |
| - // test account that we'll reference while sending requests with the |
81 |
| - // signing credential pointing to this keychain entry. |
82 |
| - backend: new Map([ |
83 |
| - [DemoHelperJSON.contractName, JSON.stringify(DemoHelperJSON)], |
84 |
| - ]), |
85 |
| - logLevel, |
| 67 | + afterAll(async () => { |
| 68 | + const pruning = pruneDockerAllIfGithubAction({ logLevel }); |
| 69 | + await expect(pruning).resolves.toBeTruthy(); |
86 | 70 | });
|
87 |
| - keychainPlugin.set( |
88 |
| - HashTimeLockJson.contractName, |
89 |
| - JSON.stringify(HashTimeLockJson), |
90 |
| - ); |
91 | 71 |
|
92 |
| - const factory = new PluginFactoryLedgerConnector({ |
93 |
| - pluginImportType: PluginImportType.Local, |
| 72 | + beforeAll(async () => { |
| 73 | + await besuTestLedger.start(); |
94 | 74 | });
|
95 | 75 |
|
96 |
| - const pluginRegistry = new PluginRegistry({}); |
97 |
| - const connector: PluginLedgerConnectorBesu = await factory.create({ |
98 |
| - rpcApiHttpHost, |
99 |
| - rpcApiWsHost, |
100 |
| - logLevel, |
101 |
| - instanceId: connectorId, |
102 |
| - pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), |
103 |
| - }); |
| 76 | + test(testCase, async () => { |
| 77 | + const rpcApiHttpHost = await besuTestLedger.getRpcApiHttpHost(); |
| 78 | + const rpcApiWsHost = await besuTestLedger.getRpcApiWsHost(); |
| 79 | + const firstHighNetWorthAccount = besuTestLedger.getGenesisAccountPubKey(); |
| 80 | + const privateKey = besuTestLedger.getGenesisAccountPrivKey(); |
| 81 | + const web3SigningCredential: Web3SigningCredential = { |
| 82 | + ethAccount: firstHighNetWorthAccount, |
| 83 | + secret: privateKey, |
| 84 | + type: Web3SigningCredentialType.PrivateKeyHex, |
| 85 | + } as Web3SigningCredential; |
| 86 | + |
| 87 | + const fakeWeb3SigningCredential: Web3SigningCredential = { |
| 88 | + ethAccount: "fakeAccount", |
| 89 | + secret: privateKey, |
| 90 | + type: Web3SigningCredentialType.PrivateKeyHex, |
| 91 | + } as Web3SigningCredential; |
| 92 | + const keychainId = uuidv4(); |
| 93 | + const keychainPlugin = new PluginKeychainMemory({ |
| 94 | + instanceId: uuidv4(), |
| 95 | + keychainId, |
| 96 | + // pre-provision keychain with mock backend holding the private key of the |
| 97 | + // test account that we'll reference while sending requests with the |
| 98 | + // signing credential pointing to this keychain entry. |
| 99 | + backend: new Map([ |
| 100 | + [DemoHelperJSON.contractName, JSON.stringify(DemoHelperJSON)], |
| 101 | + ]), |
| 102 | + logLevel, |
| 103 | + }); |
| 104 | + keychainPlugin.set( |
| 105 | + HashTimeLockJson.contractName, |
| 106 | + JSON.stringify(HashTimeLockJson), |
| 107 | + ); |
104 | 108 |
|
105 |
| - pluginRegistry.add(connector); |
106 |
| - const pluginOptions: IPluginHtlcEthBesuOptions = { |
107 |
| - logLevel, |
108 |
| - instanceId: uuidv4(), |
109 |
| - pluginRegistry, |
110 |
| - }; |
| 109 | + const factory = new PluginFactoryLedgerConnector({ |
| 110 | + pluginImportType: PluginImportType.Local, |
| 111 | + }); |
111 | 112 |
|
112 |
| - const factoryHTLC = new PluginFactoryHtlcEthBesu({ |
113 |
| - pluginImportType: PluginImportType.Local, |
114 |
| - }); |
| 113 | + const pluginRegistry = new PluginRegistry({}); |
| 114 | + const connector: PluginLedgerConnectorBesu = await factory.create({ |
| 115 | + rpcApiHttpHost, |
| 116 | + rpcApiWsHost, |
| 117 | + logLevel, |
| 118 | + instanceId: connectorId, |
| 119 | + pluginRegistry: new PluginRegistry({ plugins: [keychainPlugin] }), |
| 120 | + }); |
115 | 121 |
|
116 |
| - const pluginHtlc = await factoryHTLC.create(pluginOptions); |
117 |
| - pluginRegistry.add(pluginHtlc); |
| 122 | + pluginRegistry.add(connector); |
| 123 | + const pluginOptions: IPluginHtlcEthBesuOptions = { |
| 124 | + logLevel, |
| 125 | + instanceId: uuidv4(), |
| 126 | + pluginRegistry, |
| 127 | + }; |
118 | 128 |
|
119 |
| - const expressApp = express(); |
120 |
| - expressApp.use(bodyParser.json({ limit: "250mb" })); |
121 |
| - const server = http.createServer(expressApp); |
122 |
| - const listenOptions: IListenOptions = { |
123 |
| - hostname: "0.0.0.0", |
124 |
| - port: 0, |
125 |
| - server, |
126 |
| - }; |
127 |
| - const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; |
128 |
| - test.onFinish(async () => await Servers.shutdown(server)); |
129 |
| - const { address, port } = addressInfo; |
130 |
| - const apiHost = `http://${address}:${port}`; |
131 |
| - |
132 |
| - const configuration = new Configuration({ basePath: apiHost }); |
133 |
| - const api = new BesuApi(configuration); |
134 |
| - |
135 |
| - await pluginHtlc.getOrCreateWebServices(); |
136 |
| - await pluginHtlc.registerWebServices(expressApp); |
137 |
| - |
138 |
| - const web3 = new Web3(rpcApiHttpHost); |
139 |
| - |
140 |
| - t.comment("Deploys HashTimeLock via .json file on initialize function"); |
141 |
| - const initRequest: InitializeRequest = { |
142 |
| - connectorId, |
143 |
| - keychainId, |
144 |
| - constructorArgs: [], |
145 |
| - web3SigningCredential, |
146 |
| - gas: DataTest.estimated_gas, |
147 |
| - }; |
148 |
| - const deployOut = await pluginHtlc.initialize(initRequest); |
149 |
| - t.ok( |
150 |
| - deployOut.transactionReceipt, |
151 |
| - "pluginHtlc.initialize() output.transactionReceipt is truthy OK", |
152 |
| - ); |
153 |
| - t.ok( |
154 |
| - deployOut.transactionReceipt.contractAddress, |
155 |
| - "pluginHtlc.initialize() output.transactionReceipt.contractAddress is truthy OK", |
156 |
| - ); |
157 |
| - const hashTimeLockAddress = deployOut.transactionReceipt |
158 |
| - .contractAddress as string; |
159 |
| - |
160 |
| - //Deploy DemoHelpers |
161 |
| - t.comment("Deploys DemoHelpers via .json file on deployContract function"); |
162 |
| - const deployOutDemo = await connector.deployContract({ |
163 |
| - contractName: DemoHelperJSON.contractName, |
164 |
| - contractAbi: DemoHelperJSON.abi, |
165 |
| - bytecode: DemoHelperJSON.bytecode, |
166 |
| - web3SigningCredential, |
167 |
| - keychainId, |
168 |
| - constructorArgs: [], |
169 |
| - gas: DataTest.estimated_gas, |
170 |
| - }); |
171 |
| - t.ok(deployOutDemo, "deployContract() output is truthy OK"); |
172 |
| - t.ok( |
173 |
| - deployOutDemo.transactionReceipt, |
174 |
| - "deployContract() output.transactionReceipt is truthy OK", |
175 |
| - ); |
176 |
| - t.ok( |
177 |
| - deployOutDemo.transactionReceipt.contractAddress, |
178 |
| - "deployContract() output.transactionReceipt.contractAddress is truthy OK", |
179 |
| - ); |
180 |
| - |
181 |
| - t.comment("Get account balance"); |
182 |
| - const balance = await web3.eth.getBalance(firstHighNetWorthAccount); |
183 |
| - |
184 |
| - t.comment("Create new contract for HTLC"); |
185 |
| - const bodyObj: NewContractObj = { |
186 |
| - contractAddress: hashTimeLockAddress, |
187 |
| - inputAmount: 10, |
188 |
| - outputAmount: 0x04, |
189 |
| - expiration: DataTest.expiration, |
190 |
| - hashLock: DataTest.hashLock, |
191 |
| - receiver: DataTest.receiver, |
192 |
| - outputNetwork: "BTC", |
193 |
| - outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", |
194 |
| - connectorId: connectorId, |
195 |
| - web3SigningCredential, |
196 |
| - keychainId, |
197 |
| - gas: DataTest.estimated_gas, |
198 |
| - }; |
199 |
| - const resp = await api.newContractV1(bodyObj); |
200 |
| - t.ok(resp, "response newContract is OK"); |
201 |
| - t.equal(resp.status, 200, "response status newContract is OK"); |
202 |
| - |
203 |
| - t.comment("Get single status of HTLC"); |
204 |
| - const balance2 = await web3.eth.getBalance(firstHighNetWorthAccount); |
205 |
| - |
206 |
| - t.equal( |
207 |
| - parseInt(balance), |
208 |
| - parseInt(balance2) - 10, |
209 |
| - "Balance of account is OK", |
210 |
| - ); |
211 |
| - try { |
212 |
| - const fakeId = "0x66616b654964"; |
213 |
| - const res = await api.getSingleStatusV1({ |
214 |
| - id: fakeId, |
215 |
| - web3SigningCredential: fakeWeb3SigningCredential, |
| 129 | + const factoryHTLC = new PluginFactoryHtlcEthBesu({ |
| 130 | + pluginImportType: PluginImportType.Local, |
| 131 | + }); |
| 132 | + |
| 133 | + const pluginHtlc = await factoryHTLC.create(pluginOptions); |
| 134 | + pluginRegistry.add(pluginHtlc); |
| 135 | + |
| 136 | + const addressInfo = (await Servers.listen(listenOptions)) as AddressInfo; |
| 137 | + const { address, port } = addressInfo; |
| 138 | + const apiHost = `http://${address}:${port}`; |
| 139 | + |
| 140 | + const configuration = new Configuration({ basePath: apiHost }); |
| 141 | + const api = new BesuApi(configuration); |
| 142 | + |
| 143 | + await pluginHtlc.getOrCreateWebServices(); |
| 144 | + await pluginHtlc.registerWebServices(expressApp); |
| 145 | + |
| 146 | + const web3 = new Web3(rpcApiHttpHost); |
| 147 | + |
| 148 | + const initRequest: InitializeRequest = { |
216 | 149 | connectorId,
|
217 |
| - keychainId: "", |
| 150 | + keychainId, |
| 151 | + constructorArgs: [], |
| 152 | + web3SigningCredential, |
| 153 | + gas: DataTest.estimated_gas, |
| 154 | + }; |
| 155 | + const deployOut = await pluginHtlc.initialize(initRequest); |
| 156 | + expect(deployOut.transactionReceipt).toBeTruthy(); |
| 157 | + expect(deployOut.transactionReceipt.contractAddress).toBeTruthy(); |
| 158 | + const hashTimeLockAddress = deployOut.transactionReceipt |
| 159 | + .contractAddress as string; |
| 160 | + |
| 161 | + //Deploy DemoHelpers |
| 162 | + const deployOutDemo = await connector.deployContract({ |
| 163 | + contractName: DemoHelperJSON.contractName, |
| 164 | + contractAbi: DemoHelperJSON.abi, |
| 165 | + bytecode: DemoHelperJSON.bytecode, |
| 166 | + web3SigningCredential, |
| 167 | + keychainId, |
| 168 | + constructorArgs: [], |
| 169 | + gas: DataTest.estimated_gas, |
218 | 170 | });
|
219 |
| - t.equal(res.status, 500, "response status is 500"); |
220 |
| - } catch (e) { |
221 |
| - t.equal(e.response.status, 500); |
222 |
| - } |
223 |
| - t.end(); |
| 171 | + expect(deployOutDemo).toBeTruthy(); |
| 172 | + expect(deployOutDemo.transactionReceipt).toBeTruthy(); |
| 173 | + expect(deployOutDemo.transactionReceipt.contractAddress).toBeTruthy(); |
| 174 | + |
| 175 | + const balance = await web3.eth.getBalance(firstHighNetWorthAccount); |
| 176 | + |
| 177 | + const bodyObj: NewContractObj = { |
| 178 | + contractAddress: hashTimeLockAddress, |
| 179 | + inputAmount: 10, |
| 180 | + outputAmount: 0x04, |
| 181 | + expiration: DataTest.expiration, |
| 182 | + hashLock: DataTest.hashLock, |
| 183 | + receiver: DataTest.receiver, |
| 184 | + outputNetwork: "BTC", |
| 185 | + outputAddress: "1AcVYm7M3kkJQH28FXAvyBFQzFRL6xPKu8", |
| 186 | + connectorId: connectorId, |
| 187 | + web3SigningCredential, |
| 188 | + keychainId, |
| 189 | + gas: DataTest.estimated_gas, |
| 190 | + }; |
| 191 | + const resp = await api.newContractV1(bodyObj); |
| 192 | + expect(resp).toBeTruthy(); |
| 193 | + expect(resp.status).toEqual(200); |
| 194 | + |
| 195 | + const balance2 = await web3.eth.getBalance(firstHighNetWorthAccount); |
| 196 | + |
| 197 | + expect(parseInt(balance)).toEqual(parseInt(balance2) - 10); |
| 198 | + try { |
| 199 | + const fakeId = "0x66616b654964"; |
| 200 | + const res = await api.getSingleStatusV1({ |
| 201 | + id: fakeId, |
| 202 | + web3SigningCredential: fakeWeb3SigningCredential, |
| 203 | + connectorId, |
| 204 | + keychainId: "", |
| 205 | + }); |
| 206 | + expect(res.status).toEqual(500); |
| 207 | + } catch (e: any) { |
| 208 | + expect(e.response.status).toEqual(500); |
| 209 | + } |
| 210 | + }); |
224 | 211 | });
|
0 commit comments