Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
b0f5d48
feat: wip
Jack-Works Dec 13, 2021
057ca32
feat: impl ecdh and query author public key
Jack-Works Dec 14, 2021
54f392d
feat: add more logic to decryption process
Jack-Works Dec 14, 2021
0efd5bb
chore: use sync import for encryption package
Jack-Works Dec 16, 2021
94e89fb
feat: add gun related code
Jack-Works Dec 16, 2021
532e3a7
chore: implement more gun related data
Jack-Works Dec 17, 2021
fc07bd4
fix: typo
Jack-Works Dec 20, 2021
8cb5e09
chore: add more logic
Jack-Works Dec 20, 2021
dd53458
refactor: move twitter encoding into encryption package
Jack-Works Dec 20, 2021
d962d79
refactor: move stego to encryption package
Jack-Works Dec 20, 2021
8a56e9e
Merge branch 'main' into use-new-decryption
Jack-Works Dec 28, 2021
fc3170b
Merge branch 'main' into use-new-decryption
Jack-Works Dec 28, 2021
56166f5
Merge branch 'main' into use-new-decryption
Jack-Works Dec 28, 2021
29b7f36
chore: move gun
Jack-Works Dec 29, 2021
ab5afe8
feat: impl image decode
Jack-Works Dec 29, 2021
cf843c0
chore: wip
Jack-Works Dec 31, 2021
fdc390b
feat: make it work
Jack-Works Dec 31, 2021
a685802
Merge branch 'main' into use-new-decryption
Jack-Works Dec 31, 2021
fba66ac
feat: cleanup old things
Jack-Works Dec 31, 2021
e39ef69
chore: wip
Jack-Works Dec 31, 2021
9c0a4ee
feat: multiple decryption
Jack-Works Dec 31, 2021
3b65825
Merge branch 'main' into use-new-decryption
Jack-Works Jan 4, 2022
fc01bd5
feat: pub dec works
Jack-Works Jan 4, 2022
1d187fa
feat: wip
Jack-Works Jan 5, 2022
6980b25
fix: eslint
Jack-Works Jan 5, 2022
46c7815
fix: eslint
Jack-Works Jan 5, 2022
0af9c33
Merge branch 'main' into use-new-decryption
Jack-Works Jan 5, 2022
7e5e118
feat: use new api
Jack-Works Jan 5, 2022
4accf1d
feat: add decryption in post context
Jack-Works Jan 5, 2022
ee0960f
feat: make it work
Jack-Works Jan 5, 2022
0b43c5f
Merge branch 'main' into use-new-decryption
Jack-Works Jan 5, 2022
f5ee815
refactor: remove unused code
Jack-Works Jan 5, 2022
77d6ff1
refactor: remove unused code
Jack-Works Jan 5, 2022
67b4d30
refactor: remove unused code
Jack-Works Jan 5, 2022
1a1cbea
chore: add correct sns
Jack-Works Jan 5, 2022
0af20fc
refactor: remove unused code
Jack-Works Jan 5, 2022
b9b73f5
Merge branch 'main' into use-new-decryption
Jack-Works Jan 6, 2022
5aa9b68
fix: tsconfig
Jack-Works Jan 6, 2022
3ea3500
fix: tsconfig
Jack-Works Jan 6, 2022
d568a53
fix: co-op E2E with old implementation
Jack-Works Jan 6, 2022
ca08526
Merge branch 'main' into use-new-decryption
Jack-Works Jan 6, 2022
ed65c7d
fix: type error
Jack-Works Jan 6, 2022
5fde670
Merge branch 'main' into use-new-decryption
Jack-Works Jan 6, 2022
0d1b4df
chore: some tweak
Jack-Works Jan 6, 2022
3d2d0b7
chore: change
Jack-Works Jan 6, 2022
4de5c36
chore: move gun to own package
Jack-Works Jan 6, 2022
ac66f10
feat: fix post replacer
Jack-Works Jan 11, 2022
2d37cd8
chore: change how detect text payload
Jack-Works Jan 11, 2022
6d62b91
chore: merge with main
Jack-Works Jan 11, 2022
f337b10
chore: merge with main
Jack-Works Jan 11, 2022
285acb8
fix: checkspell
Jack-Works Jan 11, 2022
93bf1a8
Merge branch 'main' into use-new-decryption
Jack-Works Jan 12, 2022
d45fd44
fix: lint error
Jack-Works Jan 12, 2022
76b5fee
wip
Jack-Works Jan 12, 2022
2490612
Merge branch 'main' into use-new-decryption
Jack-Works Jan 12, 2022
b8d7b3a
chore: add module mapping
Jack-Works Jan 12, 2022
7cbb6a1
chore: remove unused
Jack-Works Jan 12, 2022
6d6971f
Merge branch 'main' into use-new-decryption
Jack-Works Jan 13, 2022
59fa0e6
feat: move typed message renders to its package
Jack-Works Jan 13, 2022
cf46da2
feat: replace old renderer
Jack-Works Jan 13, 2022
cce50cc
feat: use new render
Jack-Works Jan 13, 2022
057184f
fix: wrong render choose
Jack-Works Jan 13, 2022
ecce372
fix: linter
Jack-Works Jan 13, 2022
e366d53
chore: remove use-subscription in typed-message package
Jack-Works Jan 14, 2022
3ddb7b3
chore: convert flatten to transformer
Jack-Works Jan 14, 2022
0296608
refactor: move render text
Jack-Works Jan 14, 2022
54331dd
chore: add parse tag
Jack-Works Jan 14, 2022
2e83c91
chore: add parse tag
Jack-Works Jan 14, 2022
4315559
chore: wip
Jack-Works Jan 17, 2022
9e46884
Merge branch 'main' into use-new-decryption
Jack-Works Jan 17, 2022
022c577
chore: fix linter
Jack-Works Jan 17, 2022
6ac7668
fix: type error
Jack-Works Jan 17, 2022
28000d7
chore: new transform
Jack-Works Jan 17, 2022
8fd01ad
chore: delete reexpor from shared-base
Jack-Works Jan 17, 2022
c4bfead
feat: decryption demo
Jack-Works Jan 19, 2022
4a683a5
chore: remove unused code
Jack-Works Jan 19, 2022
e7f7b8b
Merge branch 'main' into use-new-decryption
Jack-Works Jan 21, 2022
6ac0399
Merge branch 'main' into use-new-decryption
Jack-Works Jan 24, 2022
ad71887
docs: improve gun-utils comment
Jack-Works Jan 24, 2022
e7125a3
Merge branch 'main' into use-new-decryption
Jack-Works Jan 24, 2022
5ab91c9
chore: move
Jack-Works Jan 25, 2022
bc2645c
fix: lint
Jack-Works Jan 25, 2022
ae6625b
Merge branch 'main' into use-new-decryption
Jack-Works Jan 26, 2022
a578464
Merge branch 'main' into use-new-decryption
Jack-Works Jan 26, 2022
3c60446
fix: project reference
Jack-Works Jan 26, 2022
9a19b95
fix: project reference
Jack-Works Jan 26, 2022
ce16957
fix: type error
Jack-Works Jan 26, 2022
cac17ec
feat: store author public key
Jack-Works Jan 27, 2022
c35eadc
Merge branch 'main' into use-new-decryption
Jack-Works Jan 27, 2022
37dfbe0
refactor: merge post replacer and post inspector
Jack-Works Jan 27, 2022
744acec
refactor: move shadow root component to shared-ui
Jack-Works Jan 27, 2022
da593e3
refactor: remove allowTextEnlarge
Jack-Works Jan 27, 2022
7025b3c
fix: promise resolve
Jack-Works Jan 27, 2022
812cebb
chore: make it prettier
Jack-Works Jan 27, 2022
f3de74d
refactor: remove unused properties
Jack-Works Jan 27, 2022
136b8b4
fix: check spell
Jack-Works Jan 28, 2022
f758d36
refactor
Jack-Works Jan 28, 2022
f3507ae
chore: remove typed-message from top tsconfig
Jack-Works Jan 28, 2022
0f7f240
chore: change meta render to hooks
Jack-Works Jan 28, 2022
b36cbd9
chore: refactor
Jack-Works Jan 28, 2022
b10afaf
Merge branch 'main' into use-new-decryption
Jack-Works Jan 28, 2022
078aaa3
chore: adjust allowTextEnlarge
Jack-Works Jan 28, 2022
4827513
fix: gun usages
Jack-Works Jan 29, 2022
24e4925
fix: cspell
Jack-Works Jan 29, 2022
278f811
refactor: refine typed-message
Jack-Works Jan 29, 2022
1ec2449
chore: move a type
Jack-Works Jan 29, 2022
498ee6e
chore: use foreach in collect
Jack-Works Jan 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
"openocean",
"OPENOCEAN",
"pkts",
"plusplus",
"pnpm",
"poap",
"poaps",
Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const config: InitialOptionsTsJest = {
extensionsToTreatAsEsm: ['.ts', '.tsx'],
moduleNameMapper: {
'@masknet/shared-base': '<rootDir>/packages/shared-base/src/index.ts',
'@masknet/typed-message/base': '<rootDir>/packages/typed-message/base/index.ts',
'jest-websocket-mock': '<rootDir>/packages/web3-shared/base/node_modules/jest-websocket-mock',
'reconnecting-websocket': '<rootDir>/packages/web3-shared/base/node_modules/reconnecting-websocket',
'date-fns/(.*)': '<rootDir>/packages/web3-shared/base/node_modules/date-fns/$1',
Expand Down
1 change: 1 addition & 0 deletions packages/encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"@dimensiondev/stego-js": "0.11.1-20201027083223-8ab41be",
"@masknet/shared-base": "workspace:*",
"@masknet/typed-message": "workspace:*",
"@msgpack/msgpack": "^2.7.0"
}
}
9 changes: 2 additions & 7 deletions packages/encryption/src/encryption/Decryption.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { unreachable } from '@dimensiondev/kit'
import {
decodeTypedMessageFromDocument,
decodeTypedMessageV38ToV40Format,
AESCryptoKey,
EC_Public_CryptoKey,
andThenAsync,
} from '@masknet/shared-base'
import { decodeTypedMessageFromDocument, decodeTypedMessageV38ToV40Format } from '@masknet/typed-message/base'
import { AESCryptoKey, EC_Public_CryptoKey, andThenAsync } from '@masknet/shared-base'
import { None, Result } from 'ts-results'
import { AESAlgorithmEnum, PayloadParseResult } from '../payload'
import { decryptWithAES, importAESFromJWK } from '../utils'
Expand Down
20 changes: 19 additions & 1 deletion packages/encryption/src/encryption/DecryptionTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TypedMessage, ProfileIdentifier, AESCryptoKey, EC_Public_CryptoKey } from '@masknet/shared-base'
import type { TypedMessage } from '@masknet/typed-message/base'
import type { ProfileIdentifier, AESCryptoKey, EC_Public_CryptoKey } from '@masknet/shared-base'
import type { PayloadParseResult } from '../payload'
import { registerSerializableClass } from '@masknet/shared-base'

export interface DecryptOptions {
message: PayloadParseResult.Payload
Expand Down Expand Up @@ -150,6 +152,7 @@ export enum ErrorReasons {
// Not used in this library.
UnrecognizedAuthor = '[@masknet/encryption] No author is recognized which is required for the image steganography decoding.',
CurrentProfileDoesNotConnectedToPersona = '[@masknet/encryption] Cannot decrypt by E2E because no persona is linked with the current profile.',
NoPayloadFound = '[@masknet/encryption] No payload found in this material.',
}
export class DecryptError extends Error {
static Reasons = ErrorReasons
Expand All @@ -158,3 +161,18 @@ export class DecryptError extends Error {
super(message, { cause })
}
}
registerSerializableClass(
'MaskDecryptError',
(x) => x instanceof DecryptError,
(e: DecryptError) => ({
cause: (e as any).cause,
recoverable: e.recoverable,
message: e.message,
stack: e.stack,
}),
(o) => {
const e = new DecryptError(o.message, o.cause, o.recoverable)
e.stack = o.stack
return e
},
)
2 changes: 1 addition & 1 deletion packages/encryption/src/encryption/Encryption.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {
PersonaIdentifier,
ProfileIdentifier,
TypedMessage,
AESCryptoKey,
PostIVIdentifier,
EC_Public_CryptoKey,
EC_Private_CryptoKey,
ECKeyIdentifier,
IdentifierMap,
} from '@masknet/shared-base'
import type { TypedMessage } from '@masknet/typed-message/base'

export interface EncryptOptions {
/** Payload version to use. */
Expand Down
6 changes: 3 additions & 3 deletions packages/encryption/src/image-steganography/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function steganographyEncodeImage(buf: ArrayBuffer, options: Encode

export type DecodeImageOptions = SteganographyIO & Partial<EncodeOptions> & Pick<EncodeOptions, 'pass'>

async function steganographyDecodeImage(buf: ArrayBuffer, options: DecodeImageOptions) {
async function inner(buf: ArrayBuffer, options: DecodeImageOptions) {
const dimension = getDimension(buf)
const preset = dimensionPreset.find((d) => isSameDimension(d, dimension))
if (!preset) return ''
Expand All @@ -104,6 +104,6 @@ async function steganographyDecodeImage(buf: ArrayBuffer, options: DecodeImageOp
})
}

export async function steganographyDecodeImageUrl(url: string, options: DecodeImageOptions) {
return steganographyDecodeImage(await options.downloadImage(url), options)
export async function steganographyDecodeImage(image: Blob | string, options: DecodeImageOptions) {
return inner(typeof image === 'string' ? await options.downloadImage(image) : await image.arrayBuffer(), options)
}
2 changes: 1 addition & 1 deletion packages/encryption/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export {
type EncodeImageOptions,
type ImageTemplateTypes,
type SteganographyIO,
steganographyDecodeImageUrl,
steganographyDecodeImage,
steganographyEncodeImage,
GrayscaleAlgorithm,
} from './image-steganography'
Expand Down
2 changes: 2 additions & 0 deletions packages/encryption/src/payload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,14 @@ export enum AESAlgorithmEnum {
A256GCM = 'A256GCM',
}
export enum SocialNetworkEnum {
Unknown = -1,
Facebook = 0,
Twitter = 1,
Instagram = 2,
Minds = 3,
}
const SocialNetworkEnumToDomain: Record<SocialNetworkEnum, string> = {
[SocialNetworkEnum.Unknown]: 'localhost',
[SocialNetworkEnum.Facebook]: 'facebook.com',
[SocialNetworkEnum.Minds]: 'minds.com',
[SocialNetworkEnum.Twitter]: 'twitter.com',
Expand Down
12 changes: 8 additions & 4 deletions packages/encryption/src/social-network-encode-decode/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Some } from 'ts-results'
import { SocialNetworkEnum } from '../payload/types'
import { TwitterDecoder, __TwitterEncoder } from './twitter'

export function socialNetworkDecoder(network: SocialNetworkEnum, content: string) {
if (network === SocialNetworkEnum.Twitter) return TwitterDecoder(content)
return Some(content)
export function socialNetworkDecoder(network: SocialNetworkEnum, content: string): string[] {
if (network === SocialNetworkEnum.Twitter)
return TwitterDecoder(content)
.map((x) => [x])
.unwrapOr([])
const all = content.match(/(\u{1F3BC}[\w+/=|]+:\|\|)/giu)
if (all) return all
return []
}
export function socialNetworkEncoder(network: SocialNetworkEnum, content: string) {
if (network === SocialNetworkEnum.Twitter) return __TwitterEncoder(content)
Expand Down
2 changes: 1 addition & 1 deletion packages/encryption/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"stripInternal": true
},
"include": ["./src/**/*"],
"references": [{ "path": "../shared-base" }]
"references": [{ "path": "../shared-base" }, { "path": "../typed-message/base" }]
}
13 changes: 13 additions & 0 deletions packages/gun-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @masknet/gun-utils

This packages wrap Gun with a developer-friendly API. Before using, please make sure you have understand:

- Gun is a decentralized graph database.
- There're many other applications are using Gun.
- Every change is visible to others (may not be correct, but in our use cases it is).
- There is no "private server", you cannot create a fresh "gun" world that only contains our data.
- DO NOT publish sensitive information on Gun.

For more information, please access https://gun.eco/.

It is especially important to understand how Gun is modeling "Array of data".
11 changes: 11 additions & 0 deletions packages/gun-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@masknet/gun-utils",
"private": true,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"dependencies": {
"event-iterator": "^2.0.0",
"gun": "^0.2020.1234"
}
}
1 change: 1 addition & 0 deletions packages/gun-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { deleteGunData, getGunData, pushToGunDataArray, setGunData, subscribeGunMapData } from './utils'
48 changes: 48 additions & 0 deletions packages/gun-utils/src/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Gun from 'gun'
import 'gun/sea'
import 'gun/lib/radix'
import 'gun/lib/radisk'
import 'gun/lib/store'
import 'gun/lib/rindexed'
import { gunServers } from './server'

export type GunRoot = ReturnType<typeof createGun>
let gun: GunRoot | undefined
export function getGunInstance(): GunRoot {
if (gun) return gun
return (gun = createGun())
}

function createGun() {
class WebSocket extends globalThis.WebSocket {
constructor(url: string | URL) {
super(url)
const abort = (this.abort = () => {
gun = undefined
this.close()
console.log('[Network/gun] WebSocket of the Gun instance is killed due to inactive.')
})
const tick = (this.tick = () => {
if (this.timer) clearTimeout(this.timer)
this.timer = setTimeout(abort, 15 * 60 * 1000)
})
this.addEventListener('message', tick, {})
}
private declare abort: () => void
private declare tick: () => void
declare timer: NodeJS.Timer | undefined
override send(data: any) {
this.tick()
super.send(data)
}
}

const _ = new Gun({
peers: [...gunServers],
localStorage: false,
radisk: true,
WebSocket,
})
_.opt({ retry: Number.POSITIVE_INFINITY })
return _
}
80 changes: 80 additions & 0 deletions packages/gun-utils/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { EventIterator } from 'event-iterator'
import { getGunInstance } from './instance'

function getGunNodeFromPath(path: string[]) {
const resultNode = path.reduce((gun, path) => gun.get(path as never), getGunInstance())
return resultNode
}
/**
* Get data from Gun. Equivalent as the following code:
*
* ```ts
* gun.get(path[0]).get(path[1])....get(path[n]).once()
* ```
*/
export function getGunData(...path: string[]) {
return new Promise<{ [x: string]: unknown } | string | number | undefined>((resolve) => {
getGunNodeFromPath(path).once(resolve)
})
}

/**
* Set data on Gun. Equivalent as the following code:
*
* ```ts
* gun.get(path[0]).get(path[1])....get(path[n]).put(data)
* ```
* @param path graph path on Gun
* @param data data to be stored
*/
export function setGunData(path: string[], data: any) {
getGunNodeFromPath(path).put(data)
}

/**
* Delete data on Gun. Equivalent as the following code:
*
* ```ts
* gun.get(path[0]).get(path[1])....get(path[n]).put(null!)
* ```
* @param path graph path on Gun
*/
export function deleteGunData(path: string[]) {
getGunNodeFromPath(path).put(null!)
}

/**
* Push data to the Gun data Set (Mathematical Set)
* @param path graph path on Gun
* @param value the object to be stored
*/
export function pushToGunDataArray(path: string[], value: object) {
getGunNodeFromPath(path).set(value)
}

/**
* Subscribe future data on Gun.
* When subscribing a Gun data Set (Mathematical Set), you will not get the immediate result back.
*
* @param path graph path on Gun
* @param isT is the data type T
* @param abortSignal the signal to stop subscribing
*/
export async function* subscribeGunMapData<T>(path: string[], isT: (x: unknown) => x is T, abortSignal: AbortSignal) {
yield* new EventIterator<T>((queue) => {
// gun.off() will remove ALL listener on it
let listenerClosed = false

abortSignal.addEventListener('abort', () => {
queue.stop()
listenerClosed = true
})

const resultNode = getGunNodeFromPath(path)

resultNode.map().on((data) => {
if (listenerClosed) return
if (isT(data)) queue.push(data)
})
})
}
10 changes: 10 additions & 0 deletions packages/gun-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src/",
"outDir": "./dist/",
"tsBuildInfoFile": "./dist/.tsbuildinfo"
},
"include": ["./src"],
"references": []
}
3 changes: 3 additions & 0 deletions packages/mask/.webpack/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ export function createConfiguration(rawFlags: BuildFlags): Configuration {
// we can speed up the compile because there is no need to wait tsc build them to the dist folder.
'@masknet/dashboard$': require.resolve('../../dashboard/src/entry.tsx'),
'@masknet/injected-script': join(__dirname, '../../injected-script/sdk'),
'@masknet/gun-utils': join(__dirname, '../../gun-utils/src/'),
'@masknet/shared': join(__dirname, '../../shared/src/'),
'@masknet/shared-base': join(__dirname, '../../shared-base/src/'),
'@masknet/theme': join(__dirname, '../../theme/src/'),
'@masknet/typed-message/base': join(__dirname, '../../typed-message/base/'),
'@masknet/typed-message/dom': join(__dirname, '../../typed-message/dom/'),
'@masknet/icons': join(__dirname, '../../icons/index.ts'),
'@masknet/web3-kit': join(__dirname, '../../web3-kit/src/'),
'@masknet/web3-providers': join(__dirname, '../../web3-providers/src'),
Expand Down
Loading