Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add toData function for SignatureV2 #210

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/core/SignatureV2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { CompactBitArray } from './CompactBitArray';
import { SimplePublicKey } from './PublicKey';
import { SignatureV2 } from './SignatureV2';

describe('SignatureV2', () => {
it('conversion: sigle sign', () => {
const sequence = 1234;
//const data = new SignatureV2.Descriptor(
const signMode = SignatureV2.SignMode.SIGN_MODE_DIRECT;
const signature = 'fakesignature';

const sigv2 = new SignatureV2(
new SimplePublicKey('A/PwvW/JLEnhb0/o5g+AnOqMN+FFT24gjJfDtA1tBsBv'),
new SignatureV2.Descriptor(
new SignatureV2.Descriptor.Single(signMode, signature)
),
sequence
);

const toData = sigv2.toData();
console.log(`toData : ${JSON.stringify(toData)}`);
const fromData = SignatureV2.fromData(toData);
console.log(`fromData : ${JSON.stringify(fromData)}`);
const toData2 = fromData.toData();
console.log(`toData2 : ${JSON.stringify(toData2)}`);

expect(toData.public_key).toEqual(toData2.public_key);
expect(toData.sequence).toEqual(toData2.sequence);
expect(toData.data).toEqual(toData2.data);
});

it('conversion: multi sign', () => {
const sequence = 1234;
//const data = new SignatureV2.Descriptor(
const signMode = SignatureV2.SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
const signature = 'fakesignature';
const single = new SignatureV2.Descriptor(
new SignatureV2.Descriptor.Single(signMode, signature)
);

const sigv2 = new SignatureV2(
new SimplePublicKey('A/PwvW/JLEnhb0/o5g+AnOqMN+FFT24gjJfDtA1tBsBv'),
new SignatureV2.Descriptor(
new SignatureV2.Descriptor.Multi(CompactBitArray.fromBits(2), [
single,
single,
])
),
sequence
);

const toData = sigv2.toData();
console.log(`toData : ${JSON.stringify(toData)}`);
const fromData = SignatureV2.fromData(toData);
console.log(`fromData : ${JSON.stringify(fromData)}`);
const toData2 = fromData.toData();
console.log(`toData2 : ${JSON.stringify(toData2)}`);

expect(toData.public_key).toEqual(toData2.public_key);
expect(toData.sequence).toEqual(toData2.sequence);
expect(toData.data).toEqual(toData2.data);
});
});
41 changes: 41 additions & 0 deletions src/core/SignatureV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { PublicKey } from './PublicKey';
import { ModeInfo } from './Tx';
import { CompactBitArray } from './CompactBitArray';
import {
SignMode,
SignMode as SignMode_pb,
signModeFromJSON,
signModeToJSON,
} from '@terra-money/terra.proto/cosmos/tx/signing/v1beta1/signing';
import { MultiSignature } from '@terra-money/terra.proto/cosmos/crypto/multisig/v1beta1/multisig';

Expand All @@ -22,6 +24,14 @@ export class SignatureV2 {
);
}

public toData(): SignatureV2.Data {
return {
public_key: this.public_key.toData(),
data: this.data.toData(),
sequence: this.sequence.toFixed(),
};
}

public static fromAmino(data: SignatureV2.Amino): SignatureV2 {
return new SignatureV2(
PublicKey.fromAmino(data.pub_key),
Expand Down Expand Up @@ -72,6 +82,22 @@ export namespace SignatureV2 {
throw new Error('must be one of single or multi');
}

public toData(): Descriptor.Data {
if (this.single) {
return {
single: this.single.toData(),
};
}

if (this.multi) {
return {
multi: this.multi.toData(),
};
}

throw new Error('must be one of single or multi');
}

public toModeInfoAndSignature(): [ModeInfo, Uint8Array] {
if (this.single) {
const sigData = this.single;
Expand Down Expand Up @@ -119,6 +145,14 @@ export namespace SignatureV2 {
public static fromData(data: Single.Data): Single {
return new Single(signModeFromJSON(data.mode), data.signature);
}

public toData(): Single.Data {
const { mode, signature } = this;
return {
mode: signModeToJSON(mode),
signature,
};
}
}

export namespace Single {
Expand All @@ -140,6 +174,13 @@ export namespace SignatureV2 {
data.signatures.map(v => Descriptor.fromData(v))
);
}

public toData(): Multi.Data {
return {
bitarray: this.bitarray.toData(),
signatures: this.signatures.map(sig => sig.toData()),
};
}
}

export namespace Multi {
Expand Down