Skip to content

Commit 321f94d

Browse files
committed
finished bitcoin
1 parent dc6ea63 commit 321f94d

5 files changed

Lines changed: 293 additions & 231 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kyve/bitcoin",
3-
"version": "0.2.3",
3+
"version": "0.3.0",
44
"license": "MIT",
55
"scripts": {
66
"build": "rimraf dist && tsc",
@@ -19,7 +19,7 @@
1919
"outputPath": "out"
2020
},
2121
"dependencies": {
22-
"@kyve/core": "1.2.3",
22+
"@kyve/core": "1.3.0",
2323
"axios": "^0.26.1",
2424
"nanoid": "^3.3.3"
2525
},

src/index.ts

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,10 @@
1-
import KYVE, { Item } from "@kyve/core";
2-
import { Signature } from "./types";
3-
import { fetchBlock, fetchBlockHash } from "./utils";
4-
import { version } from "../package.json";
1+
import { Node, Arweave, Gzip, JsonFileCache } from "@kyve/core";
52

6-
process.env.KYVE_RUNTIME = "@kyve/bitcoin";
7-
process.env.KYVE_VERSION = version;
3+
import Bitcoin from "./runtime";
84

9-
KYVE.metrics.register.setDefaultLabels({
10-
app: process.env.KYVE_RUNTIME,
11-
});
12-
13-
class KyveBitcoin extends KYVE {
14-
public async getDataItem(key: string): Promise<Item> {
15-
let hash: string;
16-
let block: any;
17-
18-
try {
19-
hash = await fetchBlockHash(
20-
this.pool.config.rpc,
21-
+key,
22-
await this.getSignature()
23-
);
24-
} catch (err) {
25-
// The only time this should fail is if the height is out of range.
26-
27-
throw err;
28-
}
29-
30-
try {
31-
block = await fetchBlock(
32-
this.pool.config.rpc,
33-
hash,
34-
await this.getSignature()
35-
);
36-
} catch (err) {
37-
this.logger.warn(`Failed to fetch block ${key}. Retrying ...`);
38-
39-
throw err;
40-
}
41-
42-
return { key, value: block };
43-
}
44-
45-
public async getNextKey(key: string): Promise<string> {
46-
if (key) {
47-
return (parseInt(key) + 1).toString();
48-
}
49-
50-
return "0";
51-
}
52-
53-
public async formatValue(value: any): Promise<string> {
54-
return value.hash;
55-
}
56-
57-
private async getSignature(): Promise<Signature> {
58-
const address = await this.sdk.wallet.getAddress();
59-
const timestamp = new Date().valueOf().toString();
60-
61-
const message = `${address}//${this.poolId}//${timestamp}`;
62-
63-
const { signature, pub_key } = await this.sdk.signString(message);
64-
65-
return {
66-
signature,
67-
pubKey: pub_key.value,
68-
poolId: this.poolId.toString(),
69-
timestamp,
70-
};
71-
}
72-
}
73-
74-
// noinspection JSIgnoredPromiseFromCall
75-
new KyveBitcoin().start();
5+
new Node()
6+
.addRuntime(new Bitcoin())
7+
.addStorageProvider(new Arweave())
8+
.addCompression(new Gzip())
9+
.addCache(new JsonFileCache())
10+
.start();

src/runtime.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { DataItem, IRuntime, Node } from "@kyve/core";
2+
import { name, version } from "../package.json";
3+
import { fetchBlock, fetchBlockHash } from "./utils";
4+
5+
export default class Bitcoin implements IRuntime {
6+
public name = name;
7+
public version = version;
8+
9+
public async getDataItem(core: Node, key: string): Promise<DataItem> {
10+
let hash: string;
11+
let block: any;
12+
13+
const headers = await this.generateCoinbaseCloudHeaders(core);
14+
15+
try {
16+
hash = await fetchBlockHash(core.poolConfig.rpc, +key, headers);
17+
} catch (err) {
18+
// The only time this should fail is if the height is out of range.
19+
throw err;
20+
}
21+
22+
try {
23+
block = await fetchBlock(core.poolConfig.rpc, hash, headers);
24+
} catch (err) {
25+
throw err;
26+
}
27+
28+
return { key, value: block };
29+
}
30+
31+
public async getNextKey(key: string): Promise<string> {
32+
return (parseInt(key) + 1).toString();
33+
}
34+
35+
public async formatValue(value: any): Promise<string> {
36+
return value.hash;
37+
}
38+
39+
private async generateCoinbaseCloudHeaders(core: Node): Promise<any> {
40+
// requestSignature for coinbase cloud
41+
const address = core.client.account.address;
42+
const timestamp = new Date().valueOf().toString();
43+
const poolId = core.pool.id;
44+
45+
const { signature, pub_key } = await core.client.signString(
46+
`${address}//${poolId}//${timestamp}`
47+
);
48+
49+
return {
50+
"Content-Type": "application/json",
51+
Signature: signature,
52+
"Public-Key": pub_key,
53+
"Pool-ID": poolId,
54+
Timestamp: timestamp,
55+
};
56+
}
57+
}

src/utils.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import axios from "axios";
22
import { nanoid } from "nanoid";
3-
import { Response, Signature } from "./types";
3+
import { Response } from "./types";
44

55
export async function fetchBlock(
66
endpoint: string,
77
hash: string,
8-
signature: Signature
8+
headers: any
99
): Promise<any> {
10-
const block = await call<any>(endpoint, "getblock", [hash, 2], signature);
10+
const block = await call<any>(endpoint, "getblock", [hash, 2], headers);
1111

1212
// Remove confirmations to maintain determinism.
1313
delete block.confirmations;
@@ -18,16 +18,16 @@ export async function fetchBlock(
1818
export async function fetchBlockHash(
1919
endpoint: string,
2020
height: number,
21-
signature: Signature
21+
headers: any
2222
): Promise<string> {
23-
return await call<string>(endpoint, "getblockhash", [height], signature);
23+
return await call<string>(endpoint, "getblockhash", [height], headers);
2424
}
2525

2626
async function call<T>(
2727
endpoint: string,
2828
method: string,
2929
params: any[],
30-
signature: Signature
30+
headers: any
3131
): Promise<T> {
3232
const { data } = await axios.get<Response<T>>(endpoint, {
3333
data: JSON.stringify({
@@ -36,13 +36,7 @@ async function call<T>(
3636
method,
3737
params,
3838
}),
39-
headers: {
40-
Signature: signature.signature,
41-
"Public-Key": signature.pubKey,
42-
"Pool-ID": signature.poolId,
43-
Timestamp: signature.timestamp,
44-
"Content-Type": "application/json",
45-
},
39+
headers,
4640
});
4741

4842
return data.result;

0 commit comments

Comments
 (0)