Skip to content

Commit

Permalink
Merge branch 'main' into mixmix/DOCS
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta authored Jun 25, 2024
2 parents 879159f + 7e6492b commit 9f66f90
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 105 deletions.
91 changes: 62 additions & 29 deletions dev/testing-utils.mjs
Original file line number Diff line number Diff line change
@@ -1,48 +1,81 @@
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { execFileSync } from 'child_process';
import { WebSocket } from 'ws';
import { promisify } from 'util'

const __dirname = dirname(fileURLToPath(import.meta.url));
const moduleRoot = join(__dirname, '..')

// NOTE: you need to edit your /etc/hosts file to use these. See dev/README.md

export async function spinNetworkUp (networkType = 'two-nodes') {
return new Promise((resolve, reject) => {
try {
execFileSync(
'dev/bin/spin-up.sh',
[networkType],
{
shell: true,
cwd: moduleRoot,
stdio: 'inherit'
} // Use shell's search path.
)
resolve(true)
try {
execFileSync('dev/bin/spin-up.sh', [networkType], {
shell: true,
cwd: moduleRoot,
stdio: 'inherit'
})
} catch (err) {
console.log('spin up failed', err)
return Promise.reject(err)
}

} catch (err) {
reject(err)
}
})
// TODO: pull all the endpoints we want to test from the networkType
const endpoint = 'ws://127.0.0.1:9944'
return retryUntil(() => isWebSocketReady(endpoint))
}

export async function spinNetworkDown (networkType = 'two-nodes') {
return new Promise((resolve, reject) => {
try {
execFileSync(
'dev/bin/spin-down.sh',
[networkType],
{
shell: true,
cwd: moduleRoot,
stdio: 'inherit'
} // Use shell's search path.
)
resolve(true)
async function retryUntil (fn, isSuccess = Boolean, opts = {}) {
const {
triesRemaining = process.env.GITHUB_WORKSPACE ? 30 : 10,
timeout = 1000
} = opts
return fn()
.then(result => {
if (isSuccess(result)) return result
else throw Error('retry failed')
})
.catch(async (err) => {
// out of tries, do not recurse
if (triesRemaining === 1) throw err
await promisify(setTimeout)(timeout)

return retryUntil(fn, isSuccess, {
triesRemaining: triesRemaining - 1,
timeout
})
})
}

async function isWebSocketReady (endpoint) {
return new Promise((resolve, reject) => {
try {
const ws = new WebSocket(endpoint)
ws.on('error', (ev) => {
ws.close()
reject(Error(ev.message))
})
ws.on('open', () => {
// NOTE: seems to be a sufficient test for our purposes!
resolve(true)
})
} catch (err) {
console.log('ws error', err.message)
reject(err)
}

})
}

export async function spinNetworkDown (networkType = 'two-nodes') {
try {
execFileSync('dev/bin/spin-down.sh', [networkType], {
shell: true,
cwd: moduleRoot,
stdio: 'inherit'
})
} catch (err) {
return Promise.reject(err)
}
}
47 changes: 47 additions & 0 deletions manifesto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Entropy Js SYSTEM OF BELIEF

### Definition SYSTEM OF BELIEF:

> According to Wikipedia, a belief system is a set of mutually supportive beliefs
> that can be religious, philosophical, political, ideological, or a combination
> of these. A belief system can also refer to a religion or a worldview.
> Beliefs can be categorized as basic or derived. Basic beliefs are not dependent
> on other beliefs for justification, but rather on something outside of the realm
> of belief. Derived beliefs depend on one or more basic beliefs for their validity.
> Belief system theory views personality structure as an organization of beliefs,
> attitudes, and values concerning the self and others.
> Some examples of belief systems include:
> Religious: Deals with morality, the existence of a god or gods, and the afterlife
> Philosophical: Discusses the human experience, such as the nature of knowledge
> and thought, what it means to exist, and the definition of reality
> Some estimates suggest that there are roughly 4,200 religions, churches,
> denominations, religious bodies, faith groups, tribes, cultures, movements,
> or ultimate concerns.
This document has two purposes it serves as a on boarding doc and
a place for us to agree on are values and motivation.

## Mission Statement:

> To have a healthy `entropy-*.js` community that delivers account management solutions and distributed signing while practicing solar punk values.
## SYSTEM OF BELIEF:

Personal and coding practices to live by:

- Take at least an "hour of joy" every day whatever that means to you just not writing more code
go out side and hug a tree or something like that. A well rounded developer is one that participates in life.

- Commit often and always
`wips: [context_here]` are great things it allows others to see what you are doing.

- Push up your branch At the end of your day and always look to see what should be pulled in the morning
`git pull origin` :yellow_heart:

> Git pull vs fetch: What's the difference?
> The key difference between git fetch and pull is that git pull merges changes from a remote repository directly into your working directory, while git fetch only copies changes into your local memory of remote branches.
if you are going to `fetch` don't forget the context your missing.

- Always add to the CHANGELOG.md file
- there's a `meta` section! if you don't feel comfortable adding to the meta section then please add to the `dev/README.md` or here to the `manifesto/README.md`
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"types": "dist/index.d.ts",
"scripts": {
"test": "yarn test:setup && yarn test:types && yarn test:ts && yarn test:only && yarn test:require && yarn test:import",
"test:setup": "./dev/bin/check-tools.sh && yarn removedb && yarn build",
"test:setup": "./dev/bin/check-tools.sh && yarn build",
"test:types": "tsc",
"test:ts": "./dev/bin/test-ts.sh",
"test:ts": "yarn removedb && ./dev/bin/test-ts.sh",
"test:require": "node tests/require.test.cjs | tap-spec",
"test:import": "node tests/import.test.mjs | tap-spec",
"test:only": "./dev/bin/test-only.sh",
Expand Down Expand Up @@ -92,11 +92,12 @@
"ts-prune": "^0.10.3",
"tsup": "^6.5.0",
"tsx": "^4.9.3",
"typedoc": "^0.25.13",
"typedoc": "^0.25.3",
"typedoc-plugin-merge-modules": "^5.1.0",
"typedoc-plugin-missing-exports": "^2.3.0",
"typedoc-plugin-rename-defaults": "^0.7.0",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"ws": "^8.17.1"
},
"dependencies": {
"@entropyxyz/entropy-protocol-nodejs": "^0.2.0",
Expand All @@ -105,7 +106,8 @@
"@types/node": "^20.12.12",
"debug": "^4.3.4",
"hpke-js": "^1.2.7",
"uuid": "^9.0.1"
"uuid": "^9.0.1",
"xtend": "^4.0.2"
},
"lint-staged": {
"*.ts": "eslint --fix"
Expand Down
49 changes: 27 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ApiPromise, WsProvider } from '@polkadot/api'
import { isValidSubstrateAddress } from './utils'
import xtend from 'xtend'
import { isValidSubstrateAddress as isDeployer } from './utils'
import RegistrationManager, { RegistrationParams } from './registration'
import SignatureRequestManager, { SigOps, SigWithAdapptersOps } from './signing'
import SignatureRequestManager, { SigOps, SigWithAdaptersOps } from './signing'
import { crypto, loadCryptoLib } from './utils/crypto'
import { Adapter } from './signing/adapters/types'
import ProgramManager from './programs'
Expand Down Expand Up @@ -130,41 +131,45 @@ export default class Entropy {
* @throws {Error} If the address is already registered or if there's a problem during registration.
*/
async register (params?: RegistrationParams): Promise<HexString> {
await this.ready
const defaultProgram = DEVICE_KEY_PROXY_PROGRAM_INTERFACE
params = params || {
programData: [defaultProgram],
programDeployer: this.keyring.accounts.registration.address,
}

const deviceKey = this.keyring.getLazyLoadAccountProxy(ChildKey.deviceKey)
deviceKey.used = true
defaultProgram.program_config.sr25519_public_keys.push(
Buffer.from(deviceKey.pair.publicKey).toString('base64')
)

if (
params.programDeployer &&
!isValidSubstrateAddress(params.programDeployer)
) {
params = params || this.#getRegisterParamsDefault()
if (params.programDeployer && !isDeployer(params.programDeployer)) {
throw new TypeError('Incompatible address type')
}

await this.ready
const verifyingKey = await this.registrationManager.register(params)

// TODO: Make legit function
const admin = this.keyring.getLazyLoadAccountProxy(ChildKey.registration)
const deviceKey = this.keyring.getLazyLoadAccountProxy(ChildKey.deviceKey)
const vk = admin.verifyingKeys || []

// HACK: these assignments trigger important `account-update` flows via the Proxy
admin.verifyingKeys = [...vk, verifyingKey]
deviceKey.verifyingKeys = [verifyingKey, ...vk]

return verifyingKey
}

/*
#getRegisterParamsDefault (): RegistrationParams {
const deviceKey = this.keyring.getLazyLoadAccountProxy(ChildKey.deviceKey)
deviceKey.used = true

const defaultProgram = xtend(DEVICE_KEY_PROXY_PROGRAM_INTERFACE, {
program_config: {
sr25519_public_keys: [
Buffer.from(deviceKey.pair.publicKey).toString('base64')
]
}
})

return {
programData: [defaultProgram],
programDeployer: this.keyring.accounts.registration.address,
}
}

/*
DO NOT DELETE THIS CODE BLOCK
Signs a given transaction based on the provided parameters.
Expand All @@ -178,12 +183,12 @@ export default class Entropy {
/**
* Signs a given transaction based on the provided parameters using the appropriate adapter.
*
* @param {SigWithAdapptersOps} params - The parameters for signing the transaction.
* @param {SigWithAdaptersOps} params - The parameters for signing the transaction.
* @returns {Promise<unknown>} A promise that resolves to the transaction signature.
* @throws {Error} If no adapter is found for the specified transaction type.
*/

async signWithAdaptersInOrder (params: SigWithAdapptersOps): Promise<unknown> {
async signWithAdaptersInOrder (params: SigWithAdaptersOps): Promise<unknown> {
await this.ready
return await this.signingManager.signWithAdaptersInOrder(params)
}
Expand Down
2 changes: 1 addition & 1 deletion src/signing/adapters/device-key-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const DEVICE_KEY_PROXY_PROGRAM_INTERFACE = {
],
}

export const ADAPTER_PROGRAMS = [DEVICE_KEY_PROXY_PROGRAM_INTERFACE]
// export const ADAPTER_PROGRAMS = [DEVICE_KEY_PROXY_PROGRAM_INTERFACE]

export interface PreSignResult extends PRESIGN_RESULT {
sigRequestHash: HexString
Expand Down
Loading

0 comments on commit 9f66f90

Please sign in to comment.