Skip to content

Commit

Permalink
Merge pull request #8 from susumuota/refactor-prettier
Browse files Browse the repository at this point in the history
refactor: format by prettier.
  • Loading branch information
susumuota committed Apr 2, 2023
2 parents 6bdeaaa + 88d5fc9 commit 4864fdc
Show file tree
Hide file tree
Showing 31 changed files with 271 additions and 270 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
os: [ubuntu-latest]
node-version: [18.15.0]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
- run: npm run test
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
- run: npm run test
36 changes: 18 additions & 18 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ jobs:
os: [ubuntu-latest]
node-version: [18.15.0]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run build
- run: npm run test
- run: npm run pack
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- uses: softprops/action-gh-release@v1
with:
draft: true
files: |
*.tgz
*.zip
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run build
- run: npm run test
- run: npm run pack
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- uses: softprops/action-gh-release@v1
with:
draft: true
files: |
*.tgz
*.zip
permissions:
contents: write
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
21 changes: 21 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "avoid",
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto",
"singleAttributePerLine": false
}
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,15 @@ npm install nostrain
import { generatePrivateKey, getPublicKey } from 'nostrain';

const sk = generatePrivateKey(); // `sk` is a hex string
const pk = getPublicKey(sk); // `pk` is a hex string
const pk = getPublicKey(sk); // `pk` is a hex string

console.log({ sk, pk });
```

### Creating, signing and verifying events

```javascript
import {
validateEvent,
verifySignature,
signEvent,
getEventHash,
generatePrivateKey,
getPublicKey
} from 'nostrain';
import { validateEvent, verifySignature, signEvent, getEventHash, generatePrivateKey, getPublicKey } from 'nostrain';

const privateKey = generatePrivateKey();

Expand Down Expand Up @@ -100,10 +93,7 @@ import { nip19, generatePrivateKey, getPublicKey } from 'nostrain';

{
const pk = getPublicKey(generatePrivateKey());
const relays = [
'wss://relay.nostr.example.mydomain.example.com',
'wss://nostr.banana.com',
];
const relays = ['wss://relay.nostr.example.mydomain.example.com', 'wss://nostr.banana.com'];
const nprofile = nip19.nprofileEncode({ pubkey: pk, relays });
const { type, data } = nip19.decode(nprofile);
console.log({ pk, relays, nprofile, type, data });
Expand Down Expand Up @@ -166,7 +156,7 @@ const event = {
kind: 1,
created_at: Math.round(Date.now() / 1000),
content: 'hello from a delegated key',
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]]
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]],
};

console.log({ tags: event.tags });
Expand Down
2 changes: 1 addition & 1 deletion examples/delegation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const event = {
kind: 1,
created_at: Math.round(Date.now() / 1000),
content: 'hello from a delegated key',
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]]
tags: [['delegation', delegation.from, delegation.cond, delegation.sig]],
};

console.log({ tags: event.tags });
Expand Down
9 changes: 1 addition & 8 deletions examples/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@

// node examples/event.ts

import {
validateEvent,
verifySignature,
signEvent,
getEventHash,
generatePrivateKey,
getPublicKey
} from 'nostrain';
import { validateEvent, verifySignature, signEvent, getEventHash, generatePrivateKey, getPublicKey } from 'nostrain';

const privateKey = generatePrivateKey();

Expand Down
2 changes: 1 addition & 1 deletion examples/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import { generatePrivateKey, getPublicKey } from 'nostrain';

const sk = generatePrivateKey(); // `sk` is a hex string
const pk = getPublicKey(sk); // `pk` is a hex string
const pk = getPublicKey(sk); // `pk` is a hex string

console.log({ sk, pk });
5 changes: 1 addition & 4 deletions examples/npub.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import { nip19, generatePrivateKey, getPublicKey } from 'nostrain';

{
const pk = getPublicKey(generatePrivateKey());
const relays = [
'wss://relay.nostr.example.mydomain.example.com',
'wss://nostr.banana.com',
];
const relays = ['wss://relay.nostr.example.mydomain.example.com', 'wss://nostr.banana.com'];
const nprofile = nip19.nprofileEncode({ pubkey: pk, relays });
const { type, data } = nip19.decode(nprofile);
console.log({ pk, relays, nprofile, type, data });
Expand Down
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"build": "tsc && vite build",
"watch": "concurrently \"tsc --watch\" \"vite build --watch\"",
"test": "vitest",
"format": "prettier --check .",
"pack": "rm -f *.tgz *.zip ; npm pack ; tar tfz *.tgz | sed -e 's|^package/||' | zip -r@ `ls *.tgz | sed -e 's|\\.tgz|\\.zip|'`"
},
"devDependencies": {
Expand All @@ -46,6 +47,7 @@
"@vitest/ui": "^0.29.8",
"concurrently": "^8.0.1",
"node-fetch": "^3.3.1",
"prettier": "2.8.7",
"tsx": "^3.12.6",
"typescript": "^5.0.3",
"vite": "^4.2.0",
Expand Down
15 changes: 8 additions & 7 deletions src/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Event', () => {
kind: Kind.Text,
tags: [],
content: 'Hello, world!',
created_at: 1617932115
created_at: 1617932115,
};

const event = finishEvent(template, privateKey);
Expand Down Expand Up @@ -68,8 +68,8 @@ describe('Event', () => {
unsignedEvent.created_at,
unsignedEvent.kind,
unsignedEvent.tags,
unsignedEvent.content
])
unsignedEvent.content,
]),
);
});

Expand All @@ -84,7 +84,8 @@ describe('Event', () => {
pubkey: publicKey, // missing content
};

expect(() => { // @ts-ignore
expect(() => {
// @ts-ignore
serializeEvent(invalidEvent);
}).toThrow("can't serialize event with wrong or missing properties");
});
Expand Down Expand Up @@ -220,7 +221,7 @@ describe('Event', () => {
content: 'Hello, world!',
created_at: 1617932115,
},
privateKey
privateKey,
);

const isValid = verifySignature(event);
Expand All @@ -238,7 +239,7 @@ describe('Event', () => {
content: 'Hello, world!',
created_at: 1617932115,
},
privateKey
privateKey,
);

// tamper with the signature
Expand All @@ -262,7 +263,7 @@ describe('Event', () => {
content: 'Hello, world!',
created_at: 1617932115,
},
privateKey1
privateKey1,
);

// verify with different private key
Expand Down
35 changes: 15 additions & 20 deletions src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ enum Kind {
RelayList = 10002,
ClientAuth = 22242,
Article = 30023,
};
}

type EventTemplate = {
kind: Kind,
tags: string[][],
content: string,
created_at: number,
kind: Kind;
tags: string[][];
content: string;
created_at: number;
};

type UnsignedEvent = EventTemplate & {
pubkey: string,
pubkey: string;
};

type Event = UnsignedEvent & {
id: string,
sig: string,
id: string;
sig: string;
};

const getBlankEvent = () => ( // @ts-ignore // kind 255 is OK
{ kind: 255, content: '', tags: [], created_at: 0 } as EventTemplate
);
const getBlankEvent = () =>
// @ts-ignore // kind 255 is OK
({ kind: 255, content: '', tags: [], created_at: 0 } as EventTemplate);

const finishEvent = (event: EventTemplate, privateKey: string) => {
const e = event as Event;
Expand All @@ -62,9 +62,7 @@ const serializeEvent = (event: UnsignedEvent) => {
return JSON.stringify([0, event.pubkey, event.created_at, event.kind, event.tags, event.content]);
};

const getEventHash = (event: UnsignedEvent) => (
bytesToHex(sha256(utf8Encoder.encode(serializeEvent(event))))
);
const getEventHash = (event: UnsignedEvent) => bytesToHex(sha256(utf8Encoder.encode(serializeEvent(event))));

const validateEvent = (event: UnsignedEvent) => {
if (typeof event !== 'object') return false;
Expand All @@ -83,13 +81,10 @@ const validateEvent = (event: UnsignedEvent) => {
return true;
};

const verifySignature = (event: Event) => (
schnorr.verify(event.sig, getEventHash(event), event.pubkey)
);
const verifySignature = (event: Event) => schnorr.verify(event.sig, getEventHash(event), event.pubkey);

const signEvent = (event: UnsignedEvent, privateKey: string) => (
bytesToHex(schnorr.sign(getEventHash(event), privateKey))
);
const signEvent = (event: UnsignedEvent, privateKey: string) =>
bytesToHex(schnorr.sign(getEventHash(event), privateKey));

export type { EventTemplate, UnsignedEvent, Event };
export { Kind, getBlankEvent, finishEvent, serializeEvent, getEventHash, validateEvent, verifySignature, signEvent };
24 changes: 12 additions & 12 deletions src/fakejson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ test('match id', () => {
expect(
fj.matchEventId(
`["EVENT","nostril-query",{"tags":[],"content":"so did we cut all corners and p2p stuff in order to make a decentralized social network that was fast and worked, but in the end what we got was a lot of very slow clients that can't handle the traffic of one jack dorsey tweet?","sig":"ca62629d189edebb8f0811cfa0ac53015013df5f305dcba3f411ba15cfc4074d8c2d517ee7d9e81c9eb72a7328bfbe31c9122156397565ac55e740404e2b1fe7","id":"fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146","kind":1,"pubkey":"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","created_at":1671150419}]`,
'fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146'
)
'fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146',
),
).toBeTruthy();

expect(
fj.matchEventId(
`["EVENT","nostril-query",{"content":"a bunch of mfs interacted with my post using what I assume were \"likes\": https://nostr.build/i/964.png","created_at":1672506879,"id":"f40bdd0905137ad60482537e260890ab50b0863bf16e67cf9383f203bd26c96f","kind":1,"pubkey":"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","sig":"8b825d2d4096f0643b18ca39da59ec07a682cd8a3e717f119c845037573d98099f5bea94ec7ddedd5600c8020144a255ed52882a911f7f7ada6d6abb3c0a1eb4","tags":[]}]`,
'fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146'
)
'fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146',
),
).toBeFalsy();
});

test('match kind', () => {
expect(
fj.matchEventKind(
`["EVENT","nostril-query",{"tags":[],"content":"so did we cut all corners and p2p stuff in order to make a decentralized social network that was fast and worked, but in the end what we got was a lot of very slow clients that can't handle the traffic of one jack dorsey tweet?","sig":"ca62629d189edebb8f0811cfa0ac53015013df5f305dcba3f411ba15cfc4074d8c2d517ee7d9e81c9eb72a7328bfbe31c9122156397565ac55e740404e2b1fe7","id":"fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146","kind":1,"pubkey":"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","created_at":1671150419}]`,
1
)
1,
),
).toBeTruthy();

expect(
fj.matchEventKind(
`["EVENT","nostril-query",{"content":"{\"name\":\"fiatjaf\",\"about\":\"buy my merch at fiatjaf store\",\"picture\":\"https://fiatjaf.com/static/favicon.jpg\",\"nip05\":\"_@fiatjaf.com\"}","created_at":1671217411,"id":"b52f93f6dfecf9d81f59062827cd941412a0e8398dda60baf960b17499b88900","kind":12720,"pubkey":"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d","sig":"fc1ea5d45fa5ed0526faed06e8fc7a558e60d1b213e9714f440828584ee999b93407092f9b04deea7e504fa034fc0428f31f7f0f95417b3280ebe6004b80b470","tags":[]}]`, // cspell:disable-line
12720
)
12720,
),
).toBeTruthy();
});

Expand All @@ -45,7 +45,7 @@ test('match subscription id', () => {
expect(fj.getSubscriptionId('["EVENT", "kasjbdjkav", {}]')).toEqual('kasjbdjkav'); // cspell:disable-line
expect(
fj.getSubscriptionId(
' [ \n\n "EVENT" , \n\n "y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH" , {}]' // cspell:disable-line
)
).toEqual('y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH') // cspell:disable-line
})
' [ \n\n "EVENT" , \n\n "y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH" , {}]', // cspell:disable-line
),
).toEqual('y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH'); // cspell:disable-line
});

0 comments on commit 4864fdc

Please sign in to comment.