gitmark 0.0.55 → 0.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env node
2
+
3
+ // USAGE: git-mark-list
4
+
5
+ // IMPORTS
6
+ const gitlog = require('gitlog').default
7
+ var bitcoin = require('bitcoinjs-lib')
8
+ var argv = require('minimist')(process.argv.slice(2))
9
+ const fs = require('fs')
10
+ const homedir = require('os').homedir()
11
+ const sha256 = require('sha256')
12
+ // const Buffer = require('safe-buffer').Buffer;
13
+ const schnorr = require('bip-schnorr')
14
+
15
+ // MODEL
16
+ // default privkey = brain for urn:json:bitmark
17
+ globalThis.data = {
18
+ privkey: '132e7465a63e30a74c5b0deae3573033319e9de18a84878613eaa21878b2b56b'
19
+ }
20
+
21
+ // console.log('data', data)
22
+
23
+ // FUNCTIONS
24
+ const BITMARK = {
25
+ messagePrefix: '\x19BITMARK Signed Message:\n',
26
+ bech32: 'btm',
27
+ bip32: {
28
+ public: 0x019da462,
29
+ private: 0x019d9cfe
30
+ },
31
+ pubKeyHash: 85,
32
+ wif: 213,
33
+ scriptHash: 0x32
34
+ }
35
+
36
+ const options = {
37
+ repo: './',
38
+ fields: ['subject', 'authorName', 'authorDate'],
39
+ execOptions: { maxBuffer: 1000 * 1024 },
40
+ number: 1000000,
41
+ fields: ['hash', 'subject', 'authorDate']
42
+ }
43
+
44
+ function addCryptoToCommit(commit) {
45
+ // console.log('commit', commit)
46
+ if (!commit.hash) return
47
+ b2 = BigInt('0x' + commit.hash)
48
+ b3 = BigInt.asUintN(256, b1 + b2)
49
+ commit.privkey = b1.toString(16)
50
+ commit.tweakedkey = b3.toString(16)
51
+ // console.log('original private key', b3.toString(16))
52
+ // console.log('tweaked private key', b3.toString(16))
53
+ var keyPair1 = bitcoin.ECPair.fromPrivateKey(
54
+ Buffer.from(b1.toString(16), 'hex'),
55
+ { network: BITMARK }
56
+ )
57
+ commit.wifkey = keyPair1.toWIF()
58
+
59
+ function buf2hex(buffer) {
60
+ // buffer is an ArrayBuffer
61
+ return [...new Uint8Array(buffer)]
62
+ .map(x => x.toString(16).padStart(2, '0'))
63
+ .join('')
64
+ }
65
+
66
+ commit.pubkey = buf2hex(keyPair1.publicKey).substring(2)
67
+ // console.log('private key WIF:', keyPair1.toWIF())
68
+
69
+ var { address } = bitcoin.payments.p2pkh({
70
+ pubkey: keyPair1.publicKey,
71
+ network: BITMARK
72
+ })
73
+ commit.address = address
74
+ // console.log('verify address computed from private key:', address)
75
+
76
+ commit.verify = `https://gitmark.info/${commit.subject.split(/[: ]/)[1]}`
77
+
78
+ return commit
79
+
80
+ }
81
+
82
+
83
+ function renderCommit(commit, type) {
84
+ if (!commit) return
85
+ if (type === 'nostr') {
86
+ var canon = [0, commit.pubkey, commit.authorDate, 1, [], commit.hash]
87
+
88
+ canon = JSON.stringify(canon)
89
+ var id = sha256(canon)
90
+
91
+ // console.log(canon)
92
+
93
+ // sign
94
+
95
+
96
+ const privateKeyHex = commit.privkey
97
+ const message = Buffer.from(id, 'hex')
98
+
99
+ const sig = schnorr.sign(privateKeyHex, message).toString('hex')
100
+
101
+ // console.log(sig)
102
+ // var sig = 1
103
+
104
+ var event = `["EVENT",{"id":"${id}","pubkey":"${commit.pubkey}","created_at":${commit.authorDate},"kind":1,"tags":[],"content":"${commit.hash}","sig":"${sig}"}]`
105
+
106
+ console.log(event)
107
+ } else {
108
+ console.log(commit)
109
+ }
110
+ }
111
+
112
+
113
+ function getPrivKey() {
114
+ try {
115
+ const fetchHeadDir = './.git/'
116
+ var fetchHeadFile = fetchHeadDir + 'FETCH_HEAD'
117
+
118
+ var fetchHead = fs.readFileSync(fetchHeadFile).toString()
119
+
120
+ var repo = fetchHead
121
+ .split(' ')
122
+ .pop()
123
+ .replace(':', '/')
124
+ .replace('\n', '')
125
+
126
+ const gitmarkRepoBase = homedir + '/.gitmark/repo'
127
+
128
+ const gitmarkFile = gitmarkRepoBase + '/' + repo + '/gitmark.json'
129
+
130
+ return require(gitmarkFile).privkey
131
+ } catch {
132
+ return undefined
133
+ }
134
+ }
135
+
136
+ // INIT
137
+ data.privkey = argv._[0] || getPrivKey() || data.privkey
138
+ data.count = argv.count
139
+
140
+ var POINT = Buffer.from(data.privkey, 'hex')
141
+
142
+ var OFFSET = Buffer.from('000000000000000000000000' + data.hash, 'hex')
143
+
144
+ // MAIN
145
+ // priv keys
146
+ var b1 = BigInt('0x' + data.privkey)
147
+ var b2
148
+ var b3
149
+ // var keyPair1 = bitcoin.ECPair.fromPrivateKey(POINT)
150
+ // var pubkey = keypair.publicKey
151
+
152
+
153
+ // genesis
154
+ var genesis = require('../gitmark.json')
155
+ var genesisCommit = {
156
+ "pubkey": genesis.pubkey,
157
+ "hash": "genesis",
158
+ "authorDate": genesis.created_at,
159
+ "privkey": b1.toString(16)
160
+ }
161
+ // console.log('genesis', genesis)
162
+ // console.log('genesisCommit', genesisCommit)
163
+ if (!data.count) {
164
+ renderCommit(genesisCommit, 'nostr')
165
+ }
166
+
167
+ // get commits
168
+ const commits = gitlog(options)
169
+ if (commits.length === 1) {
170
+ console.log('Set up new repository with a gitmark')
171
+ process.exit()
172
+ }
173
+
174
+
175
+
176
+ // traverse backwards
177
+ // console.log('Genesis tx need not be verified\n')
178
+ var done = 0
179
+ var gitmarks = []
180
+ for (var i = commits.length - 1; i >= 0; i--) {
181
+
182
+ if (commits[i].subject.match(/^gitmark[: ][A-Fa-f0-9]/)) {
183
+
184
+ var commit = {
185
+ hash: commits[i + 1]?.hash,
186
+ subject: commits[i].subject,
187
+ authorDate: Math.floor(new Date(commits[i].authorDate).getTime() / 1000),
188
+ privkey: b1.toString(16)
189
+ }
190
+ commit = addCryptoToCommit(commit)
191
+ // console.log(commit)
192
+ gitmarks.push(commit)
193
+
194
+ }
195
+ }
196
+
197
+
198
+ gitmarks = gitmarks.reverse()
199
+ var len = data.count || gitmarks.length
200
+ for (var i = 0; i < len; i++) {
201
+ // console.log(gitmarks[i])
202
+ renderCommit(gitmarks[i], 'nostr')
203
+ }
package/gitmark.json CHANGED
@@ -3,5 +3,7 @@
3
3
  "genesis": "gitmark:59ff24db0321cb6b32a404815345b00ae68ca8b81150fbea6464ee10557e0fae:1",
4
4
  "nick": "gitmark",
5
5
  "package": "./package.json",
6
+ "created_at": 1618076262,
7
+ "pubkey": "7574866ae7653e084c3c8e9e6359660e2c728d249fefb0f984bd32393f2ac67f",
6
8
  "repository": "./"
7
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gitmark",
3
- "version": "0.0.55",
3
+ "version": "0.0.59",
4
4
  "description": "gitmark",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,7 +15,9 @@
15
15
  ],
16
16
  "bin": {
17
17
  "git-mark": "bin/git-mark",
18
- "git-mark-init": "bin/git-mark-init"
18
+ "git-mark-init": "bin/git-mark-init",
19
+ "git-mark-list": "bin/git-mark-list",
20
+ "git-mark-verify": "bin/git-mark-verify"
19
21
  },
20
22
  "author": "Melvin Carvalho",
21
23
  "license": "MIT",
@@ -24,11 +26,14 @@
24
26
  },
25
27
  "homepage": "https://github.com/solidpayorg/gitmark#readme",
26
28
  "dependencies": {
29
+ "bip-schnorr": "^0.6.4",
27
30
  "bitcoinjs-lib": "^5.2.0",
28
31
  "child_process": "^1.0.2",
29
32
  "fs": "^0.0.1-security",
30
33
  "gitlog": "^4.0.4",
34
+ "gitmark": "^0.0.56",
31
35
  "minimist": "^1.2.5",
36
+ "sha256": "^0.2.0",
32
37
  "tiny-secp256k1": "^1.1.6",
33
38
  "ws": "^8.2.3"
34
39
  }