-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmine.ts
62 lines (51 loc) · 1.54 KB
/
mine.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
import mine from './src/mine'
import { readFileSync, writeFileSync } from 'fs'
import { HashPoints } from './src/contracts/hashPoints'
import {
bsv,
TestWallet,
DefaultProvider,
sha256,
toByteString,
hash256,
int2ByteString,
} from 'scrypt-ts'
import * as dotenv from 'dotenv'
// Load the .env file
dotenv.config()
if (!process.env.PRIVATE_KEY) {
throw new Error(
'No "PRIVATE_KEY" found in .env, Please run "npm run genprivkey" to generate a private key'
)
}
// Read the private key from the .env file.
// The default private key inside the .env file is meant to be used for the Bitcoin testnet.
// See https://scrypt.io/docs/bitcoin-basics/bsv/#private-keys
const privateKey = bsv.PrivateKey.fromWIF(process.env.PRIVATE_KEY || '')
// Prepare signer.
// See https://scrypt.io/docs/how-to-deploy-and-call-a-contract/#prepare-a-signer-and-provider
const signer = new TestWallet(
privateKey,
new DefaultProvider({
network: bsv.Networks.testnet,
})
)
async function main() {
await signer.connect()
await HashPoints.loadArtifact()
let txid = readFileSync('mine.txt').toString('utf8').trim()
let tx = await signer.connectedProvider.getTransaction(txid)
let instance = HashPoints.fromTx(tx, 0)
await instance.connect(signer)
console.log(`points: ${instance.points}`)
while (true) {
const { nonce } = await mine(txid, 8)
const res = await instance.methods.claim(toByteString(nonce))
tx = res.tx
txid = tx.id
writeFileSync('mine.txt', Buffer.from(txid, 'utf8'))
instance = HashPoints.fromTx(tx, 0)
await instance.connect(signer)
}
}
main()