@obbyx/obyte 0.0.0-stage → 0.1.1
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.
- package/LICENSE +21 -0
- package/README.md +15 -2
- package/dist/channel/definition.d.ts +38 -0
- package/dist/channel/definition.d.ts.map +1 -0
- package/dist/channel/definition.js +86 -0
- package/dist/channel/definition.js.map +1 -0
- package/dist/config/index.d.ts +31 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +33 -0
- package/dist/config/index.js.map +1 -0
- package/dist/core/amounts.d.ts +35 -0
- package/dist/core/amounts.d.ts.map +1 -0
- package/dist/core/amounts.js +94 -0
- package/dist/core/amounts.js.map +1 -0
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +18 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/networks.d.ts +34 -0
- package/dist/core/networks.d.ts.map +1 -0
- package/dist/core/networks.js +50 -0
- package/dist/core/networks.js.map +1 -0
- package/dist/core/result.d.ts +17 -0
- package/dist/core/result.d.ts.map +1 -0
- package/dist/core/result.js +13 -0
- package/dist/core/result.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/index.d.ts +76 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/logger/index.js +161 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/signing/signed-message.d.ts +47 -0
- package/dist/signing/signed-message.d.ts.map +1 -0
- package/dist/signing/signed-message.js +102 -0
- package/dist/signing/signed-message.js.map +1 -0
- package/dist/unit/exact-unit.d.ts +94 -0
- package/dist/unit/exact-unit.d.ts.map +1 -0
- package/dist/unit/exact-unit.js +387 -0
- package/dist/unit/exact-unit.js.map +1 -0
- package/dist/wallet/index.d.ts +29 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +106 -0
- package/dist/wallet/index.js.map +1 -0
- package/package.json +64 -3
- package/src/channel/definition.ts +114 -0
- package/src/config/index.ts +40 -0
- package/src/core/amounts.ts +116 -0
- package/src/core/errors.ts +22 -0
- package/src/core/networks.ts +81 -0
- package/src/core/result.ts +23 -0
- package/src/index.ts +58 -0
- package/src/logger/index.ts +234 -0
- package/src/signing/signed-message.ts +147 -0
- package/src/types/obyte-internal.d.ts +19 -0
- package/src/unit/exact-unit.ts +447 -0
- package/src/wallet/index.ts +130 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import obyte from 'obyte';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { isAddress } from '../channel/definition.js';
|
|
8
|
+
import { ObbyxError } from '../core/errors.js';
|
|
9
|
+
import type { ObyteNetwork } from '../core/networks.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Local single-address wallet shared by the CLI, the seller watcher and the
|
|
13
|
+
* demo playground: one private key per network in
|
|
14
|
+
* `$OBBYX_HOME/wallet-<network>.json` (default ~/.obbyx), file mode 0600.
|
|
15
|
+
* Loading verifies the file (schema), its permissions, its network and that
|
|
16
|
+
* the key derives the stored address. The key never leaves this machine.
|
|
17
|
+
*/
|
|
18
|
+
export const walletFileSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
version: z.literal(1),
|
|
21
|
+
network: z.string(),
|
|
22
|
+
address: z.string().refine(isAddress, 'invalid Obyte address'),
|
|
23
|
+
wif: z.string().min(1),
|
|
24
|
+
createdAt: z.string(),
|
|
25
|
+
})
|
|
26
|
+
.strict();
|
|
27
|
+
|
|
28
|
+
export type WalletFile = z.infer<typeof walletFileSchema>;
|
|
29
|
+
|
|
30
|
+
export function obbyxHome(env: NodeJS.ProcessEnv = process.env): string {
|
|
31
|
+
return env['OBBYX_HOME'] ?? join(homedir(), '.obbyx');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function walletPath(network: ObyteNetwork, env: NodeJS.ProcessEnv = process.env): string {
|
|
35
|
+
return join(obbyxHome(env), `wallet-${network.name}.json`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createWallet(
|
|
39
|
+
network: ObyteNetwork,
|
|
40
|
+
opts: { force?: boolean; env?: NodeJS.ProcessEnv } = {},
|
|
41
|
+
): WalletFile {
|
|
42
|
+
const path = walletPath(network, opts.env);
|
|
43
|
+
if (existsSync(path) && opts.force !== true) {
|
|
44
|
+
throw new ObbyxError(
|
|
45
|
+
'wallet_exists',
|
|
46
|
+
`A wallet already exists at ${path} (use --force to replace it)`,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
let privateKey: Uint8Array;
|
|
50
|
+
let address: string;
|
|
51
|
+
for (;;) {
|
|
52
|
+
privateKey = randomBytes(32);
|
|
53
|
+
try {
|
|
54
|
+
address = addressOfKey(privateKey, network); // rejects the (astronomically unlikely) invalid scalar
|
|
55
|
+
break;
|
|
56
|
+
} catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const wallet: WalletFile = {
|
|
61
|
+
version: 1,
|
|
62
|
+
network: network.caip2,
|
|
63
|
+
address,
|
|
64
|
+
wif: obyte.utils.toWif(privateKey, network.testnet),
|
|
65
|
+
createdAt: new Date().toISOString(),
|
|
66
|
+
};
|
|
67
|
+
mkdirSync(obbyxHome(opts.env), { recursive: true, mode: 0o700 });
|
|
68
|
+
writeFileSync(path, `${JSON.stringify(wallet, null, 2)}\n`, { mode: 0o600 });
|
|
69
|
+
chmodSync(path, 0o600);
|
|
70
|
+
return wallet;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function loadWallet(
|
|
74
|
+
network: ObyteNetwork,
|
|
75
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
76
|
+
): WalletFile & { privateKey: Uint8Array } {
|
|
77
|
+
const path = walletPath(network, env);
|
|
78
|
+
if (!existsSync(path)) {
|
|
79
|
+
throw new ObbyxError(
|
|
80
|
+
'no_wallet',
|
|
81
|
+
`No wallet for ${network.caip2}. Run: obbyx init --network ${network.name}`,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
if ((statSync(path).mode & 0o077) !== 0) {
|
|
85
|
+
throw new ObbyxError(
|
|
86
|
+
'wallet_permissions',
|
|
87
|
+
`${path} is readable by others; run: chmod 600 ${path}`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
let raw: unknown;
|
|
91
|
+
try {
|
|
92
|
+
raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
93
|
+
} catch {
|
|
94
|
+
throw new ObbyxError('wallet_corrupt', `${path} is not valid JSON`);
|
|
95
|
+
}
|
|
96
|
+
const parsed = walletFileSchema.safeParse(raw);
|
|
97
|
+
if (!parsed.success) {
|
|
98
|
+
throw new ObbyxError(
|
|
99
|
+
'wallet_corrupt',
|
|
100
|
+
`${path}: ${parsed.error.issues[0]?.message ?? 'invalid wallet file'}`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
const wallet = parsed.data;
|
|
104
|
+
if (wallet.network !== network.caip2) {
|
|
105
|
+
throw new ObbyxError('wallet_network', `${path} belongs to ${wallet.network}`);
|
|
106
|
+
}
|
|
107
|
+
let privateKey: Uint8Array;
|
|
108
|
+
try {
|
|
109
|
+
privateKey = obyte.utils.fromWif(wallet.wif, network.testnet).privateKey as Uint8Array;
|
|
110
|
+
} catch {
|
|
111
|
+
throw new ObbyxError('wallet_corrupt', `${path}: invalid key for ${network.caip2}`);
|
|
112
|
+
}
|
|
113
|
+
const address = addressOfKey(privateKey, network);
|
|
114
|
+
if (address !== wallet.address)
|
|
115
|
+
throw new ObbyxError('wallet_corrupt', `${path}: key does not match address`);
|
|
116
|
+
return { ...wallet, privateKey };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Address of a single-sig key, via obyte.js public API (signMessage reports its author). */
|
|
120
|
+
export function addressOfKey(privateKey: Uint8Array, network: ObyteNetwork): string {
|
|
121
|
+
const signed = obyte.utils.signMessage('obbyx', {
|
|
122
|
+
privateKey,
|
|
123
|
+
testnet: network.testnet,
|
|
124
|
+
}) as unknown as {
|
|
125
|
+
authors: { address: string }[];
|
|
126
|
+
};
|
|
127
|
+
const address = signed.authors[0]?.address;
|
|
128
|
+
if (address === undefined) throw new ObbyxError('wallet_corrupt', 'Cannot derive address');
|
|
129
|
+
return address;
|
|
130
|
+
}
|