-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk upgraded with multisignature feature (#7)
* sdk updated with multisignature feature * bump: [email protected] * protobuf removed * comments added to amino encoding
- Loading branch information
1 parent
c78b2be
commit 88a9c3a
Showing
7 changed files
with
288 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { fromBase64, fromHex, toBech32 } from "@cosmjs/encoding" | ||
import { Pubkey, isEd25519Pubkey, isMultisigThresholdPubkey, isSecp256k1Pubkey } from "@cosmjs/amino" | ||
import { Uint53 } from "@cosmjs/math" | ||
import { rawSecp256k1PubkeyToRawAddress } from "./secp256k1"; | ||
import { sha256 } from '@cosmjs/crypto' | ||
|
||
export function encodeAminoPubkey(pubkey: Pubkey) { | ||
const pubkeyAminoPrefixMultisigThreshold = fromHex("22c1f7e2" /* variable length not included */); | ||
const pubkeyAminoPrefixSecp256k1 = fromHex("21" /* fixed length */); | ||
const pubkeyAminoPrefixEd25519 = fromHex("1624de64" + "20" /* fixed length */); | ||
|
||
|
||
function encodeUvarint(value: number | string) { | ||
const checked = Uint53.fromString(value.toString()).toNumber(); | ||
if (checked > 127) { | ||
throw new Error("Encoding numbers > 127 is not supported here. Please tell those lazy CosmJS maintainers to port the binary.PutUvarint implementation from the Go standard library and write some tests."); | ||
} | ||
return [checked]; | ||
} | ||
|
||
if (isMultisigThresholdPubkey(pubkey)) { | ||
const out = Array.from(pubkeyAminoPrefixMultisigThreshold); | ||
out.push(0x08); // https://github.com/cosmos/cosmjs/blob/v0.31.1/packages/amino/src/encoding.ts#L198 | ||
out.push(...encodeUvarint(pubkey.value.threshold)); | ||
for (const pubkeyData of pubkey.value.pubkeys.map((p) => encodeAminoPubkey(p))) { | ||
out.push(0x12); // https://github.com/cosmos/cosmjs/blob/v0.31.1/packages/amino/src/encoding.ts#L201 | ||
out.push(...encodeUvarint(pubkeyData.length)); | ||
out.push(...pubkeyData); | ||
} | ||
return new Uint8Array(out); | ||
} | ||
else if (isEd25519Pubkey(pubkey)) { | ||
return new Uint8Array([...pubkeyAminoPrefixEd25519, ...fromBase64(pubkey.value)]); | ||
} | ||
else if (isSecp256k1Pubkey(pubkey)) { | ||
return new Uint8Array([...pubkeyAminoPrefixSecp256k1, ...fromBase64(pubkey.value)]); | ||
} | ||
else { | ||
throw new Error("Unsupported pubkey type"); | ||
} | ||
} | ||
|
||
export function pubkeyToAddress(pubkey: Pubkey) { | ||
if(isSecp256k1Pubkey(pubkey)) { | ||
const buf = Buffer.from(pubkey.value, "base64"); | ||
const bytes = Uint8Array.from(buf); | ||
return toBech32('swtr', rawSecp256k1PubkeyToRawAddress(bytes)); | ||
} else if(isMultisigThresholdPubkey(pubkey)) { | ||
const pubkeyData = encodeAminoPubkey(pubkey); | ||
return toBech32('swtr', sha256(pubkeyData).slice(0, 20)); | ||
} | ||
else { | ||
throw new Error("Unsupported public key type"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './directsecp256k1hdwallet.js' | ||
export * from './directsecp256k1wallet.js' | ||
export * from './secp256k1.js' | ||
export * from './wallet.js' | ||
export * from './wallet.js' | ||
export * from './address.js' | ||
export * from './multisignature.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { pubkeyToAddress } from "./address"; | ||
import { | ||
StdFee, | ||
isSecp256k1Pubkey, | ||
isMultisigThresholdPubkey, | ||
MultisigThresholdPubkey, | ||
isEd25519Pubkey, | ||
Pubkey, | ||
} from "@cosmjs/amino"; | ||
import { PubKey as Secp256k1PubKey } from "cosmjs-types/cosmos/crypto/secp256k1/keys.js"; | ||
import { PubKey as Ed25519PubKey } from "cosmjs-types/cosmos/crypto/ed25519/keys.js"; | ||
import { fromBase64 } from "@cosmjs/encoding"; | ||
import { LegacyAminoPubKey } from "cosmjs-types/cosmos/crypto/multisig/keys"; | ||
import { Uint53 } from "@cosmjs/math"; | ||
import { Any } from "../types-proto/google/protobuf/any.js"; | ||
import { makeCompactBitArray } from "@cosmjs/stargate/build/multisignature.js"; | ||
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing.js"; | ||
import Long from "long"; | ||
import { AuthInfo, TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx.js"; | ||
import { MultiSignature } from "cosmjs-types/cosmos/crypto/multisig/v1beta1/multisig.js"; | ||
|
||
export function makeMultisignedTxBytes( | ||
multisigPubkey: MultisigThresholdPubkey, | ||
sequence: number, | ||
fee: StdFee, | ||
bodyBytes: Uint8Array, | ||
signatures: Map<string, Uint8Array> | ||
): Uint8Array { | ||
const signedTx = makeMultisignedTx( | ||
multisigPubkey, | ||
sequence, | ||
fee, | ||
bodyBytes, | ||
signatures | ||
); | ||
return TxRaw.encode(signedTx).finish(); | ||
} | ||
|
||
export function makeMultisignedTx( | ||
multisigPubkey: MultisigThresholdPubkey, | ||
sequence: number, | ||
fee: StdFee, | ||
bodyBytes: Uint8Array, | ||
signatures: Map<string, Uint8Array> | ||
) { | ||
const signers = Array(multisigPubkey.value.pubkeys.length).fill(false); | ||
const signaturesList = new Array(); | ||
for (let i = 0; i < multisigPubkey.value.pubkeys.length; i++) { | ||
const signerAddress = pubkeyToAddress(multisigPubkey.value.pubkeys[i]); | ||
const signature = signatures.get(signerAddress); | ||
if (signature) { | ||
signers[i] = true; | ||
signaturesList.push(signature); | ||
} | ||
} | ||
const signerInfo = { | ||
publicKey: encodePubkey(multisigPubkey), | ||
modeInfo: { | ||
multi: { | ||
bitarray: makeCompactBitArray(signers), | ||
modeInfos: signaturesList.map((_) => ({ | ||
single: { mode: SignMode.SIGN_MODE_LEGACY_AMINO_JSON }, | ||
})), | ||
}, | ||
}, | ||
sequence: Long.fromNumber(sequence), | ||
} as any; | ||
|
||
const authInfo = AuthInfo.fromPartial({ | ||
signerInfos: [signerInfo], | ||
fee: { | ||
amount: [...fee.amount], | ||
gasLimit: Long.fromNumber(+fee.gas) as any, | ||
}, | ||
}); | ||
|
||
const authInfoBytes = AuthInfo.encode(authInfo).finish(); | ||
const signedTx = TxRaw.fromPartial({ | ||
bodyBytes: bodyBytes, | ||
authInfoBytes: authInfoBytes, | ||
signatures: [ | ||
MultiSignature.encode( | ||
MultiSignature.fromPartial({ signatures: signaturesList }) | ||
).finish(), | ||
], | ||
}); | ||
return signedTx; | ||
} | ||
|
||
function encodePubkey(pubkey: Pubkey) { | ||
if (isSecp256k1Pubkey(pubkey)) { | ||
const pubkeyProto = Secp256k1PubKey.fromPartial({ | ||
key: fromBase64(pubkey.value), | ||
}); | ||
|
||
const anyPubkey = Any.fromPartial({ | ||
typeUrl: "/ethermint.crypto.v1.ethsecp256k1.PubKey", | ||
value: Secp256k1PubKey.encode(pubkeyProto).finish(), | ||
}); | ||
|
||
return anyPubkey; | ||
} else if (isEd25519Pubkey(pubkey)) { | ||
const pubkeyProto = Ed25519PubKey.fromPartial({ | ||
key: fromBase64(pubkey.value), | ||
}); | ||
return Any.fromPartial({ | ||
typeUrl: "/cosmos.crypto.ed25519.PubKey", | ||
value: Uint8Array.from(Ed25519PubKey.encode(pubkeyProto).finish()), | ||
}); | ||
} else if (isMultisigThresholdPubkey(pubkey)) { | ||
const pubkeyProto = LegacyAminoPubKey.fromPartial({ | ||
threshold: Uint53.fromString(pubkey.value.threshold).toNumber(), | ||
publicKeys: pubkey.value.pubkeys.map(encodePubkey), | ||
}) as any; | ||
|
||
return Any.fromPartial({ | ||
typeUrl: "/cosmos.crypto.multisig.LegacyAminoPubKey", | ||
value: Uint8Array.from(LegacyAminoPubKey.encode(pubkeyProto).finish()), | ||
}); | ||
} else { | ||
throw new Error(`Pubkey type ${pubkey.type} not recognized`); | ||
} | ||
} |
Oops, something went wrong.