ecash-lib 1.5.1 → 2.0.0
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/README.md +4 -39
- package/dist/ecc.d.ts +3 -7
- package/dist/ecc.d.ts.map +1 -1
- package/dist/ecc.js +33 -2
- package/dist/ecc.js.map +1 -1
- package/dist/ffi/ecash_lib_wasm_bg_browser.d.ts +1 -0
- package/dist/ffi/ecash_lib_wasm_bg_browser.js +1571 -0
- package/dist/ffi/ecash_lib_wasm_bg_browser.wasm +0 -0
- package/dist/ffi/ecash_lib_wasm_bg_nodejs.wasm +0 -0
- package/dist/hash.d.ts +6 -6
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +7 -2
- package/dist/hash.js.map +1 -1
- package/dist/hdwallet.d.ts +2 -4
- package/dist/hdwallet.d.ts.map +1 -1
- package/dist/hdwallet.js +6 -7
- package/dist/hdwallet.js.map +1 -1
- package/dist/initBrowser.d.ts +1 -11
- package/dist/initBrowser.d.ts.map +1 -1
- package/dist/initBrowser.js +14 -25
- package/dist/initBrowser.js.map +1 -1
- package/dist/initNodeJs.d.ts +1 -2
- package/dist/initNodeJs.d.ts.map +1 -1
- package/dist/initNodeJs.js +9 -14
- package/dist/initNodeJs.js.map +1 -1
- package/dist/test/testRunner.d.ts +0 -2
- package/dist/test/testRunner.d.ts.map +1 -1
- package/dist/test/testRunner.js +3 -6
- package/dist/test/testRunner.js.map +1 -1
- package/dist/txBuilder.d.ts +5 -1
- package/dist/txBuilder.d.ts.map +1 -1
- package/dist/txBuilder.js +8 -7
- package/dist/txBuilder.js.map +1 -1
- package/package.json +1 -1
- package/src/ecc.ts +39 -5
- package/src/ffi/ecash_lib_wasm_bg_browser.d.ts +1 -0
- package/src/ffi/ecash_lib_wasm_bg_browser.js +1571 -0
- package/src/ffi/ecash_lib_wasm_bg_browser.wasm +0 -0
- package/src/ffi/ecash_lib_wasm_bg_nodejs.wasm +0 -0
- package/src/hash.ts +19 -14
- package/src/hdwallet.ts +4 -8
- package/src/initBrowser.ts +18 -25
- package/src/initNodeJs.ts +9 -12
- package/src/test/testRunner.ts +3 -11
- package/src/txBuilder.ts +12 -7
package/README.md
CHANGED
|
@@ -18,27 +18,9 @@ This library works for both browser and NodeJS.
|
|
|
18
18
|
|
|
19
19
|
`npm install --save ecash-lib`
|
|
20
20
|
|
|
21
|
-
### Setup
|
|
22
|
-
|
|
23
|
-
To use this library, you first have to initialize the WebAssembly module:
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import { initWasm } from 'ecash-lib';
|
|
27
|
-
await initWasm();
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
After that, to sign signatures, you need an "Ecc" instance:
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
import { Ecc } from 'ecash-lib';
|
|
34
|
-
const ecc = new Ecc();
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
**Note: You should only call this function once, as it's fairly expensive to setup, it internally precomputes some elliptic curve field elements, which takes some time**
|
|
38
|
-
|
|
39
21
|
### Usage
|
|
40
22
|
|
|
41
|
-
|
|
23
|
+
Here's how to sign your first transaction:
|
|
42
24
|
|
|
43
25
|
```ts
|
|
44
26
|
import {
|
|
@@ -47,20 +29,15 @@ import {
|
|
|
47
29
|
Script,
|
|
48
30
|
TxBuilder,
|
|
49
31
|
fromHex,
|
|
50
|
-
initWasm,
|
|
51
32
|
shaRmd160,
|
|
52
33
|
toHex,
|
|
53
34
|
ALL_BIP143,
|
|
54
35
|
} from 'ecash-lib';
|
|
55
36
|
|
|
56
|
-
// Download and compile WebAssembly
|
|
57
|
-
await initWasm();
|
|
58
|
-
// Build a signature context for elliptic curve cryptography (ECC)
|
|
59
|
-
const ecc = new Ecc();
|
|
60
37
|
const walletSk = fromHex(
|
|
61
38
|
'e6ae1669c47d092eff3eb652bea535331c338e29f34be709bc4055655cd0e950',
|
|
62
39
|
);
|
|
63
|
-
const walletPk =
|
|
40
|
+
const walletPk = new Ecc().derivePubkey(walletSk);
|
|
64
41
|
const walletPkh = shaRmd160(walletPk);
|
|
65
42
|
const walletP2pkh = Script.p2pkh(walletPkh);
|
|
66
43
|
// TxId with unspent funds for the above wallet
|
|
@@ -90,24 +67,11 @@ const txBuild = new TxBuilder({
|
|
|
90
67
|
walletP2pkh,
|
|
91
68
|
],
|
|
92
69
|
});
|
|
93
|
-
const tx = txBuild.sign(
|
|
70
|
+
const tx = txBuild.sign({ feePerKb: 1000, dustLimit: 546 });
|
|
94
71
|
const rawTx = tx.ser();
|
|
95
72
|
console.log(toHex(rawTx));
|
|
96
73
|
```
|
|
97
74
|
|
|
98
|
-
### Troubleshooting
|
|
99
|
-
|
|
100
|
-
#### Can't load WebAssembly
|
|
101
|
-
|
|
102
|
-
Some bundlers can't handle WebAssembly yet (at the time of writing, vite).
|
|
103
|
-
If you run into "CompileError: expected magic word 00 61 73 6d", you can
|
|
104
|
-
provide a custom WASM URL or module:
|
|
105
|
-
|
|
106
|
-
```ts
|
|
107
|
-
import ecashLibWasmUrl from 'ecash-lib/dist/ffi/ecash_lib_wasm_bg_browser.wasm?url';
|
|
108
|
-
await initWasm(ecashLibWasmUrl);
|
|
109
|
-
```
|
|
110
|
-
|
|
111
75
|
## Changelog
|
|
112
76
|
|
|
113
77
|
- 0.1.1 - Validation that feePerKb is an integer
|
|
@@ -125,3 +89,4 @@ await initWasm(ecashLibWasmUrl);
|
|
|
125
89
|
- 1.4.1 - Patch import in `mnemonic.ts` [D17621](https://reviews.bitcoinabc.org/D17621)
|
|
126
90
|
- 1.5.0 - Support custom WASM URL and module [D17622](https://reviews.bitcoinabc.org/D17622)
|
|
127
91
|
- 1.5.1 - `Address.withPrefix()` returns same prefix if unchanged (instead of throwing an error) [D17623](https://reviews.bitcoinabc.org/D17623)
|
|
92
|
+
- 2.0.0 - Remove `initWasm`, auto-load the WebAssembly instead. Remove unneeded `ecc` parameters, esp. in `TxBuilder.sign` and `HdNode.fromSeed` [D17639](https://reviews.bitcoinabc.org/D17639) [D17640](https://reviews.bitcoinabc.org/D17640)
|
package/dist/ecc.d.ts
CHANGED
|
@@ -16,10 +16,6 @@ export interface Ecc {
|
|
|
16
16
|
/** Add a scalar to a public key (adding G*b) */
|
|
17
17
|
pubkeyAdd(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
18
18
|
}
|
|
19
|
-
/** Ecc implementation using WebAssembly */
|
|
20
|
-
export declare let Ecc: {
|
|
21
|
-
new (): Ecc;
|
|
22
|
-
};
|
|
23
19
|
/** Dummy Ecc impl that always returns 0, useful for measuring tx size */
|
|
24
20
|
export declare class EccDummy implements Ecc {
|
|
25
21
|
derivePubkey(_seckey: Uint8Array): Uint8Array;
|
|
@@ -29,7 +25,7 @@ export declare class EccDummy implements Ecc {
|
|
|
29
25
|
seckeyAdd(_a: Uint8Array, _b: Uint8Array): Uint8Array;
|
|
30
26
|
pubkeyAdd(_a: Uint8Array, _b: Uint8Array): Uint8Array;
|
|
31
27
|
}
|
|
32
|
-
export declare function __setEcc(ecc:
|
|
33
|
-
|
|
34
|
-
}
|
|
28
|
+
export declare function __setEcc(ecc: Ecc): void;
|
|
29
|
+
export declare class Ecc implements Ecc {
|
|
30
|
+
}
|
|
35
31
|
//# sourceMappingURL=ecc.d.ts.map
|
package/dist/ecc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecc.d.ts","sourceRoot":"","sources":["../src/ecc.ts"],"names":[],"mappings":"AAIA,6DAA6D;AAC7D,MAAM,WAAW,GAAG;IAChB,2CAA2C;IAC3C,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC;IAE7C,8DAA8D;IAC9D,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,UAAU,CAAC;IAE3D,+DAA+D;IAC/D,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,UAAU,CAAC;IAE7D;;;OAGG;IACH,aAAa,C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AG,UAAU;IAIrD,SAAS,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,UAAU,GAAG,UAAU;CAGxD;AAID,wBAAgB,QAAQ,CAAC,GAAG,EAAE,GAAG,QAEhC;AAED,qBAAa,GAAI,YAAW,GAAG;CAiC9B"}
|
package/dist/ecc.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Distributed under the MIT software license, see the accompanying
|
|
4
4
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.Ecc = exports.__setEcc = exports.EccDummy = void 0;
|
|
7
7
|
/** Dummy Ecc impl that always returns 0, useful for measuring tx size */
|
|
8
8
|
class EccDummy {
|
|
9
9
|
derivePubkey(_seckey) {
|
|
@@ -26,8 +26,39 @@ class EccDummy {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
exports.EccDummy = EccDummy;
|
|
29
|
+
const ECC = {};
|
|
29
30
|
function __setEcc(ecc) {
|
|
30
|
-
|
|
31
|
+
ECC.ecc = ecc;
|
|
31
32
|
}
|
|
32
33
|
exports.__setEcc = __setEcc;
|
|
34
|
+
class Ecc {
|
|
35
|
+
/** Derive a public key from secret key. */
|
|
36
|
+
derivePubkey(seckey) {
|
|
37
|
+
return ECC.ecc.derivePubkey(seckey);
|
|
38
|
+
}
|
|
39
|
+
/** Sign an ECDSA signature. msg needs to be a 32-byte hash */
|
|
40
|
+
ecdsaSign(seckey, msg) {
|
|
41
|
+
return ECC.ecc.ecdsaSign(seckey, msg);
|
|
42
|
+
}
|
|
43
|
+
/** Sign a Schnorr signature. msg needs to be a 32-byte hash */
|
|
44
|
+
schnorrSign(seckey, msg) {
|
|
45
|
+
return ECC.ecc.schnorrSign(seckey, msg);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Return whether the given secret key is valid, i.e. whether is of correct
|
|
49
|
+
* length (32 bytes) and is on the curve.
|
|
50
|
+
*/
|
|
51
|
+
isValidSeckey(seckey) {
|
|
52
|
+
return ECC.ecc.isValidSeckey(seckey);
|
|
53
|
+
}
|
|
54
|
+
/** Add a scalar to a secret key */
|
|
55
|
+
seckeyAdd(a, b) {
|
|
56
|
+
return ECC.ecc.seckeyAdd(a, b);
|
|
57
|
+
}
|
|
58
|
+
/** Add a scalar to a public key (adding G*b) */
|
|
59
|
+
pubkeyAdd(a, b) {
|
|
60
|
+
return ECC.ecc.pubkeyAdd(a, b);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.Ecc = Ecc;
|
|
33
64
|
//# sourceMappingURL=ecc.js.map
|
package/dist/ecc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ecc.js","sourceRoot":"","sources":["../src/ecc.ts"],"names":[],"mappings":";AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;;;
|
|
1
|
+
{"version":3,"file":"ecc.js","sourceRoot":"","sources":["../src/ecc.ts"],"names":[],"mappings":";AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;;;AA0BtE,yEAAyE;AACzE,MAAa,QAAQ;IACjB,YAAY,CAAC,OAAmB;QAC5B,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,SAAS,CAAC,OAAmB,EAAE,IAAgB;QAC3C,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,WAAW,CAAC,OAAmB,EAAE,IAAgB;QAC7C,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,aAAa,CAAC,OAAmB;QAC7B,OAAO,KAAK,CAAC;IACjB,CAAC;IAED,SAAS,CAAC,EAAc,EAAE,EAAc;QACpC,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,SAAS,CAAC,EAAc,EAAE,EAAc;QACpC,OAAO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