Skip to content

Commit

Permalink
add nostrassets
Browse files Browse the repository at this point in the history
  • Loading branch information
minmin.yan committed Nov 21, 2023
1 parent e23c1d2 commit 25e7ac2
Show file tree
Hide file tree
Showing 13 changed files with 917 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/coin-nostrassets/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 OKX.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
70 changes: 70 additions & 0 deletions packages/coin-nostrassets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# @okxweb3/coin-nostrassets
NoStrAssets SDK is used to interact with the protocol NoStrAssets , it contains various functions can be used to web3 wallet.

## Installation

### Npm

To obtain the latest version, simply require the project using npm :

```shell
npm install @okxweb3/coin-nostrassets
```

## Usage

### New address and public key

```typescript
import { NoStrAssetsWallet } from "@okxweb3/coin-nostrassets";

const prv = 'bb1c93508b962c7efb0a340848538b2c5f7ba6c44e55f52389aa132a2fd3521a'
let wallet = new NoStrAssetsWallet();
let r =await wallet.getNewAddress({privateKey: prv})
console.log(r)
```

### Sign Event

```typescript
import { NoStrAssetsWallet } from "@okxweb3/coin-nostrassets";

const prv = 'bb1c93508b962c7efb0a340848538b2c5f7ba6c44e55f52389aa132a2fd3521a'
let wallet = new NoStrAssetsWallet();
let event = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),//unix
tags: [],
content: 'hello',
}
let e = await wallet.signTransaction({
privateKey: prv,
data: event
})
console.log(e)
```


### Encrypt and decrypt for nip04

```typescript
import { NoStrAssetsWallet } from "@okxweb3/coin-nostrassets";

let wallet = new NoStrAssetsWallet();
let text = 'hello'
let privkey = '425824242e3038e026f7cbeb6fe289cb6ffcfad1fa955c318c116aa1f2f32bfc'
const encrypted = await wallet.signTransaction({
privateKey: privkey,
data: new CryptTextParams('nip04encrypt', '8a0523d045d09c30765029af9307d570cb0d969e4b9400c08887c23250626eea', text)
});
console.log('encrypted', encrypted)
const decrypted = await wallet.signTransaction({
privateKey: privkey,
data: new CryptTextParams('nip04decrypt', '8a0523d045d09c30765029af9307d570cb0d969e4b9400c08887c23250626eea', encrypted)
});
console.log('decrypted', decrypted)
console.log(e)
```

## License
Most packages or folder are [MIT](<https://github.com/okx/js-wallet-sdk/blob/main/LICENSE>) licensed, see package or folder for the respective license.
5 changes: 5 additions & 0 deletions packages/coin-nostrassets/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
38 changes: 38 additions & 0 deletions packages/coin-nostrassets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@okxweb3/coin-nostrassets",
"version": "1.0.0",
"description": "",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "run-s clean build:main",
"build:main": "tsc -p tsconfig.json",
"clean": "rimraf dist tsconfig.tsbuildinfo",
"test": "jest"
},
"repository": {
"type": "git",
"url": "https://github.com/okx/js-wallet-sdk"
},
"keywords": [],
"author": "okx",
"license": "MIT",
"files": [
"dist"
],
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/node": "^12.20.52",
"jest": "^27.5.1",
"npm-run-all": "^4.1.5",
"prettier": "2.6.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.4",
"ts-node": "^10.7.0",
"typescript": "^4.6.2"
},
"dependencies": {
"@okxweb3/crypto-lib": "^1.0.0",
"@okxweb3/coin-base": "^1.0.0"
}
}
105 changes: 105 additions & 0 deletions packages/coin-nostrassets/src/NoStrAssetsWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
BaseWallet,
GetDerivedPathParam,
NewAddressParams,
ValidAddressParams,
SignTxParams,
NotImplementedError,
} from "@okxweb3/coin-base";
import {
base
} from "@okxweb3/crypto-lib";
import {addressFromPrvKey, encrypt, decrypt, npubEncode} from "./nostrassets";
import * as crypto from "crypto";
import * as Events from "events";
import {Event, getEventHash, getSignature} from "./event";
import {getPublicKey} from "./keys";

export enum nipOpType {
NIP04_Encrypt = 1,
NIP04_Decrypt = 2,
}


export class CryptTextParams {
/**
* Constructs a CryptTextParams instance.
* @param type
* @param pubkey
* @param text
*/
public constructor(
public readonly type: nipOpType,
public readonly pubkey: string,
public readonly text: string,
) {
// this.type = type;
// this.pubkey = pubkey;
// this.text = text;
}
};

// @ts-ignore
if (typeof crypto !== 'undefined' && !crypto.subtle && crypto.webcrypto) {
// @ts-ignore
crypto.subtle = crypto.webcrypto.subtle
}
export const NewAddressError = "generate address error"
export type NewAddressData = {
address: string;
publicKey?: string;
};

export class NoStrAssetsWallet extends BaseWallet {
async getDerivedPath(param: GetDerivedPathParam): Promise<any> {
return `m/86'/0'/0'/0/${param.index}`
}

checkPrivateKey(privateKey: string) {
const keyBytes = base.fromHex(privateKey)
return keyBytes.length == 32;
}

async getNewAddress(param: NewAddressParams): Promise<any> {
try {
if (!this.checkPrivateKey(param.privateKey)) {
return Promise.reject(NewAddressError);
}
const data: NewAddressData = {
address: addressFromPrvKey(param.privateKey),
publicKey: npubEncode(getPublicKey(param.privateKey))
};
return Promise.resolve(data)
} catch (e) {
return Promise.reject(NewAddressError);
}
}

async validAddress(param: ValidAddressParams): Promise<any> {
return Promise.resolve(NotImplementedError);
}

async signTransaction(param: SignTxParams): Promise<any> {
if (param.data instanceof CryptTextParams) {
const textParams = param.data as CryptTextParams;
switch (textParams.type) {
case nipOpType.NIP04_Decrypt:
return decrypt(param.privateKey, textParams.pubkey, textParams.text)
case nipOpType.NIP04_Encrypt:
return encrypt(param.privateKey, textParams.pubkey, textParams.text)
default:
return Promise.reject(NotImplementedError)
}
} else {
try {
let event = param.data as Event
event.pubkey = getPublicKey(param.privateKey)
event.id = getEventHash(event)
event.sig = getSignature(event, param.privateKey)
return Promise.resolve(event)
} catch (ex) {
return Promise.reject(NotImplementedError)
}
}
}
}
Loading

0 comments on commit 25e7ac2

Please sign in to comment.