Skip to content

Commit

Permalink
refactor: drop owner in favor of ethaddress (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
AuHau authored Mar 15, 2021
1 parent 8e81024 commit 6c26764
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import { prepareWebsocketData } from './utils/data'
import { fileArrayBuffer, isFile } from './utils/file'
import { AxiosRequestConfig } from 'axios'
import { FeedReader, FeedWriter, makeFeedReader, makeFeedWriter } from './feed'
import { EthAddress, makeSigner } from './chunk/signer'
import { makeSigner } from './chunk/signer'
import { assertIsFeedType, FeedType } from './feed/type'
import { Signer } from './chunk/signer'
import { downloadSingleOwnerChunk, uploadSingleOwnerChunkData, SOCReader, SOCWriter } from './chunk/soc'
import { makeOwner } from './chunk/owner'
import { Topic, makeTopic, makeTopicFromString } from './feed/topic'
import { createFeedManifest } from './modules/feed'
import { bytesToHex } from './utils/hex'
import { assertBeeUrl, stripLastSlash } from './utils/url'
import { EthAddress, makeEthAddress } from './utils/eth'

/**
* The Bee class provides a way of interacting with the Bee APIs based on the provided url
Expand Down Expand Up @@ -382,7 +382,7 @@ export class Bee {
assertIsFeedType(type)

const canonicalTopic = makeTopic(topic)
const canonicalOwner = makeOwner(owner)
const canonicalOwner = makeEthAddress(owner)

return createFeedManifest(this.url, bytesToHex(canonicalOwner), bytesToHex(canonicalTopic), { type })
}
Expand All @@ -402,7 +402,7 @@ export class Bee {
assertIsFeedType(type)

const canonicalTopic = makeTopic(topic)
const canonicalOwner = makeOwner(owner)
const canonicalOwner = makeEthAddress(owner)

return makeFeedReader(this.url, type, canonicalTopic, canonicalOwner)
}
Expand Down Expand Up @@ -441,7 +441,7 @@ export class Bee {
* @param ownerAddress The ethereum address of the owner
*/
makeSOCReader(ownerAddress: EthAddress | Uint8Array | string): SOCReader {
const canonicalOwner = makeOwner(ownerAddress)
const canonicalOwner = makeEthAddress(ownerAddress)

return {
download: downloadSingleOwnerChunk.bind(null, this.url, canonicalOwner),
Expand Down
18 changes: 0 additions & 18 deletions src/chunk/owner.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/chunk/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BeeError } from '../utils/error'
import { Bytes, verifyBytes } from '../utils/bytes'
import { keccak256Hash } from './hash'
import { hexToBytes, verifyHex } from '../utils/hex'
import { EthAddress } from '../utils/eth'

/**
* Ethereum compatible signing and recovery
Expand All @@ -11,7 +12,6 @@ import { hexToBytes, verifyHex } from '../utils/hex'
export type Signature = Bytes<65>
export type PrivateKey = Bytes<32>
export type PublicKey = Bytes<32> | Bytes<64>
export type EthAddress = Bytes<20>

type SyncSigner = (digest: Uint8Array) => Signature
type AsyncSigner = (digest: Uint8Array) => Promise<Signature>
Expand Down
3 changes: 2 additions & 1 deletion src/chunk/soc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Bytes, bytesAtOffset, bytesEqual, flexBytesAtOffset, verifyBytesAtOffset } from '../utils/bytes'
import { bmtHash } from './bmt'
import { EthAddress, recoverAddress, Signature, Signer } from './signer'
import { recoverAddress, Signature, Signer } from './signer'
import { keccak256Hash } from './hash'
import { SPAN_SIZE } from './span'
import { serializeBytes } from './serialize'
Expand All @@ -10,6 +10,7 @@ import { ReferenceResponse, UploadOptions } from '../types'
import { bytesToHex } from '../utils/hex'
import * as socAPI from '../modules/soc'
import * as chunkAPI from '../modules/chunk'
import { EthAddress } from '../utils/eth'

const IDENTIFIER_SIZE = 32
const SIGNATURE_SIZE = 65
Expand Down
8 changes: 4 additions & 4 deletions src/feed/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { keccak256Hash } from '../chunk/hash'
import { serializeBytes } from '../chunk/serialize'
import { EthAddress, Signer } from '../chunk/signer'
import { Signer } from '../chunk/signer'
import { Identifier, uploadSingleOwnerChunkData, verifySingleOwnerChunk } from '../chunk/soc'
import { FeedUpdateOptions, fetchFeedUpdate, FetchFeedUpdateResponse } from '../modules/feed'
import { Reference, ReferenceResponse, UploadOptions } from '../types'
Expand All @@ -10,8 +10,8 @@ import { bytesToHex, HexString, hexToBytes, verifyHex } from '../utils/hex'
import { readUint64BigEndian, writeUint64BigEndian } from '../utils/uint64'
import * as chunkAPI from '../modules/chunk'
import { Topic } from './topic'
import { Owner } from '../chunk/owner'
import { FeedType } from './type'
import { EthAddress } from '../utils/eth'

const TIMESTAMP_PAYLOAD_OFFSET = 0
const TIMESTAMP_PAYLOAD_SIZE = 8
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface FeedUpdate {
*/
export interface FeedReader {
readonly type: FeedType
readonly owner: Owner
readonly owner: EthAddress
readonly topic: Topic
/**
* Download the latest feed update
Expand Down Expand Up @@ -182,7 +182,7 @@ export async function downloadFeedUpdate(
}
}

export function makeFeedReader(url: string, type: FeedType, topic: Topic, owner: Owner): FeedReader {
export function makeFeedReader(url: string, type: FeedType, topic: Topic, owner: EthAddress): FeedReader {
const ownerHex = bytesToHex(owner)
const topicHex = bytesToHex(topic)
const download = (options?: FeedUpdateOptions) => fetchFeedUpdate(url, ownerHex, topicHex, { ...options, type })
Expand Down
15 changes: 15 additions & 0 deletions src/utils/eth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { keccak256, sha3_256 } from 'js-sha3'
import { BrandedString } from '../types'
import { HexString, hexToBytes, intToHex, isHexString, stripHexPrefix, verifyHex } from './hex'
import { Bytes, verifyBytes } from './bytes'

export type HexEthAddress = BrandedString<'HexEthAddress'>
export type OverlayAddress = BrandedString<'OverlayAddress'>
export type EthAddress = Bytes<20>

export function makeEthAddress(address: EthAddress | Uint8Array | string): EthAddress {
if (typeof address === 'string') {
const hexOwner = verifyHex(address)
const ownerBytes = hexToBytes(hexOwner)

return verifyBytes(20, ownerBytes)
} else if (address instanceof Uint8Array) {
return verifyBytes(20, address)
}
throw new TypeError('invalid owner')
}

/**
* Check if this is all caps or small caps eth address (=address without checksum)
Expand Down
6 changes: 3 additions & 3 deletions test/bee-class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './utils'
import { makeSigner } from '../src/chunk/signer'
import { makeSOCAddress, uploadSingleOwnerChunkData } from '../src/chunk/soc'
import { makeOwner } from '../src/chunk/owner'
import { makeEthAddress } from '../src/utils/eth'
import * as collection from '../src/modules/collection'

describe('Bee class', () => {
Expand Down Expand Up @@ -321,7 +321,7 @@ describe('Bee class', () => {
describe('writer', () => {
it('should read and write', async () => {
const identifier = makeBytes(32) // all zeroes
const socAddress = makeSOCAddress(identifier, makeOwner(testIdentity.address))
const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
await tryDeleteChunkFromLocalStorage(socAddress)

const socWriter = bee.makeSOCWriter(testIdentity.privateKey)
Expand All @@ -339,7 +339,7 @@ describe('Bee class', () => {
it('should read', async () => {
const signer = makeSigner(testIdentity.privateKey)
const identifier = makeBytes(32) // all zeroes
const socAddress = makeSOCAddress(identifier, makeOwner(testIdentity.address))
const socAddress = makeSOCAddress(identifier, makeEthAddress(testIdentity.address))
await tryDeleteChunkFromLocalStorage(socAddress)
await uploadSingleOwnerChunkData(BEE_URL, signer, identifier, testChunkPayload)

Expand Down

0 comments on commit 6c26764

Please sign in to comment.