@getpara/cosmjs-v0-integration 0.1.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.
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ParaAminoSigner = exports.ParaProtoSigner = void 0;
13
+ const amino_1 = require("@cosmjs/amino");
14
+ const crypto_1 = require("@cosmjs/crypto");
15
+ const proto_signing_1 = require("@cosmjs/proto-signing");
16
+ const tx_1 = require("cosmjs-types/cosmos/tx/v1beta1/tx");
17
+ const core_sdk_1 = require("@getpara/core-sdk");
18
+ class ParaCosmosSigner {
19
+ /**
20
+ * Signs a message.
21
+ *
22
+ * @param para - the ParaCore instance
23
+ * @param prefix - the cosmos address prefix, defaults to 'cosmos'
24
+ * @param walletId - optional wallet ID to use. If not present, will use the first wallet found.
25
+ * @param messageSigningTimeoutMs - optional timeout in milliseconds. If not present, defaults to 30 seconds.
26
+ **/
27
+ constructor(para, prefix = 'cosmos', walletId, messageSigningTimeoutMs) {
28
+ this.currentWalletId = para.findWalletId(walletId, { type: ['COSMOS'] });
29
+ this.para = para;
30
+ this.prefix = prefix;
31
+ this.messageSigningTimeoutMs = messageSigningTimeoutMs;
32
+ }
33
+ get currentWallet() {
34
+ var _a;
35
+ return ((_a = this.para.wallets[this.currentWalletId]) !== null && _a !== void 0 ? _a : (() => {
36
+ throw new Error(`no valid Para wallet found`);
37
+ })());
38
+ }
39
+ get publicKey() {
40
+ const uncompressedPublicKey = (0, core_sdk_1.hexToUint8Array)(this.currentWallet.publicKey);
41
+ const compressedPublicKey = crypto_1.Secp256k1.compressPubkey(uncompressedPublicKey);
42
+ return compressedPublicKey;
43
+ }
44
+ get address() {
45
+ return (0, core_sdk_1.getCosmosAddress)(this.currentWallet.publicKey, this.prefix);
46
+ }
47
+ getAccounts() {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return [
50
+ {
51
+ algo: 'secp256k1',
52
+ address: this.address,
53
+ pubkey: this.publicKey,
54
+ },
55
+ ];
56
+ });
57
+ }
58
+ }
59
+ class ParaProtoSigner extends ParaCosmosSigner {
60
+ signDirect(address, signDoc) {
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const signBytes = (0, proto_signing_1.makeSignBytes)(signDoc);
63
+ if (address !== this.address) {
64
+ throw new Error(`Address ${address} not found in wallet`);
65
+ }
66
+ const hashedMessage = (0, crypto_1.sha256)(signBytes);
67
+ const signDocJson = tx_1.SignDoc.toJSON(signDoc);
68
+ const signDocJsonStringified = JSON.stringify(signDocJson);
69
+ const signDocJsonStringEncoded = btoa(signDocJsonStringified);
70
+ const res = yield this.para.signMessage({
71
+ walletId: this.currentWallet.id,
72
+ messageBase64: Buffer.from(hashedMessage.buffer).toString('base64'),
73
+ timeoutMs: this.messageSigningTimeoutMs,
74
+ cosmosSignDocBase64: signDocJsonStringEncoded,
75
+ });
76
+ const signature = (0, core_sdk_1.hexToSignature)(`0x${res.signature}`);
77
+ const extendedSignature = new crypto_1.ExtendedSecp256k1Signature((0, core_sdk_1.hexToUint8Array)(signature.r), (0, core_sdk_1.hexToUint8Array)(signature.s), Number(signature.v));
78
+ const signatureBytes = new Uint8Array([...extendedSignature.r(32), ...extendedSignature.s(32)]);
79
+ return {
80
+ signed: signDoc,
81
+ signature: (0, amino_1.encodeSecp256k1Signature)(this.publicKey, signatureBytes),
82
+ };
83
+ });
84
+ }
85
+ }
86
+ exports.ParaProtoSigner = ParaProtoSigner;
87
+ class ParaAminoSigner extends ParaCosmosSigner {
88
+ signAmino(signerAddress, signDoc) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ if (signerAddress !== this.address) {
91
+ throw new Error(`Address ${signerAddress} not found in wallet`);
92
+ }
93
+ const hashedMessage = new crypto_1.Sha256((0, amino_1.serializeSignDoc)(signDoc)).digest();
94
+ const res = yield this.para.signMessage({
95
+ walletId: this.currentWallet.id,
96
+ messageBase64: Buffer.from(hashedMessage.buffer).toString('base64'),
97
+ });
98
+ const signature = (0, core_sdk_1.hexToSignature)(`0x${res.signature}`);
99
+ const extendedSignature = new crypto_1.ExtendedSecp256k1Signature((0, core_sdk_1.hexToUint8Array)(signature.r), (0, core_sdk_1.hexToUint8Array)(signature.s), Number(signature.v));
100
+ const signatureBytes = new Uint8Array([...extendedSignature.r(32), ...extendedSignature.s(32)]);
101
+ return {
102
+ signed: signDoc,
103
+ signature: (0, amino_1.encodeSecp256k1Signature)(this.publicKey, signatureBytes),
104
+ };
105
+ });
106
+ }
107
+ }
108
+ exports.ParaAminoSigner = ParaAminoSigner;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./cosmosSigners.js"), exports);
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,103 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { encodeSecp256k1Signature, serializeSignDoc, } from '@cosmjs/amino';
11
+ import { Secp256k1, Sha256, sha256, ExtendedSecp256k1Signature } from '@cosmjs/crypto';
12
+ import { makeSignBytes } from '@cosmjs/proto-signing';
13
+ import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
14
+ import { hexToSignature, hexToUint8Array, getCosmosAddress, } from '@getpara/core-sdk';
15
+ class ParaCosmosSigner {
16
+ /**
17
+ * Signs a message.
18
+ *
19
+ * @param para - the ParaCore instance
20
+ * @param prefix - the cosmos address prefix, defaults to 'cosmos'
21
+ * @param walletId - optional wallet ID to use. If not present, will use the first wallet found.
22
+ * @param messageSigningTimeoutMs - optional timeout in milliseconds. If not present, defaults to 30 seconds.
23
+ **/
24
+ constructor(para, prefix = 'cosmos', walletId, messageSigningTimeoutMs) {
25
+ this.currentWalletId = para.findWalletId(walletId, { type: ['COSMOS'] });
26
+ this.para = para;
27
+ this.prefix = prefix;
28
+ this.messageSigningTimeoutMs = messageSigningTimeoutMs;
29
+ }
30
+ get currentWallet() {
31
+ var _a;
32
+ return ((_a = this.para.wallets[this.currentWalletId]) !== null && _a !== void 0 ? _a : (() => {
33
+ throw new Error(`no valid Para wallet found`);
34
+ })());
35
+ }
36
+ get publicKey() {
37
+ const uncompressedPublicKey = hexToUint8Array(this.currentWallet.publicKey);
38
+ const compressedPublicKey = Secp256k1.compressPubkey(uncompressedPublicKey);
39
+ return compressedPublicKey;
40
+ }
41
+ get address() {
42
+ return getCosmosAddress(this.currentWallet.publicKey, this.prefix);
43
+ }
44
+ getAccounts() {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ return [
47
+ {
48
+ algo: 'secp256k1',
49
+ address: this.address,
50
+ pubkey: this.publicKey,
51
+ },
52
+ ];
53
+ });
54
+ }
55
+ }
56
+ export class ParaProtoSigner extends ParaCosmosSigner {
57
+ signDirect(address, signDoc) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const signBytes = makeSignBytes(signDoc);
60
+ if (address !== this.address) {
61
+ throw new Error(`Address ${address} not found in wallet`);
62
+ }
63
+ const hashedMessage = sha256(signBytes);
64
+ const signDocJson = SignDoc.toJSON(signDoc);
65
+ const signDocJsonStringified = JSON.stringify(signDocJson);
66
+ const signDocJsonStringEncoded = btoa(signDocJsonStringified);
67
+ const res = yield this.para.signMessage({
68
+ walletId: this.currentWallet.id,
69
+ messageBase64: Buffer.from(hashedMessage.buffer).toString('base64'),
70
+ timeoutMs: this.messageSigningTimeoutMs,
71
+ cosmosSignDocBase64: signDocJsonStringEncoded,
72
+ });
73
+ const signature = hexToSignature(`0x${res.signature}`);
74
+ const extendedSignature = new ExtendedSecp256k1Signature(hexToUint8Array(signature.r), hexToUint8Array(signature.s), Number(signature.v));
75
+ const signatureBytes = new Uint8Array([...extendedSignature.r(32), ...extendedSignature.s(32)]);
76
+ return {
77
+ signed: signDoc,
78
+ signature: encodeSecp256k1Signature(this.publicKey, signatureBytes),
79
+ };
80
+ });
81
+ }
82
+ }
83
+ export class ParaAminoSigner extends ParaCosmosSigner {
84
+ signAmino(signerAddress, signDoc) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ if (signerAddress !== this.address) {
87
+ throw new Error(`Address ${signerAddress} not found in wallet`);
88
+ }
89
+ const hashedMessage = new Sha256(serializeSignDoc(signDoc)).digest();
90
+ const res = yield this.para.signMessage({
91
+ walletId: this.currentWallet.id,
92
+ messageBase64: Buffer.from(hashedMessage.buffer).toString('base64'),
93
+ });
94
+ const signature = hexToSignature(`0x${res.signature}`);
95
+ const extendedSignature = new ExtendedSecp256k1Signature(hexToUint8Array(signature.r), hexToUint8Array(signature.s), Number(signature.v));
96
+ const signatureBytes = new Uint8Array([...extendedSignature.r(32), ...extendedSignature.s(32)]);
97
+ return {
98
+ signed: signDoc,
99
+ signature: encodeSecp256k1Signature(this.publicKey, signatureBytes),
100
+ };
101
+ });
102
+ }
103
+ }
@@ -0,0 +1 @@
1
+ export * from './cosmosSigners.js';
@@ -0,0 +1 @@
1
+ {"type":"module","sideEffects":false}
@@ -0,0 +1,30 @@
1
+ import { AccountData, AminoSignResponse, OfflineAminoSigner, StdSignDoc } from '@cosmjs/amino';
2
+ import { OfflineDirectSigner, DirectSignResponse } from '@cosmjs/proto-signing';
3
+ import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx';
4
+ import ParaCore, { Wallet } from '@getpara/core-sdk';
5
+ declare class ParaCosmosSigner {
6
+ readonly prefix: string;
7
+ readonly para: ParaCore;
8
+ readonly currentWalletId: string;
9
+ readonly messageSigningTimeoutMs?: number;
10
+ /**
11
+ * Signs a message.
12
+ *
13
+ * @param para - the ParaCore instance
14
+ * @param prefix - the cosmos address prefix, defaults to 'cosmos'
15
+ * @param walletId - optional wallet ID to use. If not present, will use the first wallet found.
16
+ * @param messageSigningTimeoutMs - optional timeout in milliseconds. If not present, defaults to 30 seconds.
17
+ **/
18
+ constructor(para: ParaCore, prefix?: string, walletId?: string, messageSigningTimeoutMs?: number);
19
+ get currentWallet(): Wallet;
20
+ get publicKey(): Uint8Array;
21
+ get address(): string;
22
+ getAccounts(): Promise<readonly AccountData[]>;
23
+ }
24
+ export declare class ParaProtoSigner extends ParaCosmosSigner implements OfflineDirectSigner {
25
+ signDirect(address: string, signDoc: SignDoc): Promise<DirectSignResponse>;
26
+ }
27
+ export declare class ParaAminoSigner extends ParaCosmosSigner implements OfflineAminoSigner {
28
+ signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse>;
29
+ }
30
+ export {};
@@ -0,0 +1 @@
1
+ export * from './cosmosSigners.js';
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@getpara/cosmjs-v0-integration",
3
+ "version": "0.1.0",
4
+ "main": "dist/cjs/index.js",
5
+ "module": "dist/esm/index.js",
6
+ "types": "dist/types/index.d.ts",
7
+ "typings": "dist/types/index.d.ts",
8
+ "sideEffects": false,
9
+ "dependencies": {
10
+ "@getpara/core-sdk": "0.1.0"
11
+ },
12
+ "scripts": {
13
+ "build": "yarn build:cjs && yarn build:esm && yarn build:types",
14
+ "build:cjs": "rm -rf dist/cjs && tsc --module commonjs --outDir dist/cjs && printf '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
15
+ "build:esm": "rm -rf dist/esm && tsc --module es6 --outDir dist/esm && printf '{\"type\":\"module\",\"sideEffects\":false}' > dist/esm/package.json",
16
+ "build:types": "rm -rf dist/types && tsc --module es6 --declarationDir dist/types --emitDeclarationOnly --declaration"
17
+ },
18
+ "devDependencies": {
19
+ "@cosmjs/amino": "^0.32.3",
20
+ "@cosmjs/encoding": "^0.32.3",
21
+ "@cosmjs/proto-signing": "^0.32.3",
22
+ "cosmjs-types": "0.9.0",
23
+ "typescript": "5.1.6"
24
+ },
25
+ "peerDependencies": {
26
+ "@cosmjs/amino": ">= 0.31.3 < 1",
27
+ "@cosmjs/encoding": ">= 0.31.3 < 1",
28
+ "@cosmjs/proto-signing": ">= 0.31.3 < 1"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "package.json"
33
+ ],
34
+ "gitHead": "625aaa94001a5461dcde8d6775c3b73f3862c76c"
35
+ }