-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
164 lines (144 loc) · 4.82 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Binary } from 'cafe-utility'
import { makeSingleOwnerChunkFromData, uploadSingleOwnerChunkData } from '../chunk/soc'
import * as chunkAPI from '../modules/chunk'
import { FeedUpdateOptions, FetchFeedUpdateResponse, fetchLatestFeedUpdate } from '../modules/feed'
import {
Address,
BatchId,
BeeRequestOptions,
BytesReference,
FEED_INDEX_HEX_LENGTH,
FeedReader,
FeedWriter,
PlainBytesReference,
Reference,
Signer,
Topic,
UploadOptions,
UploadResult,
} from '../types'
import { Bytes, bytesAtOffset, makeBytes } from '../utils/bytes'
import { BeeResponseError } from '../utils/error'
import { EthAddress, HexEthAddress, makeHexEthAddress } from '../utils/eth'
import { keccak256Hash } from '../utils/hash'
import { HexString, bytesToHex, hexToBytes, makeHexString } from '../utils/hex'
import { makeBytesReference } from '../utils/reference'
import { assertAddress } from '../utils/type'
import { makeFeedIdentifier } from './identifier'
import type { FeedType } from './type'
const TIMESTAMP_PAYLOAD_OFFSET = 0
const TIMESTAMP_PAYLOAD_SIZE = 8
const REFERENCE_PAYLOAD_OFFSET = TIMESTAMP_PAYLOAD_SIZE
export interface Epoch {
time: number
level: number
}
/**
* Bytes of Feed's Index.
* For Sequential Feeds this is numeric value in big-endian.
*/
export type IndexBytes = Bytes<8>
export type Index = number | Epoch | IndexBytes | string
export interface FeedUploadOptions extends UploadOptions, FeedUpdateOptions {}
export interface FeedUpdate {
timestamp: number
reference: BytesReference
}
export async function findNextIndex(
requestOptions: BeeRequestOptions,
owner: HexEthAddress,
topic: Topic,
options?: FeedUpdateOptions,
): Promise<HexString<typeof FEED_INDEX_HEX_LENGTH>> {
try {
const feedUpdate = await fetchLatestFeedUpdate(requestOptions, owner, topic, options)
return makeHexString(feedUpdate.feedIndexNext, FEED_INDEX_HEX_LENGTH)
} catch (e: any) {
if (e instanceof BeeResponseError) {
return bytesToHex(makeBytes(8))
}
throw e
}
}
export async function updateFeed(
requestOptions: BeeRequestOptions,
signer: Signer,
topic: Topic,
reference: BytesReference,
postageBatchId: BatchId,
options?: FeedUploadOptions,
): Promise<UploadResult> {
const ownerHex = makeHexEthAddress(signer.address)
const nextIndex = options?.index ?? (await findNextIndex(requestOptions, ownerHex, topic, options))
const identifier = makeFeedIdentifier(topic, nextIndex)
const at = options?.at ?? Date.now() / 1000.0
const timestamp = Binary.numberToUint64BE(Math.floor(at))
const payloadBytes = Binary.concatBytes(timestamp, reference)
return uploadSingleOwnerChunkData(requestOptions, signer, postageBatchId, identifier, payloadBytes, options)
}
export function getFeedUpdateChunkReference(owner: EthAddress, topic: Topic, index: Index): PlainBytesReference {
const identifier = makeFeedIdentifier(topic, index)
return keccak256Hash(identifier, owner)
}
export async function downloadFeedUpdate(
requestOptions: BeeRequestOptions,
owner: EthAddress,
topic: Topic,
index: Index,
): Promise<FeedUpdate> {
const address = getFeedUpdateChunkReference(owner, topic, index)
const addressHex = bytesToHex(address)
const data = await chunkAPI.download(requestOptions, addressHex)
const soc = makeSingleOwnerChunkFromData(data, address)
const payload = soc.payload()
const timestampBytes = bytesAtOffset(payload, TIMESTAMP_PAYLOAD_OFFSET, TIMESTAMP_PAYLOAD_SIZE)
const timestamp = Binary.uint64BEToNumber(timestampBytes)
const reference = makeBytesReference(payload, REFERENCE_PAYLOAD_OFFSET)
return {
timestamp,
reference,
}
}
export function makeFeedReader(
requestOptions: BeeRequestOptions,
type: FeedType,
topic: Topic,
owner: HexEthAddress,
): FeedReader {
return {
type,
owner,
topic,
async download(options?: FeedUpdateOptions): Promise<FetchFeedUpdateResponse> {
if (!options?.index && options?.index !== 0) {
return fetchLatestFeedUpdate(requestOptions, owner, topic)
}
const update = await downloadFeedUpdate(requestOptions, hexToBytes(owner), topic, options.index)
return {
reference: bytesToHex(update.reference),
feedIndex: options.index,
feedIndexNext: '',
}
},
}
}
export function makeFeedWriter(
requestOptions: BeeRequestOptions,
type: FeedType,
topic: Topic,
signer: Signer,
): FeedWriter {
const upload = async (
postageBatchId: string | Address,
reference: BytesReference | Reference,
options?: FeedUploadOptions,
) => {
assertAddress(postageBatchId)
const canonicalReference = makeBytesReference(reference)
return updateFeed(requestOptions, signer, topic, canonicalReference, postageBatchId, { ...options, type })
}
return {
...makeFeedReader(requestOptions, type, topic, makeHexEthAddress(signer.address)),
upload,
}
}