Skip to content

Commit

Permalink
Merge pull request #26 from nervosnetwork/rc/v0.1.0-alpha.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored Dec 11, 2020
2 parents b2e69c6 + a56c71f commit b17220c
Show file tree
Hide file tree
Showing 29 changed files with 529 additions and 36 deletions.
30 changes: 30 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceRoot}/packages/ui/src"
},
{
"name": "keypering",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}/packages/app",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
},
"args": ["."],
"outputCapture": "std",
"env": {
"NODE_ENV": "development"
}
}
]
}
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.1.0-alpha.3](https://github.com/nervosnetwork/keypering/compare/v0.1.0-alpha.2...v0.1.0-alpha.3) (2020-12-11)


### Bug Fixes

* Add devnet support ([4177caa](https://github.com/nervosnetwork/keypering/commit/4177caa7bdde7fc5cc46324b6c1346538ec70eb8))
* Fix variable has already been declared ([99e8f53](https://github.com/nervosnetwork/keypering/commit/99e8f53c27ca3dfb185d20237983cf4ec248f671))
* remove cell deps leave null dialog ([bc4fc03](https://github.com/nervosnetwork/keypering/commit/bc4fc033855f98448f89b7ec8d257143f14fb165))
* update indexer_url const ([2124eee](https://github.com/nervosnetwork/keypering/commit/2124eee577c5a4562ceed05390bcd80edd91df7f))
* Use blake2b encrypt message ([1be4e29](https://github.com/nervosnetwork/keypering/commit/1be4e29e872a89d6782c702fcbb93ada45909077))


### Features

* add sign message rpc interface ([599d378](https://github.com/nervosnetwork/keypering/commit/599d378eec67c97475b8b31eba571139b43dc8b8))
* support vscode debug ([e631353](https://github.com/nervosnetwork/keypering/commit/e6313535df81340e0dc8fad1d224efa8b109248e))





# [0.1.0-alpha.2](https://github.com/nervosnetwork/keypering/compare/v0.1.0-alpha.1...v0.1.0-alpha.2) (2020-09-28)


Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ Keypering consists of 3 components:
- [How to Develop a CKB DApp with Keypering (Video, Chinese + English Subtitles)](https://youtu.be/i-gQ0enK5cY)
- [How to Develop a CKB DApp with Keypering (Slides)](https://docs.google.com/presentation/d/1bswEhjSYwZZnUCF4rRL5x5vfOVXO_kDlbjsojsG94w8/edit?usp=sharing)

# Develop in devnet

1. Clone [perkins-tent](https://github.com/xxuejie/perkins-tent)
2. Run `docker run -e CKB_NETWORK=dev --rm -p 8114:9115 -v ~/.ckb-docker-devnet:/data xxuejie/perkins-tent`
3. Go to `~/.ckb-docker-devnet/confs/nginx.conf` change `location = /indexer_rpc` to `location = /indexer`
4. Open keypering setting page then click Rich Node RPC setting icon, set `http://localhost:8114/indexer` in devent input
5. Finish!
6. If you want to run a miner, append ` miner: ckb miner -C /data/ckb-data` to `~/.ckb-docker-devnet/confs/Procfile`

# Resources

- [Keypering Manual](https://nervosnetwork.github.io/keypering/#/manual) - User Guide of Keypering
Expand Down
Binary file added docs/_media/screenshots/24.sign_message_rpc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ DApp can send JSON RPC conforming to [Keypering Agency Protocal](/protocol) to i

---

### Sign Message from DApp

<div class="center">
<b><small>Sign Message</small></b>
<img src="_media/screenshots/24.sign_message_rpc.png" alt="Sign Message from DApp">
</div>

<div class="center">
<b><small>Check Signed Message from DApp</small></b>
<img src="_media/screenshots/25.check_signed_message.png" alt="Check Signed Message from DApp">
</div>

---

## Setting

![Sidebar](_media/screenshots/22.sidebar.png)
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
],
"npmClient": "yarn",
"useWorkspaces": true,
"version": "0.1.0-alpha.2"
"version": "0.1.0-alpha.3"
}
20 changes: 20 additions & 0 deletions packages/app/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [0.1.0-alpha.3](https://github.com/nervosnetwork/keypering/compare/v0.1.0-alpha.2...v0.1.0-alpha.3) (2020-12-11)


### Bug Fixes

* Add devnet support ([4177caa](https://github.com/nervosnetwork/keypering/commit/4177caa7bdde7fc5cc46324b6c1346538ec70eb8))
* Fix variable has already been declared ([99e8f53](https://github.com/nervosnetwork/keypering/commit/99e8f53c27ca3dfb185d20237983cf4ec248f671))
* remove cell deps leave null dialog ([bc4fc03](https://github.com/nervosnetwork/keypering/commit/bc4fc033855f98448f89b7ec8d257143f14fb165))
* update indexer_url const ([2124eee](https://github.com/nervosnetwork/keypering/commit/2124eee577c5a4562ceed05390bcd80edd91df7f))
* Use blake2b encrypt message ([1be4e29](https://github.com/nervosnetwork/keypering/commit/1be4e29e872a89d6782c702fcbb93ada45909077))


### Features

* add sign message rpc interface ([599d378](https://github.com/nervosnetwork/keypering/commit/599d378eec67c97475b8b31eba571139b43dc8b8))





# [0.1.0-alpha.2](https://github.com/nervosnetwork/keypering/compare/v0.1.0-alpha.1...v0.1.0-alpha.2) (2020-09-28)


Expand Down
14 changes: 14 additions & 0 deletions packages/app/__tests__/keyper/sign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { signMsg } from '../../src/keyper/sign'

describe("Test signMsg", () => {
const info = {
sk: '0xe79f3207ea4980b7fed79956d5934249ceac4751a4fae01a0f7c4a96884bc4e3',
message: 'HelloWorld',
signture: '0x97ed8c48879eed50743532bf7cc53e641c501509d2be19d06e6496dd944a21b4509136f18c8e139cc4002822b2deb5cbaff8e44b8782769af3113ff7fb8bd92700'
}

it('signMsg', () => {
expect(signMsg(info.sk, info.message))
.toEqual(info.signture)
})
})
13 changes: 11 additions & 2 deletions packages/app/__tests__/utils/transformer/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shannonToCkb } from '../../../src/utils/transformer'
import { shannonToCkb, messageToHex } from '../../../src/utils/transformer'
import fixtures from './fixtures.json'

describe('Test transformer', () => {
Expand All @@ -15,5 +15,14 @@ describe('Test transformer', () => {
expect(shannonToCkb('1234567890123456789', false, '')).toBe('12345678901.23456789')
})
})
})

describe('Test messageToHex', () => {
it('returns correct length hex', () => {
expect(messageToHex('helloCKB')).toBe('0x68656c6c6f434b42000000000000000000000000000000000000000000000000')
})

it('throws error', () => {
expect( () => { messageToHex("I am a string and I will longer than 32bytes") }).toThrow('Message length greater than 32 bytes')
})
})
})
4 changes: 2 additions & 2 deletions packages/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keypering/app",
"version": "0.1.0-alpha.2",
"version": "0.1.0-alpha.3",
"description": "",
"author": "Keith <[email protected]>",
"homepage": "https://nervosnetwork.github.io/keypering",
Expand Down Expand Up @@ -32,7 +32,7 @@
"url": "https://github.com/nervosnetwork/keypering/issues"
},
"devDependencies": {
"@keypering/specs": "0.1.0-alpha.2",
"@keypering/specs": "0.1.0-alpha.3",
"@types/elliptic": "6.4.12",
"@types/uuid": "8.0.1",
"electron": "9.2.0",
Expand Down
139 changes: 139 additions & 0 deletions packages/app/public/dialogs/signMessage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Signing</title>
<style>
* {
box-sizing: border-box;
}

body {
--primary-green: #3cc68a;
margin: 0;
padding: 20px;
font-size: 14px;
letter-spacing: 0.23px;
user-select: none;
line-height: 1.2;
font-weight: 400;
background: #fff;
}

#message, #address {
color: var(--primary-green);
}

h1 {
font-size: 16px;
letter-spacing: 0.2px;
color: #000;
margin-bottom: 20px;
}

form div {
margin-bottom: 14px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.footer {
display: flex;
justify-content: flex-end;
margin-top: 20px;
}

button {
width: 80px;
height: 30px;
border-radius: 2px;
outline: none;
border: none;
text-transform: capitalize;
margin-left: 10px;
}

button:hover {
filter: brightness(0.9);
}

button[type=submit] {
background-color: var(--primary-green);
color: #fff;
}

button:disabled {
filter: grayscale(0) !important;
}
</style>
</head>

<body>
<form>
<h1>Message Confirmation</h1>
<div>Message: <span id="message">...</span></div>
<div>Address: <span id="address">...</span></div>
<div class="footer">
<button type="button">decline</button>
<button type="submit">approve</button>
</div>
</form>
<script>
const search = new URLSearchParams(location.search)
const address = search.get('address')
if (!address) {
window.alert('Invalid request')
window.close()
}

const channel = `signMsg:${address}`

const messageElement = document.querySelector("#message")
const addressElement = document.querySelector('#address')
const form = document.querySelector('form')
const submitBtn = document.querySelector('button[type=submit]')
const declineBtn = document.querySelector('button[type=button]')

const handleCancel = () => {
ipcRenderer?.send(channel, false)
window.close()
}

const handleSubmit = () => {
ipcRenderer?.send(channel, true)
submitBtn?.setAttribute('disabled', true)
declineBtn?.setAttribute('disabled', true)
}

declineBtn?.addEventListener('click', handleCancel)
submitBtn?.focus()

form.addEventListener('submit', (e) => {
e.preventDefault()
handleSubmit()
})

window.addEventListener('keyup', (e) => {
if (e.key === 'Escape') {
handleCancel()
}
})

ipcRenderer?.once(channel, (_e, params) => {
try {
const { message, address } = params
messageElement.innerText = message
addressElement.innerText = address
} catch (err) {
window.alert(err.message)
window.close()
}
})
</script>
</body>

</html>
18 changes: 16 additions & 2 deletions packages/app/src/keyper/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Container } from '@nervosnetwork/keyper-container'
import { SignatureAlgorithm } from '@nervosnetwork/keyper-specs'
import ECPair from '@nervosnetwork/ckb-sdk-utils/lib/ecpair'
import { getSetting } from '../setting'
import Blake2b from "../models/blake2b"

const sign = (sk: string, params: Omit<KeyperingAgency.SignTransaction.Params['params'], 'description'>) => {
export const signTx = (sk: string, params: Omit<KeyperingAgency.SignTransaction.Params['params'], 'description'>) => {

const container = new Container([
{
Expand Down Expand Up @@ -35,4 +36,17 @@ const sign = (sk: string, params: Omit<KeyperingAgency.SignTransaction.Params['p
return container.sign({ lockHash: params.lockHash }, params.tx, params.inputSignConfig)
}

export default sign
export const signMsg = (sk: string, message: string) => {
const digest = signtureHash(message)
const ecPair = new ECPair(sk)
const signature = ecPair.signRecoverable(digest)
return signature
}

const signtureHash = (message: string) => {
const magicString = 'Nervos Message:'
const buffer = Buffer.from(magicString + message, 'utf-8')
const blake2b = new Blake2b()
blake2b.updateBuffer(buffer)
return blake2b.digest()
}
30 changes: 30 additions & 0 deletions packages/app/src/models/blake2b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { blake2b, PERSONAL, hexToBytes } from '@nervosnetwork/ckb-sdk-utils'

export default class Blake2b {
private blake2b: any

constructor() {
this.blake2b = blake2b(32, null, null, PERSONAL)
}

public update = (message: string): void => {
const msg = message.startsWith('0x') ?
message :
`0x${message}`
this.blake2b.update(hexToBytes(msg))
}

public updateBuffer = (message: Buffer): void => {
this.blake2b.update(message)
}

public digest = (): string => {
return `0x${this.blake2b.digest('hex')}`
}

public static digest = (message: string): string => {
const blake2bHash = new Blake2b()
blake2bHash.update(message)
return blake2bHash.digest()
}
}
Loading

0 comments on commit b17220c

Please sign in to comment.