forked from machinomy/machinomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
97 lines (83 loc) · 2.78 KB
/
client.ts
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
/**
* To run the file, it requires two environment variables to be set.
* `RPC_URL` is a JSON RPC endpoint. Alchemy works just fine. For Polygon Amoy test network,
* you could set it to `RPC_URL=https://rpc-amoy.polygon.technology`. Another variable is `MNEMONIC`.
* It is a [12-word seed phrase](https://github.com/pirapira/ethereum-word-list/blob/master/README.md#mnemonic-phrase).
* For example, `MNEMONIC=tool school decrease elegant fix awful eyebrow immense noble erase dish labor`
*
* Start this file then:
*
* yarn build
* PROVIDER_URL="https://rpc-amoy.polygon.technology" MNEMONIC="tool school decrease elegant fix awful eyebrow immense noble erase dish labor" node client.js
*
* It will open a channel towards the server side, and send a single payment.
*
* The server side (the Hub) for selling the content is provided in `hub.ts` file.
*/
import { IohTee } from '@riaskov/iohtee'
import path from 'path'
import { mnemonicToAccount } from 'viem/accounts'
async function main(): Promise<string> {
const MNEMONIC = String(process.env.ACCOUNT_MNEMONIC).trim()
const RPC_URL = String(process.env.RPC_URL).trim()
const CHAIN_ID = Number(process.env.CHAIN_ID)
const TARGET = 'https://play.iohtee.toivo.tech/hello'
const senderAccountHdPath = `m/44'/60'/0'/0/0`
const receiverAccountHdPath = `m/44'/60'/0'/0/1`
const senderAccount = mnemonicToAccount(MNEMONIC, {
path: senderAccountHdPath,
})
const receiverAccount = mnemonicToAccount(MNEMONIC, {
path: receiverAccountHdPath,
})
/**
* Account that send payments payments.
*/
console.log('sender address', senderAccount.address)
/**
* Create iohtee instance that provides API for accepting payments.
*/
const iohtee = new IohTee({
networkId: CHAIN_ID,
httpRpcUrl: RPC_URL,
mnemonic: MNEMONIC,
hdPath: senderAccountHdPath,
options: {
databaseUrl: `sqlite://${path.resolve(__dirname, '../client.db')}`,
},
})
let response = await fetch(TARGET)
let headers = response.headers
/**
* Request token to content access
*/
const result = await iohtee.buy({
price: BigInt(String(headers.get('paywall-price'))),
gateway: headers.get('paywall-gateway')!,
receiver: headers.get('paywall-address')! as `0x${string}`,
meta: 'metaidexample',
})
const token = result.token
/**
* Request paid content
*/
const content = await fetch(TARGET, {
headers: {
authorization: `paywall ${token} ${'metaidexample'} ${String(
headers.get('paywall-price'),
)}`,
},
})
const body = content.body! as any
return body.read().toString()
}
main()
.then((content: string) => {
console.log('Bought content: ')
console.log(`"${content}"`)
process.exit(0)
})
.catch((error) => {
console.error(error)
process.exit(1)
})