Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1208904834486479 evm compatible generate tx did #33

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
101 changes: 101 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"repository": "https://github.com/peaqnetwork/peaq-js.git",
"dependencies": {
"@polkadot/api": "12.4.2",
"ethers": "^6.13.4",
"peaq-did-proto-js": "^2.1.0",
"uuid": "^9.0.0"
},
Expand Down
56 changes: 56 additions & 0 deletions packages/sdk/src/modules/eth/did/did.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import dotenv from 'dotenv';
import { Main as SDK } from '../main';
import { ethers, toUtf8String} from 'ethers';

dotenv.config();

/**
* Global variables used to initialize the BASE_URL, and seed phrases that are used to create wallets.
*
* Address error used multiple times, so it is initialized here.
*/
const BASE_URL = process.env['BASE_URL'] as string;
const SEED = process.env['SEED'] as string;
const SEED2 = process.env['SEED2'] as string;
const ETH_PRIVATE2 = process.env['ETH_PRIVATE2'] as string;


/**
* Tests functionality in the sdk for DID ethereum operations.
*
*
*/
describe('Ethereum Did', () => {
let sdk: SDK;

beforeAll(async () => {
sdk = await SDK.createEth();
}, 40000);

afterAll(async () => {
});

/**
* Tests the generation of a DID operations for ethereum tx values
*/
describe('test ethereum did()', () => {
it('test create did tx and send', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how a user would use the sdk.


// create transaction to send
const tx = await sdk.did.create({name: 'test1', address: '0x48C9774C88736F7c169D2598278876727AFD3599'});
expect(tx.to).toBe('0x0000000000000000000000000000000000000800');
expect(tx.data).toMatch(/^0x([0-9a-fA-F]*)$/);

// send transaction
const provider = new ethers.WebSocketProvider(BASE_URL);
const signer = new ethers.Wallet(ETH_PRIVATE2, provider);
const response = await signer.sendTransaction(tx);

// make sure the tx was sent to the right precompile from my address with the hex data
expect(response.to).toBe('0x0000000000000000000000000000000000000800');
expect(response.from).toBe('0x48C9774C88736F7c169D2598278876727AFD3599');
expect(response.data).toMatch(/^0x([0-9a-fA-F]*)$/);

}, 50000);
})
})
86 changes: 86 additions & 0 deletions packages/sdk/src/modules/eth/did/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Base } from '../../base';
import { CustomDocumentFields, Did } from '../../did';
import * as peaqDidProto from 'peaq-did-proto-js';
import { hexToU8a } from '@polkadot/util';

import { ethers } from 'ethers';


enum FunctionSignatures {
ADD_ATTRIBUTE = "addAttribute(address,bytes,bytes,uint32)",
READ_ATTRIBUTE = "readAttribute(address,bytes)",
UPDATE_ATTRIBUTE = "updateAttribute(address,bytes,bytes,uint32)",
REMOVE_ATTRIBUTE = "removeAttribute(address,bytes)"
}

enum PrecompileAddresses {
DID = "0x0000000000000000000000000000000000000800"
}

interface CreateDidOptions {
name: string;
address: string;
customDocumentFields?: CustomDocumentFields;
}

/**
* DID class to create ethereum transactions.
*/
export class Eth_Did extends Base {
abiCoder: ethers.AbiCoder = new ethers.AbiCoder();
value: Did = new Did();
Copy link
Contributor Author

@jpgundrum jpgundrum Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to did


constructor() {
super();
}

// sdk.eth.did.create()
public async create(options: CreateDidOptions) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tx creation the same as was done in the particle-peaq-demo/src/App.tsx

const { name, address, customDocumentFields } = options;

if(!ethers.isAddress(address)){
throw new Error("Not valid address")
}

const createDidFunctionSelector = ethers.keccak256(ethers.toUtf8Bytes(FunctionSignatures.ADD_ATTRIBUTE)).substring(0, 10);

const didAddress = address;
const didName = ethers.hexlify(ethers.toUtf8Bytes(name));

const didDocHash = await this.value.generate({address, customDocumentFields});
const validityFor = 0;

// // to log document that is being added can uncomment below (useful for development debugging)
// const document = peaqDidProto.Document.deserializeBinary(hexToU8a(didDocHash.value));
// console.log(document.toObject());

const params = this.abiCoder.encode(
["address", "bytes", "bytes", "uint32"],
[didAddress, didName, didDocHash.value, validityFor]
);

let payload = params.replace("0x", createDidFunctionSelector);

const tx = {
to: PrecompileAddresses.DID,
data: payload
};

return tx;
}

// sdk.eth.did.read()
public async read() {

}

// sdk.eth.did.update()
public async update() {
}

// sdk.eth.did.remove()
public async remove() {
}

}

29 changes: 29 additions & 0 deletions packages/sdk/src/modules/eth/main/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Base } from '../../base';
import { Eth_Did } from '../did';


/**
* Main class for interacting with the SDK on the Ethereum side.
*/
export class Main extends Base {
// TODO add rbac and storage when developed
public did: Eth_Did;

constructor() {
super();
this.did = new Eth_Did();
}


/**
* Creates a new instance of the Ethereum's main class to call DID operations.
*
* @param
* @returns The callable sdk methods.
*/
public static async createEth() {
const sdk = new Main();
return sdk;
}

}