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

Release/1.5.0 #130

Merged
merged 20 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = {
"@stellar/typescript-wallet-sdk/test/tsconfig.json",
"@stellar/typescript-wallet-sdk-km/tsconfig.json",
"@stellar/typescript-wallet-sdk-km/test/tsconfig.json",
"@stellar/typescript-wallet-sdk-soroban/tsconfig.json",
"@stellar/typescript-wallet-sdk-soroban/test/tsconfig.json",
],
sourceType: "module",
},
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/npmPublishBeta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: typescript-wallet-sdk beta build
on:
push:
branches:
- develop
jobs:
npm-beta:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18

- run: yarn install
- run: yarn build
- run: yarn test:ci

- name: Create beta package version
run: |
timestamp=$(date +%s%3N)
current_version=$(jq -r '.version' @stellar/typescript-wallet-sdk/package.json)
echo "new_version=${current_version}-beta.${timestamp}" >> $GITHUB_ENV

- name: Update package.json version
uses: jossef/action-set-json-field@6e6d7e639f24b3955ef682815317b5613ac6ca12 # v1
with:
file: ./@stellar/typescript-wallet-sdk/package.json
field: version
value: ${{ env.new_version }}

- name: Publish beta build
run: |
cd @stellar/typescript-wallet-sdk
yarn publish --tag beta --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
22 changes: 22 additions & 0 deletions .github/workflows/npmPublishSdkSoroban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: npm publish wallet sdk Soroban
on: [workflow_dispatch]
jobs:
npm-publish:
name: npm-publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
registry-url: https://registry.npmjs.org/
- run: yarn install
- run: yarn build
- run: yarn test:ci

- name: Publish to NPM
run: |
cd @stellar/typescript-wallet-sdk-soroban
yarn publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
16 changes: 16 additions & 0 deletions .github/workflows/playwrightTests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Playwright Tests
on: [pull_request]
jobs:
playwright:
name: "Playwright e2e Tests"
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.43.0-jammy
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
- run: yarn install
- run: yarn build
- run: yarn test:e2e:ci
13 changes: 13 additions & 0 deletions @stellar/typescript-wallet-sdk-km/CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Release notes - Typescript Wallet SDK Key Manager - 1.5.0

### Added
* Challenge token to param to auth header

### Fixed
* Update BrowserStorageConfigParams method types

# Release notes - Typescript Wallet SDK Key Manager - 1.4.0

### Added
* Init to the project, added key manager functionality

3 changes: 2 additions & 1 deletion @stellar/typescript-wallet-sdk-km/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/typescript-wallet-sdk-km",
"version": "1.0.1",
"version": "1.5.0",
"engines": {
"node": ">=18"
},
Expand All @@ -16,6 +16,7 @@
"@stellar/tsconfig": "^1.0.2",
"@types/jest": "^29.5.12",
"@typescript-eslint/parser": "^7.1.1",
"@stellar/typescript-wallet-sdk": "*",
"babel-jest": "^29.7.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { EncryptedKey } from "../Types";
export interface BrowserStorageConfigParams {
prefix?: string;
storage: {
get: (key?: string | string[] | object) => Promise<object>;
get: (
key?: null | string | string[] | Record<string, unknown>,
) => Promise<Record<string, unknown>>;
remove: (key: string | string[]) => Promise<void>;
set: (items: object) => Promise<object>;
set: (items: Record<string, unknown>) => Promise<void>;
};
}

Expand Down
1 change: 1 addition & 0 deletions @stellar/typescript-wallet-sdk-km/src/Types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export interface GetAuthTokenParams {
authServer: string;
authServerHomeDomains: [string];
authServerKey: string;
challengeToken?: string;
account?: string;
clientDomain?: string;
onChallengeTransactionSignature?: (tx: Transaction) => Promise<Transaction>;
Expand Down
8 changes: 7 additions & 1 deletion @stellar/typescript-wallet-sdk-km/src/keyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export class KeyManager {
password,
authServer,
authServerKey,
challengeToken,
authServerHomeDomains,
clientDomain,
onChallengeTransactionSignature = (tx: Transaction) =>
Expand Down Expand Up @@ -302,7 +303,12 @@ export class KeyManager {
challengeUrl += `&client_domain=${encodeURIComponent(clientDomain)}`;
}

const challengeRes = await fetch(challengeUrl);
let headers = {};
if (challengeToken) {
headers = { Authorization: `Bearer ${challengeToken}` };
}

const challengeRes = await fetch(challengeUrl, { headers });

if (challengeRes.status !== 200) {
const challengeText = await challengeRes.text();
Expand Down
108 changes: 108 additions & 0 deletions @stellar/typescript-wallet-sdk-km/test/keyManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import {
TimeoutInfinite,
BASE_FEE,
} from "@stellar/stellar-sdk";
import {
DefaultAuthHeaderSigner,
SigningKeypair,
} from "@stellar/typescript-wallet-sdk";
import { mockRandomForEach } from "jest-mock-random";
import randomBytes from "randombytes";
import sinon from "sinon";
Expand Down Expand Up @@ -500,6 +504,7 @@ describe("fetchAuthToken", () => {
"https://www.stellar.org/auth?account=" +
"GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H" +
"&client_domain=example.com",
{ headers: {} },
);
});

Expand Down Expand Up @@ -615,6 +620,109 @@ describe("fetchAuthToken", () => {
expect(res).toBe(token);
});

test("Can use a challenge token", async () => {
const authServer = "https://www.stellar.org/auth";
const password = "very secure password";

const keyNetwork = Networks.TESTNET;

const token = "👍";
const accountKey = Keypair.random();
const account = new Account(accountKey.publicKey(), "-1");

// set up the manager
const testStore = new MemoryKeyStore();
const testKeyManager = new KeyManager({
keyStore: testStore,
});

testKeyManager.registerEncrypter(IdentityEncrypter);

const keypair = Keypair.master(keyNetwork);

// A Base64 digit represents 6 bits, to generate a random 64 bytes
// base64 string, we need 48 random bytes = (64 * 6)/8
//
// Each Base64 digit is in ASCII and each ASCII characters when
// turned into binary represents 8 bits = 1 bytes.
const value = randomBytes(48).toString("base64");

const tx = new TransactionBuilder(account, {
fee: BASE_FEE,
networkPassphrase: keyNetwork,
})
.addOperation(
Operation.manageData({
name: `stellar.org auth`,
value,
source: keypair.publicKey(),
}),
)
.addOperation(
Operation.manageData({
name: "web_auth_domain",
value: new URL(authServer).hostname,
source: account.accountId(),
}),
)
.setTimeout(300)
.build();

tx.sign(accountKey);

jest
.spyOn(global, "fetch")
.mockResolvedValueOnce(
new Response(
JSON.stringify({
transaction: tx.toXDR(),
network_passphrase: keyNetwork,
}),
),
)
.mockResolvedValueOnce(
new Response(
JSON.stringify({
token,
status: 1,
message: "Good job friend",
}),
),
);

// save this key
const keyMetadata = await testKeyManager.storeKey({
key: {
type: KeyType.plaintextKey,
publicKey: keypair.publicKey(),
privateKey: keypair.secret(),
network: keyNetwork,
},
password,
encrypterName: "IdentityEncrypter",
});

// create a challengeToken
const authHeaderSigner = new DefaultAuthHeaderSigner();
const challengeToken = await authHeaderSigner.createToken({
claims: {},
issuer: new SigningKeypair(accountKey),
});

await testKeyManager.fetchAuthToken({
id: keyMetadata.id,
password,
authServer,
challengeToken,
authServerKey: account.accountId(),
authServerHomeDomains: ["stellar.org"],
});

expect(
(global.fetch as any).mock.calls[0][1].headers["Authorization"],
).toBeTruthy();
});

test("Rejects TXs with non-zero seq numbers", async () => {
const authServer = "https://www.stellar.org/auth";
const password = "very secure password";
Expand Down
8 changes: 8 additions & 0 deletions @stellar/typescript-wallet-sdk-soroban/CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Release notes - Typescript Wallet SDK Key Soroban - 1.5.0

### Added
* Init to the project, added soroban functionality
* getTokenInvocationArgs function
* Token parsing/formatting functions
* scValyByType function
* generic getInvocationDetails helper
80 changes: 80 additions & 0 deletions @stellar/typescript-wallet-sdk-soroban/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Stellar Typescript Wallet Soroban SDK [![npm version](https://badge.fury.io/js/@stellar%2Ftypescript-wallet-sdk-soroban.svg)](https://badge.fury.io/js/@stellar%2Ftypescript-wallet-sdk-soroban)

The Typescript Wallet Soroban SDK is a work-in-progress library that (currently)
allows developers to use soroban helpers in their wallet applications. It works
in conjuction with the main
[Typescript Wallet SDK](https://github.com/stellar/typescript-wallet-sdk) to
hold all the functionality a developer would need to create a wallet for the
stellar network.

## Dependency

The library is available via npm. To import `typescript-wallet-sdk-soroban` you
need to add it as a dependency to your code:

yarn:

```shell
yarn add @stellar/typescript-wallet-sdk-soroban
```

npm:

```shell
npm install @stellar/typescript-wallet-sdk-soroban
```

## Introduction

Here's some examples on how to use the Soroban helpers:

```typescript
import {
getTokenInvocationArgs,
formatTokenAmount,
parseTokenAmount,
scValByType,
} from "@stellar/typescript-wallet-sdk-soroban";

const transaction = TransactionBuilder.fromXDR(
"AAAAAgAAAACM6IR9GHiRoVVAO78JJNksy2fKDQNs2jBn8bacsRLcrDucaFsAAAWIAAAAMQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAGAAAAAAAAAABHkEVdJ+UfDnWpBr/qF582IEoDQ0iW0WPzO9CEUdvvh8AAAAIdHJhbnNmZXIAAAADAAAAEgAAAAAAAAAAjOiEfRh4kaFVQDu/CSTZLMtnyg0DbNowZ/G2nLES3KwAAAASAAAAAAAAAADoFl2ACT9HZkbCeuaT9MAIdStpdf58wM3P24nl738AnQAAAAoAAAAAAAAAAAAAAAAAAAAFAAAAAQAAAAAAAAAAAAAAAR5BFXSflHw51qQa/6hefNiBKA0NIltFj8zvQhFHb74fAAAACHRyYW5zZmVyAAAAAwAAABIAAAAAAAAAAIzohH0YeJGhVUA7vwkk2SzLZ8oNA2zaMGfxtpyxEtysAAAAEgAAAAAAAAAA6BZdgAk/R2ZGwnrmk/TACHUraXX+fMDNz9uJ5e9/AJ0AAAAKAAAAAAAAAAAAAAAAAAAABQAAAAAAAAABAAAAAAAAAAIAAAAGAAAAAR5BFXSflHw51qQa/6hefNiBKA0NIltFj8zvQhFHb74fAAAAFAAAAAEAAAAHa35L+/RxV6EuJOVk78H5rCN+eubXBWtsKrRxeLnnpRAAAAACAAAABgAAAAEeQRV0n5R8OdakGv+oXnzYgSgNDSJbRY/M70IRR2++HwAAABAAAAABAAAAAgAAAA8AAAAHQmFsYW5jZQAAAAASAAAAAAAAAACM6IR9GHiRoVVAO78JJNksy2fKDQNs2jBn8bacsRLcrAAAAAEAAAAGAAAAAR5BFXSflHw51qQa/6hefNiBKA0NIltFj8zvQhFHb74fAAAAEAAAAAEAAAACAAAADwAAAAdCYWxhbmNlAAAAABIAAAAAAAAAAOgWXYAJP0dmRsJ65pP0wAh1K2l1/nzAzc/bieXvfwCdAAAAAQBkcwsAACBwAAABKAAAAAAAAB1kAAAAAA==",
Networks.FUTURENET,
) as Transaction<Memo<MemoType>, Operation.InvokeHostFunction[]>;
const op = transaction.operations[0];

const args = getTokenInvocationArgs(op);
/*
extracts args from the invoke host function operation:
args = {
fnName: "transfer,
contractId: "CAPECFLUT6KHYOOWUQNP7KC6PTMICKANBURFWRMPZTXUEEKHN67B7UI2",
from: "GCGORBD5DB4JDIKVIA536CJE3EWMWZ6KBUBWZWRQM7Y3NHFRCLOKYVAL",
to: "GDUBMXMABE7UOZSGYJ5ONE7UYAEHKK3JOX7HZQGNZ7NYTZPPP4AJ2GQJ",
amount: 5
}
*/

const formattedAmount = formatTokenAmount("10000123", 3);
// converts smart contract token amount into a displayable amount that can be
// used on client UI
// formattedAmount = "10000.123"

const parsedAmount = parseTokenAmount("10000.123", 3);
// converts an amount to a whole (bigint) number that can be used on
// smart contracts operations
// parsedAmount = 10000123

const accountAddress = xdr.ScVal.scvAddress(
xdr.ScAddress.scAddressTypeAccount(
xdr.PublicKey.publicKeyTypeEd25519(
StrKey.decodeEd25519PublicKey(
"GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB",
),
),
),
);

const addressString = scValByType(accountAddress);
// converts smart contract complex value into a simple string
// addressString = "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"
```
5 changes: 5 additions & 0 deletions @stellar/typescript-wallet-sdk-soroban/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const parentConfig = require("../../babel.config");

module.exports = {
...parentConfig,
};
Loading
Loading