Skip to content

Commit

Permalink
Jawn/update core (#343)
Browse files Browse the repository at this point in the history
* wip removing permisioned

* wip: thinking about how to structure keys

* types generated (#307)

* updating to core v0.0.11

* smol fixes with types import and interfaces

* trying to work out private

* private mapping comparisons

* clean up

* some more cleanup for testing

* account id to validator id which is then used to get threshold server info .. language cleanup. removed comparison logic

* revert to update

* updated RegisteredInfo

* revert test

* remove resolution from package json

* set up listening event for AccountRegistered Event

* updating programs with verifying key

* verifying key

* new types

* new test

* inline doc

* check registratin status getting stuck

* test

* exiting from event sub

* some TO-DO notes

* keys WIP

* update polkadotjs

* wip: we really need somethi to describe entropy accounts... im starting to do that

* wip: "accounts" as keys

* wip: register

* Update README.md

* Update README.md

* wip: holy cow accounts

* syntax error on enum declaration in constant types file

* wip

* wip

* wip: keyring class

* WIP: update to core version 12

* wip: types && ish

* WIP: core update 12 cont

* wip: programs

* wip: cleaned up keys directory and corresponding types

* WIP: program updated core 12

* documentation

* trying to log verifying key

* generate yarn.lock

* WIP: programs

* wip: types

* wip: types and all the other things "one by one" need the right adapter need to finsish crypto global wait export just hitting each file while updating types

* wip: types and all the other things "one by one" need the right adapter need to finsish crypto global wait export just hitting each file while updating types

* WIP: added adapted for device key proxy

* wip: signTransaction -> signWithAdapters

* wip: replace "accounts with keyring"

* wip: replace "accounts with keyring"

* WIP: adapter types

* wip: array lol

* WIP: programs index verifying key

* wip: lol keys

* WIP: registration returning verifying keys

* wip: lol keys

* wip: fix secret key

* wip: fix secret key

* wip: fix secret key

* WIP: almost there.

* wip: types

* wip: removing eth mentions and updating type definitions to bunlde for cli

* wip: export keys utils

* fix deps

* WIP: update release script, added comment on proxy, updated singing index

* updating docs

* initial pass at inline-documentation

* wip: tests

* remove ethereum refference

* WIP: types doc -- first pass

* wip: accounts

* wip: types

* wip: i think im done editing types in keys for now need to figure out whats next to do in tests

* wip: nope still a type thing to fix

* EOD/WIP: working on getting tests type compatible its kind of a "process" starting with linting made code changes as i see them. Ive deffintaly notice that im replicating some work here maybe i slipped up by not commiting something over the weekend or maybe its someones ide or bad git habbits

* wip: lol accounts and minor fix to registration

* WIP: keys and program fixes

* WIP: keys

* WIP: end-to-end test fix

* end to end test

* wip: keys

* fix lint

* lint-fix

* WIP: src/index

* WIP: keys/index

* WIP: utils

* WIP: registration and utils

* docker image

* wip: crypto

* yarn

* types fix

* wip: crypto update

* crypto lib errors

* wip: Hpke

* wip: getting tests to pass

* wip: test data

* WIP: passing 32 bytes uint 8 array

* WIP: passing crypto test. please double check

* Delete docs/classes/extrinsic.default.md

* wip: updating path to crypto utils

* wip: moving crypto/types to crypto/types/index

* Delete src/keys/types/high-level.ts

* wip: updates from mass pr review for update-core

* wip: working on e2e tests

* wip

* wip: working on e2e tests

* WIP: derivation paths

* WIP: matching uuid

* WIP: derivation paths cont

* WIP: sorting out undefined object in get lazy load

* wip: keyring try 2: make stuff better? wrote some new functions next stepp make sure they are right

* wip: pushing up these changes to package json; their from mixs pr and are what fixed the linking of the sdk on cli

* wip: types and typeos still need to update everywher else

* wip: i think i have keys figured out now just need to get the rest of the tests

* wip: getting keys to match :{

* made keys match!

* wip: registering

* wip: updated keyring to pass address to individual entropy accounts properly; changed debug to type boolean instead of hardcoding true

* wip: getting the register event running into issues with tssnode

* add test gotchas, fix spin-up to show pull locally

* Add --no-sync flag for bootstrapping

added due to entropyxyz/entropy-core#784

* Can Register!

* console.log > debug

* wip: tests

* add noop program

* add substrate types just as a reffences not acctually running them for anything

* got programs working correctly

* WIP: program json includes

* WIP: debugging program issue, now at signing

* WIP: fixed pointer. cleaned up

* WIP: clean up before signing

* wip: final stretch for e2e test: working on signing

* WIP: error at txrequestdata

* WIP: error at formatTxRequest

* WIP: working but failing at runtime due to program error

* WIP: cleaned up runtime error

* WIP: got a signature back

* wip: mapping out where address differs upon registration and initialization of new keyring

* wip: exporting keys utils from sdk

* export

* wip: signWithAdapter

* updated type for pairmaterial as its the context type and not child key

* wip

* wip: admin type on entropy acct, debug now sets all keys as used

* WIP: stuck at invalid type map expected string

* 0.1.5-0

* WIP: sign test failing

* WIP: test sign fail

* wip: signing Error: request failed 500, Internal Server Error fetch: http://bob-tss-server:3002/user/sign_tx FULLRESPONSE: Parse transaction_request error

* wip: trying more things out

* 0.1.5-1

* update e2e tests

* wip: siging tests

* only run e2e test

* signing now works with noop program

* 0.1.5-2

* fix: getAccounts to always return the registering key

* fix: admin and register key should be the same... [no test written]

* 0.1.5-3

* keys: proxy try get

* Mixmix/fix tests (#347)

* tidy tests

* enable as many tests as possible

* fixed

* add close, add slowdown for github in end-to-end tests

* Naynayjessie/fix sign with adapter (#350)

* wip: adding jesses changes for signwithadapter without commenting out tests

* wip: kebab cased some things

* fix test

* wip: signWithAdapters -> signWithAdaptersInOrder

* wip: base adapter as noop

* fixed programs

* remove console.log

* remove console.log

* wrong type to pull in

---------

Co-authored-by: Nayyir Jutha <[email protected]>
Co-authored-by: Jesse Abramowitz <[email protected]>
Co-authored-by: frankie <[email protected]>

* wip: export cryptoWaid ready

* turn require test back on

* require.test.cjs passes again

---------

Co-authored-by: frankie <[email protected]>
Co-authored-by: Johnny <[email protected]>
Co-authored-by: Nayyir Jutha <[email protected]>
Co-authored-by: mix irving <[email protected]>
Co-authored-by: Zach Mandeville <[email protected]>
Co-authored-by: Nayyir Jutha <[email protected]>
Co-authored-by: Jesse Abramowitz <[email protected]>
  • Loading branch information
8 people authored May 24, 2024
1 parent 1353959 commit 737c248
Show file tree
Hide file tree
Showing 65 changed files with 4,470 additions and 3,053 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ module.exports = {
root: true,
rules: {
indent: ['error', 2],
'space-before-function-paren': 'off',
'space-before-function-paren': 2,
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/ban-ts-comment': 1,
'no-async-promise-executor': 'off',
},
ignorePatterns: ['node_modules/', 'dist/', 'examples/', '.yarnrc.yml'],
}
86 changes: 18 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,84 +36,34 @@ Below is an example that instantiates Entropy, deploys a program, registers usin
**`Example`**

```typescript
// get a Signer object from seed using util function
//store that private key
import { Keyring } from '@entropyxyz/sdk/keys'
import { wasmGlobalsReady, Entropy } from '@entropyxyz/sdk'

const signer = await getWallet(charlieStashSeed)
await wasmGlobalsReady()

// create an Entropy Account object
const newSeed = {seed || mnemonic}
const keyring = new Keyring(account)
// you should allways store what comes from this
let persistMe = keyring.accounts.toJson()
const saveToStorage = (state) => persistMe = state
keyring.accounts.on('account-update', (fullAccount) => { saveToStorage(fullAccount) })

const entropyAccount: EntropyAccount = {
sigRequestKey: signer,
programModKey: signer,
}

// initialize Entropy

const entropy = new Entropy({ account: entropyAccount })

// await entropy to be ready

await entropy.ready

// path to program wasm file

const basicTxProgram: any = readFileSync(
'./tests/testing-utils/template_basic_transaction.wasm'
)

// returns pointer hash

const pointer = await entropy.programs.dev.deploy(basicTxProgram)

// configuration object
let entropy = New Entropy({keyring, endpoint})
// session end

const config = `
{
"allowlisted_addresses": [
"772b9a9e8aa1c9db861c6611a82d251db4fac990"
]
}
`
// converts config to bytes
// new session with same account as before
// the second time you use entropy:
const loadedFromStorage = persistMe

const encoder = new TextEncoder()
const byteArray = encoder.encode(config)
const newKeyring = new Keyring(loadFromStorage)

// converts U8Array to hex
keyring.accounts.on('account-update', (fullAccountAsJSON) => { saveToStorage(fullAccountAsJSON) })

const programConfig = util.u8aToHex(new Uint8Array(byteArray))

// construct Program Data

const programData: ProgramData = {
programPointer: pointer,
programConfig: programConfig,
}

// attempt user registration

await entropy.register({
keyVisibility: 'Permissioned',
initialPrograms: [programData],
programModAccount: 'insert ProgramModAccount address',
})

// basic transaction composition

const basicTx = {
to: '0x772b9a9e8aa1c9db861c6611a82d251db4fac990',
value: 1,
chainId: 1,
nonce: 1,
data: '0x' + Buffer.from('Created On Entropy').toString('hex'),
}
entropy = new Entropy({keyring: newKeyring, endpoint})

// get entropy signature

const signature = (await entropy.signTransaction({
txParams: basicTx,
type: 'eth',
})) as string
```

## Table of contents
Expand Down
29 changes: 29 additions & 0 deletions dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,35 @@ You can close them down like this:
docker compose --file dev/docker-scripts/two-nodes.yaml down
```

### Gotcha 2 - ports still in use?

Is something still using port 9944?

```bash
ps auxw | grep 9944
```

NOTE: this just kills + removes everything

```bash
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
```

### Gotcha 3 - `latest` tag

If you use the `latest` tag ... this is a moving target

```bash
export ENTROPY_CORE_VERSION=latest
```

If you must do this you should run

```bash
docker compose --file dev/docker-scripts/two-nodes.yaml pull
```

## When updating core version:

1. change docker version in `./dev/bin/ENTROPY_CORE_VERSION.sh`
Expand Down
2 changes: 1 addition & 1 deletion dev/bin/ENTROPY_CORE_VERSION.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#! /usr/bin/env bash

export ENTROPY_CORE_VERSION=test-hc-release-00c87d6a4-testnet-launch-params
export ENTROPY_CORE_VERSION=release-v0.1.0-rc.1
6 changes: 3 additions & 3 deletions dev/bin/generate-types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@


# pull meta data write to file
curl -H "Content-Type: application/json" -d '{"id":"1", "jsonrpc":"2.0", "method": "state_getMetadata", "params":[]}' http://127.0.0.1:9944 > entropy-metadata.json &&
curl -H "Content-Type: application/json" -d '{"id":"1", "jsonrpc":"2.0", "method": "state_getMetadata", "params":[]}' http://127.0.0.1:9944 > entropy-metadata.scale &&
# generate types

# node --experimental-specifier-resolution=node --loader ts-node/esm node_modules/.bin/polkadot-types-from-defs --package dscp-matchmaker-api/interfaces --input ./node_modules/@polkadot/types/ --endpoint ./entropy-metadata.json &&
# node --experimental-specifier-resolution=node --loader ts-node/esm node_modules/.bin/polkadot-types-from-chain --endpoint ./entropy-metadata.json --output ./substrate-generated-types/interfaces

# "generate:defs":
ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package ./node_modules/@polkadot/types/ --input ./substrate-generated-types --endpoint entropy-metadata.json &&
ts-node --skip-project node_modules/.bin/polkadot-types-from-defs --package ./node_modules/@polkadot/types/ --input ./substrate-generated-types --endpoint entropy-metadata.scale &&
# "generate:meta":
ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package ./node_modules/@polkadot/types/ --endpoint entropy-metadata.json --output ./substrate-generated-types
ts-node --skip-project node_modules/.bin/polkadot-types-from-chain --package ./node_modules/@polkadot/types/ --endpoint entropy-metadata.scale --output ./substrate-generated-types
2 changes: 1 addition & 1 deletion dev/bin/spin-up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ docker_file=dev/docker-scripts/$1.yaml
if [ $GITHUB_WORKSPACE ]; then
docker compose --file $docker_file up --detach --quiet-pull;
else
docker compose --file $docker_file up --detach --quiet-pull;
docker compose --file $docker_file up --detach;
fi
5 changes: 0 additions & 5 deletions entropy-metadata-bkp.json

This file was deleted.

1 change: 0 additions & 1 deletion entropy-metadata.json

This file was deleted.

1 change: 1 addition & 0 deletions entropy-metadata.scale

Large diffs are not rendered by default.

42 changes: 25 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"name": "@entropyxyz/sdk",
"version": "0.1.4",
"version": "0.1.5-3",
"license": "AGPL-3.0-only",
"description": "JS SDK for entropy blockchain ",
"files": [
"dist",
"dist/keys",
"dist/keys/utils.ts",
"dist/utils"
],
"type": "module",
"exports": {
".": {
Expand All @@ -15,6 +21,11 @@
"require": "./dist/keys/index.cjs",
"default": "./dist/keys/index.js"
},
"./keys/utils": {
"module": "./dist/keys/utils.js",
"require": "./dist/keys/utils.cjs",
"default": "./dist/keys/index.js"
},
"./crypto-utils": {
"module": "./dist/utils/crypto.js",
"require": "./dist/utils/crypto.cjs",
Expand All @@ -27,18 +38,13 @@
}
},
"types": "dist/index.d.ts",
"files": [
"dist",
"dist/keys",
"dist/utils"
],
"scripts": {
"test": "yarn test:setup && yarn test:import && yarn test:require yarn test:types && yarn test:ts && yarn test:only",
"test:setup": "yarn bundle && ./dev/bin/check-tools.sh && yarn removedb",
"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 bundle",
"test:types": "tsc",
"test:ts": "set -e; for t in tests/*.test.ts; do npx tsx $t | tap-spec; done",
"test:import": "node tests/import.test.mjs | tap-spec",
"test:require": "node tests/require.test.cjs | tap-spec",
"test:import": "node tests/import.test.mjs | tap-spec",
"test:only": "if grep -r --exclude-dir=node_modules --exclude-dir=.git --color 'test\\.only' ; then exit 1; fi",
"test:coverage": "nyc yarn test",
"lint": "yarn lint:ts && yarn lint:eslint",
Expand Down Expand Up @@ -88,21 +94,23 @@
"ts-node": "^10.9.1",
"ts-prune": "^0.10.3",
"tsup": "^6.5.0",
"tsx": "^4.9.3",
"typedoc": "^0.25.3",
"typedoc-plugin-markdown": "^3.17.0",
"typescript": "^4.9.5"
},
"dependencies": {
"@entropyxyz/entropy-protocol-nodejs": "^0.1.0",
"@entropyxyz/entropy-protocol-web": "^0.1.0",
"@polkadot/api": "^10.11.1",
"@types/node": "^20.10.3",
"ethereum-cryptography": "^2.1.2",
"ethereumjs-tx": "^2.1.2",
"tsx": "^4.9.3"
"@entropyxyz/entropy-protocol-nodejs": "^0.2.0",
"@entropyxyz/entropy-protocol-web": "^0.2.0",
"@polkadot/api": "^10.13.1",
"@types/node": "^20.12.12",
"debug": "^4.3.4",
"hpke-js": "^1.2.7",
"uuid": "^9.0.1"
},
"lint-staged": {
"*.ts": "eslint --fix",
"*.{js,ts,json,md}": "prettier --write"
}
},
"packageManager": "[email protected]"
}
60 changes: 16 additions & 44 deletions src/extrinsic/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { ApiPromise, SubmittableResult } from '@polkadot/api'
import { Signer, EventFilter } from '../types'
import { EventRecord } from '@polkadot/types/interfaces/types'
import { SubmittableExtrinsic } from '@polkadot/api/types'
import { RegistryError } from '@polkadot/types-codec/types'

import { Signer } from '../keys/types/internal'
import { EventFilter } from '../types/internal'

interface Decoded extends RegistryError {
name: string
docs: string[]
section: string
}

/**
* A utility class to simplify extrinsic operations with the Polkadot/Substrate API.
* Allows the user to send extrinsics and automatically handles errors, events, and certain special conditions like free transactions.
*
* A utility class to simplify extrinsic operations with Entropy
* Allows the user to send extrinsics and automatically handles errors, and events
*/

export default class ExtrinsicBaseClass {
substrate: ApiPromise
signer: Signer
Expand All @@ -27,30 +26,27 @@ export default class ExtrinsicBaseClass {
* @param signer - The signer object containing the wallet and other signing-related functionalities.
*/

constructor({ substrate, signer }) {
constructor ({ substrate, signer }) {
this.substrate = substrate
this.signer = signer
}

/**
* Sends an extrinsic and waits for a specific event or rejects with an error.
*
* @param call - The extrinsic call to send.
* @param freeTx - Optional. Flag indicating if the transaction should be free (default: false).
* @param filter - An event filter to wait for.
* @returns A promise that resolves with the filtered event record.
* @throws {Error} Will reject the promise if a dispatch error occurs or the filtered event is not found.
*/

async sendAndWaitFor(
async sendAndWaitFor (
call: SubmittableExtrinsic<'promise'>,
freeTx = false,
filter: EventFilter
): Promise<EventRecord> {
const newCall = freeTx ? await this.handleFreeTx(call) : call
const pair = this.signer.pair
this.signer.used = true
return new Promise<EventRecord>((resolve, reject) => {
newCall
.signAndSend(this.signer.wallet, (res: SubmittableResult) => {
call
.signAndSend(pair, (res: SubmittableResult) => {
const { dispatchError, status } = res
if (dispatchError) {
if (dispatchError.isModule) {
Expand All @@ -66,11 +62,14 @@ export default class ExtrinsicBaseClass {
}
}
if (status.isInBlock || status.isFinalized) {
const record = res.findRecord(filter.section, filter.name)
const record: EventRecord = res.findRecord(
filter.section,
filter.name
)
if (record) {
resolve(record)
} else {
reject(Error('Event record not found'))
reject({ message: 'Event record not found', records: res })
}
}
})
Expand All @@ -79,31 +78,4 @@ export default class ExtrinsicBaseClass {
})
})
}

/**
* Prepares a free transaction, performs a dry run, and ensures its viability.
*
* In this system:
* - **Electricity** represents an energy unit allowing certain transactions to bypass traditional fees.
* - An account's **Zaps** represent the available electricity it has. Consuming zaps results in transaction execution without fees.
* - **Batteries** are rechargeable units in an account that generate zaps over time.
*
* This method leverages the `callUsingElectricity` from the `freeTx` module to create a transaction that utilizes zaps.
* A dry run is then performed to ensure its success when broadcasted.
*
* @param call - The extrinsic intended for execution.
* @returns A promise resolving to a transaction prepared to use electricity.
* @throws {Error} If the dry run fails or there's insufficient electricity (zaps).
*/

async handleFreeTx(
call: SubmittableExtrinsic<'promise'>
): Promise<SubmittableExtrinsic<'promise'>> {
const freeTxWrapper = this.substrate.tx.freeTx.callUsingElectricity(call)
const result = await freeTxWrapper.dryRun(this.signer.wallet)
if (result.isErr) {
throw new Error(result.toString())
}
return freeTxWrapper
}
}
Loading

0 comments on commit 737c248

Please sign in to comment.