-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb7d204
commit f659805
Showing
5 changed files
with
285 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,10 @@ | ||
import KYVE, { Item } from '@kyve/core'; | ||
import { Signature } from './types'; | ||
import { fetchBlock, isHeightOutOfRange } from './utils'; | ||
import { name, version } from '../package.json'; | ||
import { Node, Arweave, Gzip, JsonFileCache } from '@kyve/core'; | ||
|
||
process.env.KYVE_RUNTIME = name; | ||
process.env.KYVE_VERSION = version; | ||
import Substrate from './runtime'; | ||
|
||
KYVE.metrics.register.setDefaultLabels({ | ||
app: process.env.KYVE_RUNTIME, | ||
}); | ||
|
||
class KyveSubstrate extends KYVE { | ||
public async getDataItem(key: string): Promise<Item> { | ||
let block; | ||
|
||
try { | ||
block = await fetchBlock( | ||
this.pool.config.rpc, | ||
+key, | ||
await this.getSignature() | ||
); | ||
} catch (err) { | ||
if (isHeightOutOfRange(err)) throw new Error(); | ||
|
||
this.logger.warn(`Failed to fetch block ${key}. Retrying ...`); | ||
|
||
throw err; | ||
} | ||
|
||
return { key, value: block }; | ||
} | ||
|
||
public async getNextKey(key: string): Promise<string> { | ||
if (key) { | ||
return (parseInt(key) + 1).toString(); | ||
} | ||
|
||
return '0'; | ||
} | ||
|
||
public async formatValue(value: any): Promise<string> { | ||
return value.hash; | ||
} | ||
|
||
private async getSignature(): Promise<Signature> { | ||
const address = await this.sdk.wallet.getAddress(); | ||
const timestamp = new Date().valueOf().toString(); | ||
|
||
const message = `${address}//${this.poolId}//${timestamp}`; | ||
|
||
const { signature, pub_key } = await this.sdk.signString(message); | ||
|
||
return { | ||
signature, | ||
pubKey: pub_key.value, | ||
poolId: this.poolId.toString(), | ||
timestamp, | ||
}; | ||
} | ||
} | ||
|
||
// noinspection JSIgnoredPromiseFromCall | ||
new KyveSubstrate().start(); | ||
new Node() | ||
.addRuntime(new Substrate()) | ||
.addStorageProvider(new Arweave()) | ||
.addCompression(new Gzip()) | ||
.addCache(new JsonFileCache()) | ||
.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { DataItem, IRuntime, Node } from '@kyve/core'; | ||
import { name, version } from '../package.json'; | ||
import { fetchBlock, isHeightOutOfRange } from './utils'; | ||
|
||
export default class Substrate implements IRuntime { | ||
public name = name; | ||
public version = version; | ||
|
||
public async getDataItem(core: Node, key: string): Promise<DataItem> { | ||
let block; | ||
|
||
const headers = await this.generateCoinbaseCloudHeaders(core); | ||
|
||
try { | ||
block = await fetchBlock(core.poolConfig.rpc, +key, headers); | ||
} catch (err) { | ||
if (isHeightOutOfRange(err)) throw new Error(); | ||
|
||
throw err; | ||
} | ||
|
||
return { key, value: block }; | ||
} | ||
|
||
public async getNextKey(key: string): Promise<string> { | ||
return (parseInt(key) + 1).toString(); | ||
} | ||
|
||
public async formatValue(value: any): Promise<string> { | ||
return value.hash; | ||
} | ||
|
||
private async generateCoinbaseCloudHeaders(core: Node): Promise<any> { | ||
// requestSignature for coinbase cloud | ||
const address = core.client.account.address; | ||
const timestamp = new Date().valueOf().toString(); | ||
const poolId = core.pool.id; | ||
|
||
const { signature, pub_key } = await core.client.signString( | ||
`${address}//${poolId}//${timestamp}` | ||
); | ||
|
||
return { | ||
'Content-Type': 'application/json', | ||
Signature: signature, | ||
'Public-Key': pub_key.value, | ||
'Pool-ID': poolId, | ||
Timestamp: timestamp, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.