Skip to content

Commit

Permalink
Merge pull request #1810 from okTurtles/hash-fix
Browse files Browse the repository at this point in the history
Hash fix
  • Loading branch information
taoeffect authored Jan 24, 2024
2 parents 4348366 + ff4c3e8 commit f61b445
Show file tree
Hide file tree
Showing 30 changed files with 2,586 additions and 11,980 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"plugins": [
"babel-plugin-root-import",
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-transform-class-properties",
"@babel/plugin-transform-object-rest-spread",
"@babel/plugin-syntax-dynamic-import"
]
}
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
.*/dist-dashboard/.*
<PROJECT_ROOT>/contracts/.*
.*/backend/dashboard/.*
.*/shared/multiformats/.*
.*/frontend/assets/.*
.*/frontend/controller/service-worker.js
.*/frontend/utils/blockies.js
Expand Down
4 changes: 2 additions & 2 deletions backend/dashboard/views/utils/dummy-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */

// dummy placeholder data to be used in various pages
import { blake32Hash } from '@common/functions.js'
import { createCID } from '@common/functions.js'
import { addTimeToDate, MONTHS_MILLIS } from '@common/cdTimeUtils.js'
import L from '@common/translations.js'

Expand All @@ -10,7 +10,7 @@ const fakeUsers = ['Flex Kubin', 'Attila Hun', 'Childish Gambino', 'Ken M', 'Mar
const PAST_THREE_MONTHS = -3 * MONTHS_MILLIS
const randomPastDate = () => addTimeToDate(new Date(), Math.floor(Math.random() * PAST_THREE_MONTHS))
const randomFromArray = arr => arr[Math.floor(Math.random() * arr.length)]
const randomHash = () => blake32Hash(Math.random().toString(16).slice(2))
const randomHash = () => createCID(Math.random().toString(16).slice(2))

// Contracts.vue //
const contractDummyData = []
Expand Down
6 changes: 3 additions & 3 deletions backend/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ export default async () => {
})
sbp('sbp/selectors/lock', ['chelonia/db/get', 'chelonia/db/set', 'chelonia/db/delete'])
}
// TODO: Update this to only run when persistence is disabled when `¢hel deploy` can target SQLite.
// TODO: Update this to only run when persistence is disabled when `chel deploy` can target SQLite.
if (persistence !== 'fs' || options.fs.dirname !== './data') {
const HASH_LENGTH = 50
// Remember to keep these values up-to-date.
const HASH_LENGTH = 52
const CONTRACT_MANIFEST_MAGIC = '{"head":{"manifestVersion"'
const CONTRACT_SOURCE_MAGIC = '"use strict";'
// Preload contract source files and contract manifests into Chelonia DB.
// Note: the data folder may contain other files if the `fs` persistence mode
// has been used before. We won't load them here; that's the job of `chel migrate`.
// Note: our target files are currently deployed with unprefixed hashes as file names.
// We can take advantage of this to recognize them more easily.
// TODO: Update this code when `¢hel deploy` no longer generates unprefixed keys.
// TODO: Update this code when `chel deploy` no longer generates unprefixed keys.
const keys = (await readdir(dataFolder))
// Skip some irrelevant files.
.filter(k => k.length === HASH_LENGTH)
Expand Down
4 changes: 2 additions & 2 deletions backend/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import sbp from '@sbp/sbp'
import { GIMessage } from '~/shared/domains/chelonia/GIMessage.js'
import { blake32Hash } from '~/shared/functions.js'
import { createCID } from '~/shared/functions.js'
import { SERVER_INSTANCE } from './instance-keys.js'
import path from 'path'
import chalk from 'chalk'
Expand Down Expand Up @@ -187,7 +187,7 @@ route.POST('/file', {
const { hash, data } = request.payload
if (!hash) return Boom.badRequest('missing hash')
if (!data) return Boom.badRequest('missing data')
const ourHash = blake32Hash(data)
const ourHash = createCID(data)
if (ourHash !== hash) {
console.error(`hash(${hash}) != ourHash(${ourHash})`)
return Boom.badRequest('bad hash!')
Expand Down
1 change: 0 additions & 1 deletion frontend/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import '@sbp/okturtles.events'
import '@sbp/okturtles.eventqueue'
import { mapMutations, mapGetters, mapState } from 'vuex'
import 'wicg-inert'

import '@model/captureLogs.js'
import type { GIMessage } from '~/shared/domains/chelonia/chelonia.js'
import '~/shared/domains/chelonia/chelonia.js'
Expand Down
6 changes: 3 additions & 3 deletions frontend/model/contracts/manifests.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifests": {
"gi.contracts/chatroom": "21XWnNRF7KJdx1Kch5kDqQdUyKuXrwKFj6NRg95CEGRV7ghhLZ",
"gi.contracts/group": "21XWnNN4pyGLkKd192nMhGvqyWpV96UvViFRCDtzEnkMWX9WMM",
"gi.contracts/identity": "21XWnNXmm246ks6gYuvf5RLijrv89Cdbi5PFspXULaAHS7pc6w"
"gi.contracts/chatroom": "z9brRu3VGUJuML5qnvRfPu8GvuH8rX21FoTfkXTZDYXiVfqhUQXh",
"gi.contracts/group": "z9brRu3VNbanb5DR2YTUrFDUaZ7LwHfQS3V6mwExwYEXSMmVKDyp",
"gi.contracts/identity": "z9brRu3VSV4mxub2x1bcJ2Jx8n5c4moy9LUG9FWnpVisEmNhMJ7N"
}
}
4 changes: 2 additions & 2 deletions frontend/utils/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

import sbp from '@sbp/sbp'
import { blake32Hash } from '~/shared/functions.js'
import { createCID } from '~/shared/functions.js'
import { handleFetchResult } from '~/frontend/controller/utils/misc.js'

// Copied from https://stackoverflow.com/a/27980815/4737729
Expand Down Expand Up @@ -33,7 +33,7 @@ export const imageUpload = async (imageFile: File): Promise<any> => {
if (result === null) {
console.warn('File upload failed: could not load the given file into an array buffer.')
} else {
const hash = blake32Hash(new Uint8Array(((result: any): ArrayBuffer)))
const hash = createCID(new Uint8Array(((result: any): ArrayBuffer)))
console.debug('picture hash:', hash)
fd.append('hash', hash)
fd.append('data', file)
Expand Down
Loading

0 comments on commit f61b445

Please sign in to comment.