-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 () => { | ||
|
||
// 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); | ||
}) | ||
}) |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
} | ||
|
||
} | ||
|
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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.