-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/phase-2' into dev
- Loading branch information
Showing
35 changed files
with
1,800 additions
and
40 deletions.
There are no files selected for viewing
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
3 changes: 3 additions & 0 deletions
3
hasura/metadata/databases/punkga-pg/tables/public_system_key.yaml
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,3 @@ | ||
table: | ||
name: system_key | ||
schema: public |
3 changes: 3 additions & 0 deletions
3
hasura/metadata/databases/punkga-pg/tables/public_user_wallet.yaml
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,3 @@ | ||
table: | ||
name: user_wallet | ||
schema: public |
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
1 change: 1 addition & 0 deletions
1
hasura/migrations/punkga-pg/1697081234401_create_table_public_system_key/down.sql
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 @@ | ||
DROP TABLE "public"."system_key"; |
1 change: 1 addition & 0 deletions
1
hasura/migrations/punkga-pg/1697081234401_create_table_public_system_key/up.sql
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 @@ | ||
CREATE TABLE "public"."system_key" ("id" serial NOT NULL, "encrypted_seed" text NOT NULL, PRIMARY KEY ("id") ); |
1 change: 1 addition & 0 deletions
1
hasura/migrations/punkga-pg/1697181470204_create_table_public_user_wallet/down.sql
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 @@ | ||
DROP TABLE "public"."user_wallet"; |
1 change: 1 addition & 0 deletions
1
hasura/migrations/punkga-pg/1697181470204_create_table_public_user_wallet/up.sql
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 @@ | ||
CREATE TABLE "public"."user_wallet" ("id" serial NOT NULL, "address" text NOT NULL, "data" text NOT NULL, "user_id" bpchar, "is_master_wallet" boolean NOT NULL DEFAULT false, PRIMARY KEY ("id") , FOREIGN KEY ("user_id") REFERENCES "public"."authorizer_users"("id") ON UPDATE restrict ON DELETE restrict, UNIQUE ("address"), UNIQUE ("user_id")); |
4 changes: 4 additions & 0 deletions
4
...ons/punkga-pg/1697181476394_alter_table_public_user_wallet_add_column_created_at/down.sql
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,4 @@ | ||
-- Could not auto-generate a down migration. | ||
-- Please write an appropriate down migration for the SQL below: | ||
-- alter table "public"."user_wallet" add column "created_at" timestamptz | ||
-- null default now(); |
2 changes: 2 additions & 0 deletions
2
...tions/punkga-pg/1697181476394_alter_table_public_user_wallet_add_column_created_at/up.sql
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,2 @@ | ||
alter table "public"."user_wallet" add column "created_at" timestamptz | ||
null default now(); |
21 changes: 21 additions & 0 deletions
21
...ons/punkga-pg/1697181480144_alter_table_public_user_wallet_add_column_updated_at/down.sql
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,21 @@ | ||
-- Could not auto-generate a down migration. | ||
-- Please write an appropriate down migration for the SQL below: | ||
-- alter table "public"."user_wallet" add column "updated_at" timestamptz | ||
-- null default now(); | ||
-- | ||
-- CREATE OR REPLACE FUNCTION "public"."set_current_timestamp_updated_at"() | ||
-- RETURNS TRIGGER AS $$ | ||
-- DECLARE | ||
-- _new record; | ||
-- BEGIN | ||
-- _new := NEW; | ||
-- _new."updated_at" = NOW(); | ||
-- RETURN _new; | ||
-- END; | ||
-- $$ LANGUAGE plpgsql; | ||
-- CREATE TRIGGER "set_public_user_wallet_updated_at" | ||
-- BEFORE UPDATE ON "public"."user_wallet" | ||
-- FOR EACH ROW | ||
-- EXECUTE PROCEDURE "public"."set_current_timestamp_updated_at"(); | ||
-- COMMENT ON TRIGGER "set_public_user_wallet_updated_at" ON "public"."user_wallet" | ||
-- IS 'trigger to set value of column "updated_at" to current timestamp on row update'; |
19 changes: 19 additions & 0 deletions
19
...tions/punkga-pg/1697181480144_alter_table_public_user_wallet_add_column_updated_at/up.sql
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,19 @@ | ||
alter table "public"."user_wallet" add column "updated_at" timestamptz | ||
null default now(); | ||
|
||
CREATE OR REPLACE FUNCTION "public"."set_current_timestamp_updated_at"() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
_new record; | ||
BEGIN | ||
_new := NEW; | ||
_new."updated_at" = NOW(); | ||
RETURN _new; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
CREATE TRIGGER "set_public_user_wallet_updated_at" | ||
BEFORE UPDATE ON "public"."user_wallet" | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE "public"."set_current_timestamp_updated_at"(); | ||
COMMENT ON TRIGGER "set_public_user_wallet_updated_at" ON "public"."user_wallet" | ||
IS 'trigger to set value of column "updated_at" to current timestamp on row update'; |
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,67 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
import { GraphqlService } from '../graphql/graphql.service'; | ||
|
||
@Injectable() | ||
export class KeysGraphql { | ||
private readonly logger = new Logger(KeysGraphql.name); | ||
constructor( | ||
private configSvc: ConfigService, | ||
private graphqlSvc: GraphqlService | ||
) {} | ||
|
||
async queryEncryptedSeed() { | ||
const headers = { | ||
'x-hasura-admin-secret': this.configSvc.get<string>( | ||
'graphql.adminSecret' | ||
), | ||
}; | ||
|
||
const result = await this.graphqlSvc.query( | ||
this.configSvc.get<string>('graphql.endpoint'), | ||
'', | ||
`query system_key { | ||
system_key { | ||
encrypted_seed | ||
} | ||
} | ||
`, | ||
'system_key', | ||
{}, | ||
headers | ||
); | ||
|
||
return result.data.system_key[0]; | ||
} | ||
|
||
async storeEncryptedSeed(encryptedSeed: string) { | ||
const headers = { | ||
'x-hasura-admin-secret': this.configSvc.get<string>( | ||
'graphql.adminSecret' | ||
), | ||
}; | ||
|
||
const result = await this.graphqlSvc.query( | ||
this.configSvc.get<string>('graphql.endpoint'), | ||
'', | ||
`mutation insert_system_key($encrypted_seed: String!) { | ||
insert_system_key(objects: {encrypted_seed: $encrypted_seed}) { | ||
affected_rows | ||
} | ||
}`, | ||
'insert_system_key', | ||
{ | ||
encrypted_seed: encryptedSeed, | ||
}, | ||
headers | ||
); | ||
|
||
this.logger.debug(result); | ||
if (result.data?.insert_system_key?.affected_rows > 0) { | ||
this.logger.debug(`Insert system key success`); | ||
} else { | ||
this.logger.debug(`Insert system key fail`); | ||
} | ||
} | ||
} |
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,81 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { KMSService } from '@aura-nw/aura-kms'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { Secp256k1HdWallet } from '@cosmjs/amino'; | ||
import { GasPrice } from '@cosmjs/stargate'; | ||
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'; | ||
|
||
@Injectable() | ||
export class KMSBuilderService { | ||
private readonly logger = new Logger(KMSBuilderService.name); | ||
private SystemKey = null; | ||
private KMS: KMSService = null; | ||
|
||
constructor(private configService: ConfigService) { | ||
this.KMS = new KMSService( | ||
this.configService.get<string>('kms.accessKeyId'), | ||
this.configService.get<string>('kms.secretAccessKey'), | ||
this.configService.get<string>('kms.region'), | ||
this.configService.get<string>('kms.apiVersion') | ||
); | ||
} | ||
|
||
async getSeed(encryptedSeed: string) { | ||
const buffer = Buffer.from(encryptedSeed, 'base64'); | ||
const keyId = 'alias/' + this.configService.get<string>('kms.alias'); | ||
const decryptedData = await this.KMS.decrypt(keyId, buffer); | ||
|
||
const bufferSeed = Buffer.from(decryptedData.Plaintext, 'base64'); | ||
const originalSeed = bufferSeed.toString('ascii'); | ||
return originalSeed; | ||
} | ||
|
||
async encryptSeed(seed: string) { | ||
const keyId = 'alias/' + this.configService.get<string>('kms.alias'); | ||
const encryptedData = await this.KMS.encrypt(keyId, seed); | ||
|
||
return Buffer.from(encryptedData.CiphertextBlob).toString('base64'); | ||
} | ||
|
||
// async getClient(wallet: Secp256k1HdWallet) { | ||
// const rpcEndpoint = this.configService.get<string>('rpc.endpoint'); | ||
// const gasPrice = GasPrice.fromString( | ||
// this.configService.get<string>('aura.gasprice') | ||
// ); | ||
// return SigningCosmWasmClient.connectWithSigner(rpcEndpoint, wallet, { | ||
// gasPrice, | ||
// }); | ||
// } | ||
|
||
// async transferToken(senderInfo: any, msgs: any, memo: string, token: string) { | ||
// // try { | ||
// const data = senderInfo.encrypt_key; | ||
// const deserializeData = { | ||
// type: 'secp256k1wallet-v1', | ||
// kdf: { | ||
// algorithm: 'argon2id', | ||
// params: { outputLength: 32, opsLimit: 24, memLimitKib: 12288 }, | ||
// }, | ||
// encryption: { algorithm: 'xchacha20poly1305-ietf' }, | ||
// data: data, | ||
// }; | ||
// // get secret key | ||
// if (!this.SystemKey) { | ||
// this.SystemKey = await this.getSystemKey(token); | ||
// } | ||
// const wallet = await Secp256k1HdWallet.deserialize( | ||
// JSON.stringify(deserializeData), | ||
// this.SystemKey | ||
// ); | ||
// const client = await this.getClient(wallet); | ||
|
||
// const result = await client.signAndBroadcast( | ||
// senderInfo.wallet_address, | ||
// msgs, | ||
// 'auto' | ||
// // memo, | ||
// ); | ||
|
||
// return result.transactionHash; | ||
// } | ||
} |
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SysKeyService } from './syskey.service'; | ||
import { KMSBuilderService } from './kms.service'; | ||
import { KeysGraphql } from './keys.graphql'; | ||
import { GraphqlModule } from '../graphql/graphql.module'; | ||
|
||
@Module({ | ||
imports: [GraphqlModule], | ||
providers: [SysKeyService, KMSBuilderService, KeysGraphql], | ||
exports: [SysKeyService], | ||
}) | ||
export class SysKeyModule {} |
Oops, something went wrong.