-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from KeystoneHQ/feat/support_sign_message_hash
Feat/support sign message hash
- Loading branch information
Showing
7 changed files
with
327 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,66 @@ | ||
{ | ||
"name": "keystone-airgaped-base", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"clean": "./scripts/clean.sh", | ||
"build": "pnpm -r build", | ||
"bootstrap": "pnpm clean && pnpm install", | ||
"lint": "eslint '*/**/*.ts' --quiet --fix", | ||
"prettier:check": "pnpm run prettier --check", | ||
"prettier:fix": "pnpm run prettier --write", | ||
"prettier": "prettier '*/**/*.{js,jsx,ts,tsx}'", | ||
"test": "pnpm -r test", | ||
"prepack": "pnpm -r build" | ||
}, | ||
"jest": { | ||
"modulePathIgnorePatterns": [ | ||
"__data__" | ||
], | ||
"testEnvironment": "node" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
"name": "keystone-airgaped-base", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"packageManager": "[email protected]", | ||
"scripts": { | ||
"clean": "./scripts/clean.sh", | ||
"build": "pnpm -r build", | ||
"bootstrap": "pnpm clean && pnpm install", | ||
"lint": "eslint '*/**/*.ts' --quiet --fix", | ||
"prettier:check": "pnpm run prettier --check", | ||
"prettier:fix": "pnpm run prettier --write", | ||
"prettier": "prettier '*/**/*.{js,jsx,ts,tsx}'", | ||
"test": "pnpm -r test", | ||
"prepack": "pnpm -r build" | ||
}, | ||
"jest": { | ||
"modulePathIgnorePatterns": [ | ||
"__data__" | ||
], | ||
"testEnvironment": "node" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{js,jsx,ts,tsx}": [ | ||
"prettier --write", | ||
"eslint " | ||
], | ||
"*.json": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"pnpm": { | ||
"neverBuiltDependencies": [ | ||
"node-hid" | ||
] | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.21.5", | ||
"@babel/core": "^7.22.1", | ||
"@babel/plugin-proposal-optional-chaining": "^7.21.0", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@types/jest": "^27.4.1", | ||
"@typescript-eslint/eslint-plugin": "^5.14.0", | ||
"@typescript-eslint/parser": "^5.14.0", | ||
"eslint": "^8.10.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"git-commit-msg-linter": "^4.1.2", | ||
"husky": "^7.0.4", | ||
"jest": "^27.5.1", | ||
"lint-staged": "^12.3.5", | ||
"prettier": "^2.5.1", | ||
"tsdx": "^0.14.1", | ||
"typescript": "^4.6.2" | ||
}, | ||
"dependencies": { | ||
"uuid": "^8.3.2" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{js,jsx,ts,tsx}": [ | ||
"prettier --write", | ||
"eslint " | ||
], | ||
"*.json": [ | ||
"prettier --write" | ||
] | ||
}, | ||
"pnpm": { | ||
"neverBuiltDependencies": ["node-hid"] | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.21.5", | ||
"@babel/core": "^7.22.1", | ||
"@babel/plugin-proposal-optional-chaining": "^7.21.0", | ||
"@babel/preset-env": "^7.16.11", | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@types/jest": "^27.4.1", | ||
"@typescript-eslint/eslint-plugin": "^5.14.0", | ||
"@typescript-eslint/parser": "^5.14.0", | ||
"eslint": "^8.10.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"git-commit-msg-linter": "^4.1.2", | ||
"husky": "^7.0.4", | ||
"jest": "^27.5.1", | ||
"lint-staged": "^12.3.5", | ||
"prettier": "^2.5.1", | ||
"tsdx": "^0.14.1", | ||
"typescript": "^4.6.2" | ||
}, | ||
"dependencies": { | ||
"uuid": "^8.3.2" | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/ur-registry-sui/__tests__/SuiSignHashRequest.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { | ||
CryptoKeypath, | ||
extend, | ||
DataItem, | ||
PathComponent, | ||
RegistryItem, | ||
DataItemMap, | ||
} from "@keystonehq/bc-ur-registry"; | ||
import { ExtendedRegistryTypes } from "./RegistryType"; | ||
import * as uuid from "uuid"; | ||
|
||
const { decodeToDataItem, RegistryTypes } = extend; | ||
|
||
enum Keys { | ||
requestId = 1, | ||
messageHash, | ||
derivationPaths, | ||
addresses, | ||
origin, | ||
} | ||
|
||
type signHashRequestProps = { | ||
requestId?: Buffer; | ||
messageHash: string; | ||
derivationPaths: CryptoKeypath[]; | ||
addresses?: Buffer[]; | ||
origin?: string; | ||
}; | ||
|
||
export class SuiSignHashRequest extends RegistryItem { | ||
private requestId?: Buffer; | ||
private messageHash: string; | ||
private derivationPaths: CryptoKeypath[]; | ||
private addresses?: Buffer[]; | ||
private origin?: string; | ||
|
||
getRegistryType = () => ExtendedRegistryTypes.SUI_SIGN_HASH_REQUEST; | ||
|
||
constructor(args: signHashRequestProps) { | ||
super(); | ||
this.requestId = args.requestId; | ||
this.messageHash = args.messageHash; | ||
this.derivationPaths = args.derivationPaths; | ||
this.addresses = args.addresses; | ||
this.origin = args.origin; | ||
} | ||
|
||
public getRequestId = () => this.requestId; | ||
public getMessageHash = () => this.messageHash; | ||
public getDerivationPaths = () => | ||
this.derivationPaths.map((key) => key.getPath()); | ||
public getAddresses = () => this.addresses; | ||
public getOrigin = () => this.origin; | ||
|
||
public toDataItem = () => { | ||
const map: DataItemMap = {}; | ||
if (this.requestId) { | ||
map[Keys.requestId] = new DataItem( | ||
this.requestId, | ||
RegistryTypes.UUID.getTag() | ||
); | ||
} | ||
if (this.addresses) { | ||
map[Keys.addresses] = this.addresses; | ||
} | ||
if (this.origin) { | ||
map[Keys.origin] = this.origin; | ||
} | ||
|
||
map[Keys.messageHash] = this.messageHash; | ||
map[Keys.derivationPaths] = this.derivationPaths.map((item) => { | ||
const dataItem = item.toDataItem(); | ||
dataItem.setTag(item.getRegistryType().getTag()); | ||
return dataItem; | ||
}); | ||
return new DataItem(map); | ||
}; | ||
|
||
public static fromDataItem = (dataItem: DataItem) => { | ||
const map = dataItem.getData(); | ||
const messageHash = map[Keys.messageHash]; | ||
const derivationPaths = map[Keys.derivationPaths].map((item: DataItem) => | ||
CryptoKeypath.fromDataItem(item) | ||
); | ||
const addresses = map[Keys.addresses] ? map[Keys.addresses] : undefined; | ||
const requestId = map[Keys.requestId] | ||
? map[Keys.requestId].getData() | ||
: undefined; | ||
const origin = map[Keys.origin] ? map[Keys.origin] : undefined; | ||
return new SuiSignHashRequest({ | ||
requestId, | ||
messageHash, | ||
derivationPaths, | ||
addresses, | ||
origin, | ||
}); | ||
}; | ||
|
||
public static fromCBOR = (_cborPayload: Buffer) => { | ||
const dataItem = decodeToDataItem(_cborPayload); | ||
return SuiSignHashRequest.fromDataItem(dataItem); | ||
}; | ||
|
||
public static constructSuiRequest( | ||
messageHash: string, | ||
publicKeyHdPath: string[], | ||
xfps: string[], | ||
uuidString: string, | ||
addresses?: Buffer[], | ||
origin?: string | ||
) { | ||
const publicKeyHdPathObjects = publicKeyHdPath.map((path, index) => { | ||
const paths = path.replace(/[m|M]\//, "").split("/"); | ||
const pathComponent = paths.map((path) => { | ||
const index = parseInt(path.replace("'", "")); | ||
let isHardened = false; | ||
if (path.endsWith("'")) { | ||
isHardened = true; | ||
} | ||
return new PathComponent({ index, hardened: isHardened }); | ||
}); | ||
return new CryptoKeypath(pathComponent, Buffer.from(xfps[index], "hex")); | ||
}); | ||
|
||
return new SuiSignHashRequest({ | ||
requestId: Buffer.from(uuid.parse(uuidString)), | ||
messageHash, | ||
derivationPaths: publicKeyHdPathObjects, | ||
addresses: addresses || undefined, | ||
origin: origin || undefined, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters