Skip to content

Commit

Permalink
test signWithAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Jun 27, 2024
1 parent 1a32e94 commit f0f71c9
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 23 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
"test": "yarn test:types && yarn test:ts && yarn test:only",
"lint": "eslint . --ext .ts --fix",
"test:only": "./dev/bin/test-only.sh",
"test:ts": "./dev/bin/test-ts.sh",
"test:ts": "yarn removedb && ./dev/bin/test-ts.sh",
"test:types": "tsc --project tsconfig.json",
"prepare": "husky",
"postinstall": "husky",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
"link:sdk": "yarn link @entropyxyz/sdk",
"unlink:sdk": "yarn unlink @entropyxyz/sdk",
"re-link:sdk": "yarn unlink:sdk && yarn link:sdk"
"re-link:sdk": "yarn unlink:sdk && yarn link:sdk",
"removedb": "rm -rf .entropy && docker compose --file node_modules/@entropyxyz/sdk/dev/docker-scripts/two-nodes.yaml down 2> /dev/null"
},
"files": [
"dist"
Expand Down
17 changes: 9 additions & 8 deletions src/common/initializeEntropy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,35 @@ const keyrings = {
export function getKeyring (address) {
if (!address && keyrings.default) return keyrings.default
if (address && keyrings[address]) return keyrings[address]
// explicitly return undefined so there is no confusion around what is selected
// explicitly return undefined so there is no confusion around what is selected
return undefined
}

interface InitializeEntropyOpts {
keyMaterial: MaybeKeyMaterial,
password?: string,
endpoint: string
endpoint: string,
configPath?: string // for testing
}
type MaybeKeyMaterial = EntropyAccountData | string

// WARNING: in programatic cli mode this function should NEVER prompt users, but it will if no password was provided
// This is currently caught earlier in the code
export const initializeEntropy = async ({ keyMaterial, password, endpoint }: InitializeEntropyOpts): Promise<Entropy> => {
export const initializeEntropy = async ({ keyMaterial, password, endpoint, configPath }: InitializeEntropyOpts): Promise<Entropy> => {
try {
// if (defaultAccount && defaultAccount.seed === keyMaterial.seed) return entropys[defaultAccount.registering.address]
await wasmGlobalsReady()

const { accountData, password: successfulPassword } = await getAccountDataAndPassword(keyMaterial, password)
// check if there is no admin account and no seed so that we can throw an error
// check if there is no admin account and no seed so that we can throw an error
if (!accountData.seed && !accountData.admin) {
throw new Error("Data format is not recognized as either encrypted or unencrypted")
}

if (accountData && accountData.admin && !accountData.registration) {
accountData.registration = accountData.admin
accountData.registration.used = true // TODO: is this even used?
const store = await config.get()
const store = await config.get(configPath)
store.accounts = store.accounts.map((account) => {
if (account.address === accountData.admin.address) {
let data = accountData
Expand All @@ -61,7 +62,7 @@ export const initializeEntropy = async ({ keyMaterial, password, endpoint }: Ini
return account
})
// re save the entire config
await config.set(store)
await config.set(store, configPath)
}

let selectedAccount
Expand All @@ -70,7 +71,7 @@ export const initializeEntropy = async ({ keyMaterial, password, endpoint }: Ini
if(!storedKeyring) {
const keyring = new Keyring({ ...accountData, debug: true })
keyring.accounts.on('account-update', async (newAccountData) => {
const store = await config.get()
const store = await config.get(configPath)
store.accounts = store.accounts.map((account) => {
if (account.address === store.selectedAccount) {
let data = newAccountData
Expand All @@ -85,7 +86,7 @@ export const initializeEntropy = async ({ keyMaterial, password, endpoint }: Ini
})

// re save the entire config
await config.set(store)
await config.set(store, configPath)

})
keyrings.default = keyring
Expand Down
23 changes: 23 additions & 0 deletions src/flows/sign/sign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface SignWithAdapterInput {
/** the message as a utf-8 encoded string */
msg: string,
verifyingKey?: string,
// LATER:
// auxillaryData: any
}

function stringToHex (str: string): string {
return Buffer.from(str).toString('hex')
}

export async function signWithAdapter (entropy, input: SignWithAdapterInput) {
return entropy.signWithAdaptersInOrder({
msg: {
msg: stringToHex(input.msg)
},
// type
order: ['deviceKeyProxy'],
signatureVerifyingKey: input.verifyingKey
// auxillaryData
})
}
6 changes: 2 additions & 4 deletions tests/balance.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import test from 'tape'

import { getBalance, getBalances } from '../src/flows/balance/balance'
import { setupTest } from './testing-utils'
import { setupTest, charlieStashAddress as richAddress } from './testing-utils'

const networkType = 'two-nodes'
// TODO: export charlieStashSeed
const richAddress = '5Ck5SLSHYac6WFt5UZRSsdJjwmpSZq85fd5TRNAdZQVzEAPT'

test('getBalance + getBalances', async (t) => {
const { run, entropy } = await setupTest(t, networkType)
const { run, entropy } = await setupTest(t, { networkType })

const newAddress = entropy.keyring.accounts.registration.address

Expand Down
21 changes: 21 additions & 0 deletions tests/sign.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'tape'

import { signWithAdapter } from '../src/flows/sign/sign'
import { setupTest, charlieStashSeed } from './testing-utils'

test('Sign - signWithAdapter', async (t) => {
const { run, entropy } = await setupTest(t, { seed: charlieStashSeed })

await run('register', entropy.register())

const signature = await run(
'sign',
signWithAdapter(entropy, { msg: "heyo!" })
)

t.true(signature && signature.length > 32, 'signature has some body!')
signature && console.log(signature)

t.end()
})

2 changes: 1 addition & 1 deletion tests/testing-utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export const charlieStashAddress =
'5Ck5SLSHYac6WFt5UZRSsdJjwmpSZq85fd5TRNAdZQVzEAPT'

export const charlieStashSeed =
'0x66256c4e2f90e273bf387923a9a7860f2e9f47a1848d6263de512f7fb110fc08'
'0x66256c4e2f90e273bf387923a9a7860f2e9f47a1848d6263de512f7fb110fc08'
1 change: 1 addition & 0 deletions tests/testing-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
spinNetworkDown,
}

export * from './constants'
export * from './setup-test'

/* Helper for wrapping promises which makes it super clear in logging if the promise
Expand Down
37 changes: 29 additions & 8 deletions tests/testing-utils/setup-test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
import { wasmGlobalsReady } from '@entropyxyz/sdk'
import { Test } from 'tape'
import { Entropy, wasmGlobalsReady } from '@entropyxyz/sdk'
// @ts-ignore
import { spinNetworkUp, spinNetworkDown, } from "@entropyxyz/sdk/testing"

import { initializeEntropy } from '../../src/common/initializeEntropy'
import * as config from '../../src/config'
import { makeSeed, promiseRunner, sleep } from './'

interface SetupTestOpts {
configPath?: string
networkType?: string
seed?: string,
}
const NETWORK_TYPE_DEFAULT = 'two-nodes'
let counter = 0

export async function setupTest (t: Test, opts?: SetupTestOpts): Promise<{ entropy: Entropy; run: any }> {
const {
configPath = `/tmp/entropy-cli-${Date.now()}_${counter++}.json`,
networkType = NETWORK_TYPE_DEFAULT,
seed = makeSeed()
} = opts || {}

export async function setupTest (t, networkType = NETWORK_TYPE_DEFAULT) {
const run = promiseRunner(t)

await run('wasm', wasmGlobalsReady())
await run('network up', spinNetworkUp(networkType))
// this gets called after all tests are run
// this gets called after t.end() is called
t.teardown(async () => {
await entropy.close()
await spinNetworkDown(networkType).catch((error) =>
console.error('Error while spinning network down', error.message)
)
})

await run('config.init', config.init(configPath))

// TODO: remove this after new SDK is published
await sleep(process.env.GITHUB_WORKSPACE ? 30_000 : 5_000)

const newSeed = makeSeed()
const entropy = await initializeEntropy({ keyMaterial: { seed: newSeed, debug: true }, endpoint: 'ws://127.0.0.1:9944', })
const newAddress = entropy.keyring.accounts.registration.address

const entropy = await initializeEntropy({
keyMaterial: { seed, debug: true },
endpoint: 'ws://127.0.0.1:9944',
configPath
})

await run('entropy ready', entropy.ready)

return { t, run, entropy }
return { entropy, run }
}

0 comments on commit f0f71c9

Please sign in to comment.