Skip to content

Commit

Permalink
tidy up + make #register safer (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix authored Jun 24, 2024
1 parent 674a810 commit a89c439
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
"tsx": "^4.9.3",
"typedoc": "^0.25.3",
"typedoc-plugin-markdown": "^3.17.0",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"xtend": "^4.0.2"
},
"dependencies": {
"@entropyxyz/entropy-protocol-nodejs": "^0.2.0",
Expand Down
50 changes: 27 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 { crypto, loadCryptoLib } from './utils/crypto'
Expand Down Expand Up @@ -101,41 +102,44 @@ 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> {
const defaultProgram = DEVICE_KEY_PROXY_PROGRAM_INTERFACE

params = params || {
programData: [defaultProgram],
programDeployer: this.keyring.accounts.registration.address,
}

await Promise.all([this.ready, this.substrate.isReady])

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)
// fuck frankie TODO: Make legit function

// 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 @@ -155,7 +159,7 @@ export default class Entropy {
*/

async signWithAdaptersInOrder (params: SigWithAdapptersOps): Promise<unknown> {
(await this.ready) && this.substrate.isReady
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
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4981,7 +4981,7 @@ ws@^8.15.1, ws@^8.8.1:
resolved "https://registry.npmjs.org/ws/-/ws-8.17.0.tgz"
integrity sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==

xtend@~4.0.1:
xtend@^4.0.2, xtend@~4.0.1:
version "4.0.2"
resolved "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz"
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
Expand Down

0 comments on commit a89c439

Please sign in to comment.