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/pkp sui #185

Merged
merged 6 commits into from
Jul 20, 2023
Merged
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
1 change: 1 addition & 0 deletions lit.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"PKP_PUBKEY": "049bc4a7b33316170694f6ca6d5917af9c4a492f91636b71105059e244f558b5a99c395001390f50c82aa8b44448984218a306951a30b0e57f133062dc9ceacefc",
"PKP_ETH_ADDRESS": "0x016013f36abb93F6304eC0aBAbe5b0F3b6636579",
"PKP_COSMOS_ADDRESS": "cosmos15f53jgu645yngkkzf8q7qz9cl93exn67uatdkf",
"PKP_SUI_ADDRESS": "0x18b33e1afd494e49cc8ce58cd852bd1ec5e8bfd0ce2d387158c7d83a2276a398",
"HEX_TEST_MEMO": "0x4a532d53444b2054657374",

"PKP_PUBKEY_2": "04a9c9a5db5472e6fac05ec001b804d3daa340a9b791e75dd52180312c7f0d4e806150473da6c785fadd8050ced5aada250146a97d928d0deb8b584bc7f169f10a",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@cypress/react": "^6.2.0",
"@cypress/webpack-dev-server": "^2.3.0",
"@metamask/eth-sig-util": "5.0.2",
"@mysten/sui.js": "^0.37.1",
"@playwright/test": "^1.25.2",
"@simplewebauthn/browser": "^7.2.0",
"@simplewebauthn/typescript-types": "^7.0.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/pkp-sui/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@nrwl/web/babel",
{
"useBuiltIns": "usage"
}
]
]
}
18 changes: 18 additions & 0 deletions packages/pkp-sui/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
12 changes: 12 additions & 0 deletions packages/pkp-sui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# pkp-sui

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build pkp-sui` to build the library.

## Running unit tests

Run `nx test pkp-sui` to execute the unit tests via [Jest](https://jestjs.io).
If test 3,4 fail, It means sui testnet has been reset. Use sui testnet faucet to get some sui and try again.
16 changes: 16 additions & 0 deletions packages/pkp-sui/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable */
export default {
displayName: 'pkp-sui',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/packages/pkp-sui',
setupFilesAfterEnv: ['../../jest.setup.js'],
};
30 changes: 30 additions & 0 deletions packages/pkp-sui/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@lit-protocol/pkp-sui",
"version": "0.0.1",
"type": "commonjs",
"license": "MIT",
"homepage": "https://github.com/Lit-Protocol/js-sdk",
"repository": {
"type": "git",
"url": "https://github.com/Lit-Protocol/js-sdk"
},
"keywords": [
"library"
],
"bugs": {
"url": "https://github.com/Lit-Protocol/js-sdk/issues"
},
"publishConfig": {
"access": "public",
"directory": "../../dist/packages/pkp-sui"
},
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts",
"browser": {
"crypto": false,
"stream": false
},
"tags": [
"universal"
]
}
70 changes: 70 additions & 0 deletions packages/pkp-sui/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "pkp-sui",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pkp-sui/src",
"projectType": "library",
"targets": {
"build": {
"executor": "nx:run-commands",
"options": {
"command": "yarn build:target pkp-sui"
}
},
"_buildTsc": {
"executor": "@nrwl/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pkp-sui",
"main": "packages/pkp-sui/src/index.ts",
"tsConfig": "packages/pkp-sui/tsconfig.lib.json",
"assets": [
"packages/pkp-sui/*.md"
]
}
},
"_buildWeb": {
"executor": "@websaam/nx-esbuild:package",
"options": {
"globalName": "LitJsSdk_pkpSui",
"outfile": "dist/packages/pkp-sui-vanilla/pkp-sui.js",
"entryPoints": [
"./packages/pkp-sui/src/index.ts"
],
"define": {
"process.env.NODE_DEBUG": "false",
"global": "window"
},
"plugins": [
{
"package": "esbuild-node-builtins",
"function": "nodeBuiltIns"
}
]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"packages/pkp-sui/**/*.ts"
]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": [
"coverage/packages/pkp-sui"
],
"options": {
"jestConfig": "packages/pkp-sui/jest.config.ts",
"passWithNoTests": true
}
}
},
"tags": []
}
3 changes: 3 additions & 0 deletions packages/pkp-sui/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PKPSuiWallet } from './lib/pkp-sui';

export { PKPSuiWallet };
17 changes: 17 additions & 0 deletions packages/pkp-sui/src/lib/TransactionBlockData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { hashTypedData } from './hash';
import bs58 from 'bs58';


/**
* Generate transaction digest.
*
* @param bytes BCS serialized transaction data
* @returns transaction digest.
*/
export function getDigestFromBytes(bytes: Uint8Array) {
const hash = hashTypedData('TransactionData', bytes);
return bs58.encode(hash);
}
20 changes: 20 additions & 0 deletions packages/pkp-sui/src/lib/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { blake2b } from '@noble/hashes/blake2b';

/**
* Generates a Blake2b hash of typed data as a base64 string.
*
* @param typeTag type tag (e.g. TransactionData, SenderSignedData)
* @param data data to hash
*/
export function hashTypedData(typeTag: string, data: Uint8Array): Uint8Array {
const typeTagBytes = Array.from(`${typeTag}::`).map((e) => e.charCodeAt(0));

const dataWithTag = new Uint8Array(typeTagBytes.length + data.length);
dataWithTag.set(typeTagBytes);
dataWithTag.set(data, typeTagBytes.length);

return blake2b(dataWithTag, { dkLen: 32 });
}
84 changes: 84 additions & 0 deletions packages/pkp-sui/src/lib/pkp-sui.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
JsonRpcProvider,
testnetConnection,
TransactionBlock,
} from '@mysten/sui.js';

import * as LITCONFIG from 'lit.config.json';

jest.useRealTimers();

describe('PKPSuiWallet', () => {
it('should create a wallet', async () => {
const { PKPSuiWallet } = await import('./pkp-sui');
const wallet = new PKPSuiWallet(
{
controllerAuthSig: LITCONFIG.CONTROLLER_AUTHSIG,
pkpPubKey: LITCONFIG.PKP_PUBKEY,
},
new JsonRpcProvider(testnetConnection)
);
const address = await wallet.getAddress();
expect(address).toEqual(LITCONFIG.PKP_SUI_ADDRESS);
});
it('should connects to lit node client', async () => {
const { PKPSuiWallet } = await import('./pkp-sui');
const wallet = new PKPSuiWallet(
{
controllerAuthSig: LITCONFIG.CONTROLLER_AUTHSIG,
pkpPubKey: LITCONFIG.PKP_PUBKEY,
},
new JsonRpcProvider(testnetConnection)
);
await wallet.init();
expect(wallet.litNodeClientReady).toEqual(true);
});
it('should retrieve account balance', async () => {
const { PKPSuiWallet } = await import('./pkp-sui');
const provider = new JsonRpcProvider(testnetConnection);
const wallet = new PKPSuiWallet(
{
controllerAuthSig: LITCONFIG.CONTROLLER_AUTHSIG,
pkpPubKey: LITCONFIG.PKP_PUBKEY,
},
provider
);
const address = await wallet.getAddress();
const balance = await provider.getBalance({
owner: address,
});
expect(parseInt(balance.totalBalance)).toBeGreaterThanOrEqual(1000);
});

it('should send a transaction to itself', async () => {
const { PKPSuiWallet } = await import('./pkp-sui');
const wallet = new PKPSuiWallet(
{
controllerAuthSig: LITCONFIG.CONTROLLER_AUTHSIG,
pkpPubKey: LITCONFIG.PKP_PUBKEY,
},
new JsonRpcProvider(testnetConnection)
);
const address = await wallet.getAddress();
const tx = new TransactionBlock();
const [coin] = tx.splitCoins(tx.gas, [tx.pure(1000)]);
tx.transferObjects([coin], tx.pure(address));

const dryTransaction = await wallet.dryRunTransactionBlock({
transactionBlock: tx,
});

expect(dryTransaction.effects.status.status).toEqual('success');

// This will only send a transaction if the test flag is set to true
if (LITCONFIG.test.sendRealTxThatCostsMoney) {
const transaction = await wallet.signAndExecuteTransactionBlock({
transactionBlock: tx,
});
expect(transaction.digest).toBeDefined();
// expect transaction.digest to be string of length 44
expect(transaction.digest.length).toEqual(44);
}

}, 60000);
});
Loading