Skip to content

Commit

Permalink
console: Move EUI transformation to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnaIyer committed Jul 10, 2023
1 parent f5055d9 commit c912678
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
5 changes: 1 addition & 4 deletions pkg/webui/console/views/device-general-settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
selectNsConfig,
} from '@ttn-lw/lib/selectors/env'

import { hexToBase64 } from '@console/lib/bytes'
import {
mayEditApplicationDeviceKeys,
mayReadApplicationDeviceKeys,
Expand Down Expand Up @@ -101,9 +100,7 @@ const DeviceGeneralSettings = () => {
const {
ids: { dev_eui: devEui, join_eui: joinEui },
} = device
await dispatch(
attachPromise(unclaimDevice(appId, devId, hexToBase64(devEui), hexToBase64(joinEui))),
)
await dispatch(attachPromise(unclaimDevice(appId, devId, devEui, joinEui)))
}, [appId, devId, device, dispatch])

const handleUnclaimFailure = useCallback(async () => {
Expand Down
5 changes: 3 additions & 2 deletions sdk/js/src/service/claim.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import autoBind from 'auto-bind'

import hexToBase64 from '../util/bytes'
import Marshaler from '../util/marshaler'

class DeviceClaim {
Expand Down Expand Up @@ -62,8 +63,8 @@ class DeviceClaim {
}

const response = await this._api.EndDeviceClaimingServer.Unclaim(params, {
dev_eui: devEui,
join_eui: joinEui,
dev_eui: hexToBase64(devEui),
join_eui: hexToBase64(joinEui),
})

return Marshaler.payloadSingleResponse(response)
Expand Down
46 changes: 46 additions & 0 deletions sdk/js/src/util/bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright © 2023 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* Converts hex encoded string to base64.
*
* @param {string} str - Hex encoded string.
* @returns {string} - `str` base64 encoded.
*/
export const hexToBase64 = str =>
btoa(
String.fromCharCode.apply(
null,
str
.replace(/\r|\n/g, '')
.replace(/([\da-fA-F]{2}) ?/g, '0x$1 ')
.replace(/ +$/, '')
.split(' '),
),
)

/**
* Converts base64 encoded string to hex.
*
* @param {string} str - Base64 encoded string.
* @returns {string} - `str` hex encoded.
*/
export const base64ToHex = str =>
Array.from(atob(str.replace(/[ \r\n]+$/, '')))
.map(char => {
const tmp = char.charCodeAt(0).toString(16)

return tmp.length > 1 ? tmp : `0${tmp}`
})
.join('')

0 comments on commit c912678

Please sign in to comment.